#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; struct node *head=NULL; void create(); void delEnd(); void print(); void main() { int choice; do { printf("\n__MENU__\n1.Create List\n2.Delete from the End.\n3...EXIT...\n"); printf("\nENTER your choice\n"); scanf("%d",&choice); switch(choice) { case 1: create();break; case 2 :delEnd();break; case 3:printf("~~~~~~~THANK YOU~~~~~~~\n");break; default:printf("INVALID input !!!");break; } }while(choice!=3); } void delEnd() { int p=0; struct node*temp1,*temp2; temp1=temp2=head; if(head==NULL) { printf("LIST is empty\n"); p=1; } else if(head->next==NULL) { head=NULL; } else { while(temp1->next!=NULL) { temp1=temp1->next; if(temp1->next!=NULL) temp2=temp2->next; }temp2->next=NULL; free(temp1); } if(p!=1) print(); } void create() { int x, i,n; head=NULL; struct node* newnode,*temp; temp=head; printf("Enter no. of elements in the LIST \n"); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { temp=head; newnode=(struct node*)malloc(sizeof(struct node*)); scanf("%d",&x); newnode->data=x; if(head==NULL) { head=newnode; newnode->next=NULL; } else { while(temp->next!=NULL) temp=temp->next; temp->next=newnode; newnode->next=NULL; } } print(); } void print() { struct node* temp; temp=head; if(head==NULL) printf("list is empty !!!\n\n"); else { printf("\nLIST elements are\t"); while(temp!=NULL) { printf("%d\t",temp->data); temp=temp->next; } } }
|
|
Labels: Data Structure in C, Single Linked List