☰
×
◈ Home
◈ Array
◈ Stack Using Array
◈ Stack Using Linked List
◈ Queue Using Array
◈ Queue Using Linked List
◈ Singly Linked List
◈ Doubly Linked List
◈ Circular Linked List
◈ Trees
◈ Graphs
◈ Hashes
◈ Heaps
Home
About
Consulting
Training
Tutorials
Interview Questions
Contact
C Programs
C Data Structure Programs
Home
C Data Structure Programs
◈ Home
◈ Array
◈ Stack Using Array
◈ Stack Using Linked List
◈ Queue Using Array
◈ Queue Using Linked List
◈ Singly Linked List
◈ Doubly Linked List
◈ Circular Linked List
◈ Trees
◈ Graphs
◈ Hashes
◈ Heaps
C Data Structure Programs
Singly Linked List
« Prev
Next »
Singly Linked List - Operation
☛ Add Data at End
☛ Add Data at Begining
☛ Add Data after number
☛ Add Data after number
☛ Add Data before node
☛ Add Data after node
☛ Display
☛ Number of Node
☛ Delete by Node
☛ Reversing List
Singly Linked List - Full Operation
#include
#include
#include
int add_end(int num); int add_beg(int num); int add_after_num(int val, int val1); int add_before_num(int val, int val1); int add_after_node(int val, int val1); int add_before_node(int val, int val1, int j); int display(); int sum(int j); int delete_num(int num); int del_node(int val); int reverse(int n); struct node{ int data; struct node *next; }*head; struct node *curn, *newn; int main() { int i,j=0, val, val1,c=0; struct node *a; head=NULL; while (1) { printf("\n"); printf ("\n Single Linked List Operations\n"); printf ("=================\n"); printf ("1. add_at_end\n"); printf ("2. add_at_beg\n"); printf ("3. add_after_number\n"); printf ("4. add_before_number\n"); printf ("5. add_after_node \n"); printf ("6. add_before_node \n"); printf ("7. Display\n"); printf ("8. Number of Node\n"); printf ("9. Delete by Number\n"); printf ("10. Delete by Node\n"); printf ("11. Reversing List\n"); printf ("12. exit\n\n"); printf ("Enter your choice: "); scanf ("%d", &i); printf("\n"); switch(i) { case 1: /* add_at_end */ { printf("Enter data: "); scanf("%d", &val); add_end(val); break; } case 2: /* add_at_Begining */ printf("Enter value: "); scanf("%d",&val); add_beg(val); break; case 3: /* Insert the value after the given number */ printf("Enter value in the list to insert after: "); scanf("%d", &val); curn=head; printf("Enter value to insert: "); scanf("%d", &val1); add_after_num(val, val1); break; case 4: /* Insert the value before the given number */ printf("Enter the number in the list to insert before: "); scanf("%d",&val); printf("Enter new value to insert: "); scanf("%d", &val1); add_before_num(val,val1); break; case 5: /* Insert the value after the given node */ c=sum(j); printf(" Enter nth node to insert after: "); scanf (" %d",&val ); if(val>0 && val<=c) { printf(" Enter value to insert: "); scanf (" %d", &val1); add_after_node(val, val1); break; } printf("You have selected the node is OUT of Range \n"); break; case 6: /* Insert the value before the given node */ printf(" Enter nth node to insert before: "); scanf(" %d", &val ); c=sum(j); // if(val<=sum(j)) if(val<=c && val>0) { printf(" Enter value to insert: "); scanf(" %d", &val1); add_before_node(val, val1, c); break; } else printf("The No of node you entered in wrong. \n"); break; case 7: display(); break; case 8: /* To get number of Node in the List */ sum(j); printf("Number of node in the list is %d \n", sum(j)); break; case 9: /* Delete operation by given number*/ if(head==NULL) { printf("List is Empty\n"); break; } int n; printf("Enter the num to delete: "); scanf("%d", &n); delete_num(n); break; case 10: /* Delete operation by node */ if(head==NULL) { printf("Node is Empty \n"); break; } else { c=sum(j); printf("Enter the node to delete\n"); scanf("%d",&val); if(val<=c && val>0) { del_node(val); break; } else { printf("Wrong node selection. \n"); break; } } case 11: /* Reversing the List of element */ if(head!=NULL) { if(head->next!=NULL) { int d=sum(j); reverse(d); break; } else { printf("For reverse we need atleast 2 node\n"); break; } } else { printf("List is empty.\n"); break; } case 12: /* To Exit from the Program */ return 0; // or exit(0) default: printf("Wrong Option\n"); } // switch } // while } // main() int add_end(int num) { newn=(struct node*) malloc(sizeof(struct node)); newn->data=num; if(head == NULL) { head=curn=newn; } else { while(curn->next!=NULL) { curn=curn->next; } curn->next=newn; } newn->next=NULL; curn=head; printf("%d is added successfully at the end of the list \n", num); return; } int add_beg(int num) { newn= head; newn= (struct node *) malloc (sizeof (struct node)); newn->data = num; if (head == NULL) { head = newn; head->next = NULL; } else { newn->next = head; head = newn; } printf("%d is successfully added at the beginning \n", num); return; } int add_after_num(int val, int val1) { curn=head; newn=malloc(sizeof(struct node)); newn->data=val1; while(val==curn->data || curn->next != NULL ) { if(val==curn->data) { newn->next=curn->next; curn->next=newn; printf("%d is added successfully after %d \n",val1, val); return; } else { curn=curn->next; } } printf("The given number is not in the list\n"); return 1; } int add_before_num(int val, int val1) { struct node *newn, *curn, *prev; prev=curn=head; newn=malloc(sizeof(struct node)); newn->data=val1; while(curn!=NULL) { if(curn->data==val) { if(curn==head) { newn->next=head; head=newn; return; } else { newn->next=prev->next; prev->next=newn; return; } } else { prev=curn; curn=curn->next; } } return; } int add_after_node(int val, int val1) { int i; newn=malloc(sizeof(struct node)); newn->data=val1; curn=head; if(head!=NULL) { for(i=1;i
next; } newn->next=curn->next; curn->next=newn; } else { printf("List is empty\n"); } } int add_before_node(int val, int val1, int j) { struct node *newn, *curn, *prev; int i; newn=malloc(sizeof(struct node)); newn->data=val1; prev=curn=head; printf("%d", j); if(head!=NULL && val<=j) { for(i=1; i
next; } if(val==1) { newn->next=prev; head=prev=newn; return; } else { newn->next=prev->next; prev->next=newn; curn=newn; return; } } else { printf("List is empty\n"); } return; } int display() { struct node *r; r = head; if (r == NULL) { printf("List is Empty\n"); return; } while (r != NULL) { printf ("%d => ", r->data); r = r->next; } printf (" NUll\n"); } int sum(int j) { if(head!=NULL) { while(curn!=NULL) { j++; curn=curn->next; } curn=head; return j; } else return 0; } int delete_num(int num) { struct node *curn, *prev; prev=curn=head; while(curn->data!=num && curn!=NULL) { prev=curn; curn=curn->next; } if(curn->data==num) { if(curn==head) { free(curn); head=NULL; return; } else { free(curn); prev->next=NULL; curn=head; return; } } else { printf("The given number is not in the list \n"); return; } } int del_node(int val) { int i; struct node *curn, *prev; curn=prev=head; for(i=1; i
next; } if(val==1) { if(curn->next==NULL) { free(head); head=NULL; return; } curn=curn->next; free(prev); head=curn; return; } else { prev->next=curn->next; free(curn); prev=curn=head; return; } } int reverse(int n) { struct node *first, *last, *temp; first=last=head; int i=1,j, k, l; k=(n+1)/2; l=n; while(i++<=k) { j=1; while(j
next; j++; } l--; if(first==last) break; temp=first->data; first->data=last->data; last->data=temp; first=first->next; last=head; // return; //should not return here. otherwise one time only loop will execute. } // return; // no need to return. bcz struct declared globally. }
« Prev
Next »
If you have any queries please email us at
info@motionzen.com