Advertisement
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;
- }
- node add_node_front(node head,int value)
- {
- node p=createnode();
- p->data=value;
- p->next=head;
- head=p;
- 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");
- }
- node print_node_rev(node head)
- {
- node prev,next,current;
- current=head;
- prev=NULL;
- while(current!=NULL)
- {
- next=current->next;
- current->next=prev;
- prev=current;
- current=next;
- }
- head=prev;
- return head;
- }
- int main()
- {
- printf("enter 1 to add node at thge end\nenter 2 to add node at the front\nenter 3 to print\nenter 4 to print rev\n");
- 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==3)
- {
- print_node(head);
- goto x;
- }
- else if(n==2)
- {
- int mdata;
- printf("new data\n");
- scanf("%d",&mdata);
- head=add_node_front(head,mdata);
- goto x;
- }
- else if(n==4)
- {
- head=print_node_rev(head);
- print_node(head);
- goto x;
- }
- else if(n==0)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement