Write a Modular C Programming code for All Operations ( inserting at selected position | counting & searching | Display & exit) used in Nodes of Singly linked list | Data Structure.
The Programme should be like a Menu card:(using case numbers) The code should do the operation which is given by the user.
Read & Display the Data given to the node.
Refer to Notes: Notes on Data Structures And Algorithms – Linked List
CODE:
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; struct node *head; struct node* insert_at_end(struct node *); void display(struct node *); int count_nodes(struct node *); struct node * insert_at_pos(struct node *, int); void search(struct node *, int); struct node *temp,*temp1,*newnode; struct node* insert_at_beg(struct node *); main() { int ch,c,pos,n,k,p; head=NULL; printf("\nEnter the Number of Nodes you want: "); scanf("%d",&n); printf("\nEnter the Datails of Nodes:\n"); while(p!=n) { newnode=(struct node*)malloc(sizeof(struct node)); scanf("%d",&newnode->data); head=insert_at_end(newnode); p++; } while(1) { printf("\n\n+++++++++++++++++++++++++++++++++++++++++++++\n"); printf("1) Display linked list\n"); printf("2) Count no of nodes\n"); printf("3) Insert a node at a specific position\n"); printf("4) Search for a given node\n"); printf("5) Exit\n"); printf("+++++++++++++++++++++++++++++++++++++++++++++\n"); printf("\n-> Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: display(head);break; case 2: c=count_nodes(head); printf("no of nodes=%d\n",c);break; case 3: printf("Enter position: "); scanf("%d",&pos); if(pos<1||pos>count_nodes(head)+1) { printf("Invalid pos\n"); } else { newnode=(struct node*)malloc(sizeof(struct node)); printf("Enter data: "); scanf("%d",&newnode->data); head=insert_at_pos(newnode,pos); } break; case 4: printf("Enter the data you want to search : "); scanf("%d",&k); search(head,k); break; case 5: exit(0);break; default: printf("Invalid choice\n"); } } } struct node* insert_at_end(struct node *newnode) { if(head==NULL) { head=newnode; head->next=NULL; return(head); } else { temp=head; while(temp->next!=NULL) { temp=temp->next; } temp->next=newnode; newnode->next=NULL; return(head); } } void display(struct node *head) { if(head==NULL) { printf("The linkded list is empty\n"); } else { temp=head; while(temp!=NULL) { printf("%d->",temp->data); temp=temp->next; } printf("\n"); } } int count_nodes(struct node *head) { int count=0; if(head==NULL) return(count); else { temp=head; while(temp!=NULL) { count++; temp=temp->next; } return(count); } } struct node* insert_at_beg(struct node *newnode) { if(head==NULL) { head=newnode; head->next=NULL; return(head); } else { newnode->next=head; head=newnode; return(head); } } struct node* insert_at_pos(struct node* newnode, int pos) { int p=1; if(pos==1) head=insert_at_beg(newnode); else { temp=head; while(p!=pos) { temp1=temp; temp=temp->next; p++; } temp1->next=newnode; newnode->next=temp; } return(head); } void search(struct node* head, int k) { int i,fu; fu=count_nodes(head); temp=head; for(i=1;i<=fu;i++) { if(k==temp->data) { printf("\nThe data is found!!!"); printf("\nThe position of data is: %d",i); return(0); } temp=temp->next; } printf("\nThe Entered Data is not Found!!!"); }
OUTPUT
Enter the Number of Nodes you want: 5 Enter the Datails of Nodes: 10 20 30 40 50 +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 1 10->20->30->40->50-> +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 2 no of nodes=5 +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 3 Enter position: 2 Enter data: 60 +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 1 10->60->20->30->40->50-> +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 2 no of nodes=6 +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 4 Enter the data you want to search : 70 The Entered Data is not Found!!! +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 20 Invalid choice +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 4 Enter the data you want to search : 20 The data is found!!! The position of data is: 3 +++++++++++++++++++++++++++++++++++++++++++++ 1) Display linked list 2) Count no of nodes 3) Insert a node at a specific position 4) Search for a given node 5) Exit +++++++++++++++++++++++++++++++++++++++++++++ -> Enter your choice: 5 Process returned 0 (0x0) execution time : 48.558 s Press any key to continue.
© Credits
Supreet Palankar (2)
'H' div
Department of Computer Science
( ) — Number of Contributions!
Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:
Top 100+ C Programming codes – KLE Technological University