Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- } *start=NULL;
- void add_start();
- void add_end();
- void delete_start();
- void delete_end();
- void print();
- int main()
- {
- int choice;
- //system("cls");
- system("cls");
- while(1)
- {
- printf("\nEnter choice");
- printf("\n1) Print linked list\n2)Add element at start\n3)Add element at end\n4)Clear linked list\n5)Delete element at start\n6)Delete element at end\n7)Exit the program\n");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: system("cls");
- print();
- break;
- case 2: system("cls");
- add_start();
- break;
- case 3: system("cls");
- add_end();
- break;
- case 4: start=NULL;
- system("cls");
- printf("\nCleared linked list. (Memory didn't get freed. Wait for update :P)\n");
- break;
- case 5: system("cls");
- delete_start();
- break;
- case 6: system("cls");
- delete_end();
- break;
- case 7: exit(0);
- break;
- default: printf("\nInvalid");
- }
- }
- return 0;
- }
- void add_start()
- {
- struct node *new;
- new=(struct node *) (malloc(sizeof(struct node)));
- printf("\nEnter data to add at start:");
- scanf("%d",&(new->data));
- new->next=NULL;
- if(start==NULL)
- {
- start=new;
- return;
- }
- new->next=start;
- start=new;
- }
- void print()
- {
- if(start==NULL)
- {
- printf("\nNo data in Linked List\n");
- return;
- }
- struct node *p;
- p=start;
- printf("\nLinked list: \n");
- while(p!=NULL)
- {
- printf("\n%d",p->data);
- p=p->next;
- }
- }
- void add_end()
- {
- struct node *new;
- new=(struct node *) (malloc(sizeof(struct node)));
- new->next=NULL;
- printf("\nEnter data to add at end:");
- scanf("%d",&(new->data));
- struct node *p;
- p=start;
- if(start==NULL)
- {
- start=new;
- return;
- }
- while(p->next!=NULL)
- {
- p=p->next;
- }
- p->next=new;
- }
- void delete_start()
- {
- struct node *p;
- if(start==NULL)
- {
- printf("\nLink list is empty\n");
- return;
- }
- else
- {
- p=start;
- start=start->next;
- free(p);
- return;
- }
- }
- void delete_end()
- {
- struct node *p,*n;
- if(start==NULL)
- {
- printf("\nLink List Is Empty\n");
- return;
- }
- else
- {
- p=start;
- if(p->next!=NULL)
- {
- while(p->next->next!=NULL)
- p=p->next;
- n=p->next;
- p->next=NULL;
- free(n);
- return;
- }
- else
- {
- start=NULL;
- free(p);
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement