Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by shreenath on 24/7/16.
- //
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- int data;
- struct node* link;
- }*head;
- struct node *head=NULL;
- void insertFront(int insertData)
- {
- struct node *temp=(struct node*)malloc(sizeof(struct node));
- temp->data=insertData;
- temp->link=head->link;
- head=temp;
- printf("\nNode Added at Front : Data = %d",head->data);
- }
- void insertMiddle(int insertData, int key)
- {
- struct node *temp=(struct node*)malloc(sizeof(struct node));
- struct node *pre=(struct node*)malloc(sizeof(struct node));
- temp->data=insertData;
- pre=head;
- while(pre->data!=key)
- pre=pre->link;
- temp->link=pre->link;
- pre->link=temp;
- printf("\nNode Added at Middle : Data = %d",temp->data);
- }
- void insertLast(int insertData)
- {
- struct node *temp=(struct node*)malloc(sizeof(struct node));
- struct node *pre=(struct node*)malloc(sizeof(struct node));
- temp->data=insertData;
- pre=head;
- while(pre->link!=NULL)
- pre=pre->link;
- pre->link=temp;
- temp->link=NULL;
- printf("\nNode Added at Last : Data = %d",temp->data);
- }
- void traversal()
- {
- struct node *pre=(struct node*)malloc(sizeof(struct node));
- int i;i=0;
- pre=head;
- printf("\nLinked List Traversal");
- while(pre->link!=NULL)
- {
- printf("Node %d - %d\n",i,pre->data);
- pre=pre->link;
- i++;
- }
- }
- void main()
- {
- int ch,key;
- int newdata;
- printf("1. Insertion at Front"
- "\n2. Insertion at Middle"
- "\n3. Insertion at Last"
- "\n4. Traversal"
- "\n5. Exit");
- do
- {
- printf("\nEnter your Choice: ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1: printf("\nEnter Data: ");
- scanf("%d",&newdata);
- insertFront(newdata);
- break;
- case 2: printf("\nEnter Data and Key: ");
- scanf("%d,%d",&newdata,&key);
- insertMiddle(newdata,key);
- break;
- case 3: printf("\nEnter Data: ");
- scanf("%d",&newdata);
- insertLast(newdata);
- break;
- case 4: traversal();
- break;
- case 5: printf("\nThank You");
- break;
- default: printf("\nInvalid Entry! Re-Enter");
- }
- }while(ch!=5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement