Write a Menu Driven Program using C for singly list -To Create Linked List -To Display Linked List -To Add Not at Last Positon of Linked List -To Delete Node From Last Position of Linked List.
#include#include #include struct node { int data; struct node *link; }*head; void createlist(); void display(); void addlast(); void deletelast(); void main() { int ch; clrscr(); head=NULL; while(1) { printf("\n 1.To Create Linked List"); printf("\n 2.To Display Linked List"); printf("\n 3.To Add Not at Last Positon of Linked List."); printf("\n 4.To Delete Node From Last Position of Linked List."); printf("\n 5.Exit."); printf("\n Enter Your Choice: "); scanf("%d",&ch); switch(ch) { case 1:createlist(); break; case 2:display(); break; case 3:addlast(); break; case 4:deletelast(); break; case 5:exit(0); default: printf("\n Invalid Choice"); } } } void createlist() { struct node *newnode,*prev; int ele,n,i; printf("\n How Many Nodes?: "); scanf("%d",&n); head=NULL; for(i=1;i<=n;i++) { newnode=(struct node*)malloc(sizeof(struct node)); if(newnode==NULL) { printf("\n List is Empty"); return; } printf("\n Enter Element %d :",i); scanf("%d",&ele); newnode->data=ele; newnode->link=NULL; if(head==NULL) { head=newnode; prev=newnode; } else { prev->link=newnode; prev=newnode; } } } void display() { struct node *temp; if(head==NULL) { printf("\n Linked List is Empty."); return; } temp=head; printf("\n Element in the list are : "); while(temp!=NULL) { printf("\t%d",temp->data); temp=temp->link; } } void addlast() { struct node *newnode,*temp; int ele; newnode=(struct node*)malloc(sizeof(struct node)); if(newnode==NULL) { printf("\n Insufficient Memory"); exit(0); } printf("\n Enter The Last Element To Be added :"); scanf("%d",&ele); newnode->data=ele; newnode->link=NULL; temp=head; while(temp->link!=NULL) { temp=temp->link; } temp->link=newnode; } void deletelast() { struct node *temp,*curr; temp=head; if(head==NULL) { printf("\n Linked List is Empty."); return; } while(temp->link->link!=NULL) { temp=temp->link; } curr=temp->link; temp->link=NULL; free(curr); } /*OUTPUT 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice: 1 How Many Nodes?: 3 Enter Element 1 :2 Enter Element 2 :1 Enter Element 3 :3 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice: 2 Element in the list are : 2 1 3 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice: 3 Enter The Last Element To Be added :4 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice: 2 Element in the list are : 2 1 3 4 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice: 4 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice: 2 Element in the list are : 2 1 3 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice: 4 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice: 2 Element in the list are : 2 1 1.To Create Linked List 2.To Display Linked List 3.To Add Not at Last Positon of Linked List. 4.To Delete Node From Last Position of Linked List. 5.Exit. Enter Your Choice:
Write a Menu Driven Program using C for singly list -To Create Linked List -To Display Linked List -To Add Not at Last Positon of Linked List -To Delete Node From Last Position of Linked List.
Reviewed by
on
November 17, 2013
Rating: