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* link;
- };
- struct node* root=NULL;
- void add_begin()
- {
- struct node* temp;
- temp=(struct node*)malloc(sizeof(struct node));
- printf("enter data to add at begin\n");
- scanf("%d",&temp->data);
- temp->link=root;
- root=temp;
- printf("pehle pe add ho gya\n");
- }
- void traverse()
- {
- struct node* temp;
- temp=root;
- if(root==NULL)
- printf("linked list is empty\n");
- else
- {
- while(temp->link!=NULL)
- {
- printf("%d\n",temp->data);
- temp=temp->link;
- }
- printf("%d\n",temp->data);
- //last elemnt k liye
- }
- }
- void append()
- {
- struct node* temp;
- temp=(struct node*)malloc(sizeof(struct node));
- printf("enter data\n");
- scanf("%d",&temp->data);
- temp->link=NULL;
- if(root==NULL)
- root=temp;
- else
- {
- struct node* p;
- p=root;
- while(p->link!=NULL)
- p=p->link;
- p->link=temp;
- }
- }
- void main()
- {
- printf("You can only append the link list\n");
- int count;
- printf("enter number of nodes\n");
- scanf("%d",&count);
- traverse();
- while(count>0)
- {
- append();
- count--;
- }
- printf("linked list ban gya\n");
- traverse();
- add_begin();
- traverse();
- //while isliye use kiya ki jab tak user khud program cut na
- //kare tab tak input leta rhe
- }
Add Comment
Please, Sign In to add comment