0% found this document useful (0 votes)
8 views9 pages

C Program for Singly Linked List Operations

This document contains a C program that implements a singly linked list with various operations such as creating nodes, inserting at the front and end, deleting from the front and end, displaying the list, and searching for elements. The main function provides a menu-driven interface for users to interact with the linked list. The program includes error handling for empty lists and invalid choices.

Uploaded by

Pratibha S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

C Program for Singly Linked List Operations

This document contains a C program that implements a singly linked list with various operations such as creating nodes, inserting at the front and end, deleting from the front and end, displaying the list, and searching for elements. The main function provides a menu-driven interface for users to interact with the linked list. The program includes error handling for empty lists and invalid choices.

Uploaded by

Pratibha S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<stdio.

h>

#include<stdlib.h>

struct node

int data;

struct node*next;

};

struct node * start=NULL;

struct node *temp, *p,*q;

void createnode()

temp=(struct node*)malloc(sizeof(struct node));

printf("\n Enter data:");

scanf("%d", &temp->data);

temp->next=NULL;

void insertfront()

{
createnode();

if(start==NULL)

start=temp;

else

temp->next=start;

start=temp;

void insertend()

createnode();

if(start==NULL)

start=temp;

else

q=start;

while(q->next!=NULL)

q=q->next;

q->next=temp;

}
void deletefront()

if(start==NULL)

printf("EMPTY LIST \n");

else

q=start;

start=start->next;

printf("\n Student deleted is %d ",q->data);

free(q);

void deleteend()

if(start==NULL)

printf("Thelistisempty. \n");

else

q=start;

while(q->next!=NULL)
{

p=q;

q=q->next;

p->next=NULL;

printf("\n Student deleted is %d",q->data);

free(q);

void display()

if(start==NULL)

printf("\n Listisempty.\n");

else

printf("\n -------------STUDENT DETAILS----------------\n");

for(q=start; q!=NULL;q= q->next)

printf("%d------>",q->data);

}
void search()

int key, flag=0;

printf("\nEnter key element to be searched");

scanf("%d", &key);

for(q=start;q!=NULL;q=q->next)

if(q->data==key)

flag=1;

break;

if(flag==1)

printf("\n Search suceessful");

else

printf("\n Search unsuccessful");

void insert()

createnode();

int flag=0;
q=start;

while(q!=NULL)

p=q;

if(q->data==30)

q=q->next;

p->next=temp;

temp->next=q;

else

q=q->next;

}
int main()

int choice,n,i;

while(1)

printf("\n\n-----SINGLYLINKEDLIST MENU \n");

printf("1:CREATE\n");

printf("2:DISPLAY\n");

printf("3:INSERT AT END\n");

printf("4:INSERT AT FRONT\n");

printf("5:DELETE AT END\n");

printf("6:DELETE FROM FRONT\n");

printf("7:SEARCH\n");

printf("8:INSERT\n");

printf("[Link]\n");

printf("Enteryourchoice\n");

scanf("%d",&choice);

switch(choice)

case 1:printf("Enter numberofstudents:\n");

scanf("%d",&n);

for(i=1;i<=n;i++)

insertfront();

printf("\n LIST CREATED\n");


break;

case 2: display();

break;

case 3:insertend();

break;

case 4: insertfront();

break;

case 5:deleteend();

break;

case 6: deletefront();

break;

case 7:search();

break;

case 8:insert();

break;

case 9: exit(0);

default:printf("InvalidChoice\n");

}
}

You might also like