0% found this document useful (0 votes)
77 views

Singly Linked List

The document describes functions to implement a singly linked list in C including insert, delete, find, find previous, and display functions. It defines a node struct with element and next pointer fields. The main function allows the user to choose between insert, delete, and display options and calls the corresponding functions. The insert function allocates a new node, sets its element value, and inserts it into the list. The deletion function finds the previous node, links around the target node, and frees the memory. The display function traverses the list and prints the element values.

Uploaded by

mkharish24
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Singly Linked List

The document describes functions to implement a singly linked list in C including insert, delete, find, find previous, and display functions. It defines a node struct with element and next pointer fields. The main function allows the user to choose between insert, delete, and display options and calls the corresponding functions. The insert function allocates a new node, sets its element value, and inserts it into the list. The deletion function finds the previous node, links around the target node, and frees the memory. The display function traverses the list and prints the element values.

Uploaded by

mkharish24
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Singly linked list: #include<stdio.h> #include<conio.h> #include<stdlib.

h> void insert(int x); void deletion(int x); void display(); struct node { int element; struct node*next; }*list=NULL,*p; struct node*find(int s) { p=list->next; while(p!=NULL && p->element!=s) p=p->next; return p; } struct node*findprevious(int s) { p=list; while(p->next !=NULL && p->next->element !=s) p=p->next; return p; } void main() { int data,ch; clrscr(); printf("\n\n1.INSERT\n\n2.DELETE\n\n3.DISPLAY"); do { printf("\n\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\n\nEnter the element to be inserted:"); scanf("%d",&data); insert(data); break; case 2: printf("\n\nEnter the element to be deleted:"); scanf("%d",&data); deletion(data);

break; case 3: display(); break; default: printf("\n\nInvalid choice....."); getch(); exit(0); } }while(ch<4); } void insert(int x) { struct node*newnode; int pos; newnode=malloc(sizeof(struct node)); newnode->element=x; if(list->next==NULL) { list->next=newnode; newnode->next=NULL; } else { printf("\n\nEnter hte position of hte element to be inserted:"); scanf("%d",&pos); newnode->next=p->next; p->next=newnode; } } void deletion(int x) { struct node*temp; temp=malloc(sizeof(struct node)); p=findprevious(x); if(p->next!=NULL) { temp=p->next; p->next=temp->next; printf("\n\nThe deleted element is %d",temp->element); free(temp); } else printf("\n\nElement is not found in the list!!!"); } void display()

{ if(list->next==NULL) printf("\n\nlist is empty!!!"); else { p=list->next; printf("\n\nThe contents of yhte list are\n\n"); while(p!=NULL) { printf("%d->",p->element); p=p->next; } } }

You might also like