Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- struct linked_list
- {
- int data;
- struct linked_list *next;
- };
- typedef struct linked_list *node;
- node createnode()
- {
- node temp;
- temp=(node)malloc(sizeof(node));
- temp->next=NULL;
- return temp;
- }
- node add_node(node head,int value)
- {
- node temp=createnode(),p;
- temp->data=value;
- if(head==NULL)
- head=temp;
- else
- {
- p=head;
- while(p->next!=NULL)
- p=p->next;
- p->next=temp;
- }
- return head;
- }
- void print_node(node head)
- {
- node a;
- if(head==NULL)
- printf("empty\n");
- else
- {
- a=head;
- while(a!=NULL)
- {
- printf("%d ",a->data);
- a=a->next;
- }
- }
- printf("\n");
- }
- int main()
- {
- int n;
- node head;
- head=NULL;
- x:
- scanf("%d",&n);
- if(n==1)
- {
- int ndata;
- printf("new data\n");
- scanf("%d",&ndata);
- head=add_node(head,ndata);
- goto x;
- }
- else if(n==2)
- {
- print_node(head);
- goto x;
- }
- else if(n==0)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment