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;
- }*head;
- void createlinklist(int n);
- void display();
- int main()
- {
- int n;
- printf("Enter the number of nodes: ");
- scanf("%d",&n);
- createlinklist(n);
- printf("\nData entered in the list are:\n");
- display();
- printf("Reverse list is:\n");
- reverseList();
- display();
- return 0;
- }
- void createlinklist(int n)
- {
- struct Node *p, *temp;
- int data, i;
- head= (struct Node*)malloc(sizeof(struct Node));
- printf("Input data for node 1: ");
- scanf("%d",&data);
- head->data=data;
- head->next=NULL;
- p=head;
- temp=head;
- for(i=2; i<=n; i++)
- {
- printf("Enter data for node %d: ",i);
- scanf("%d",&data);
- temp=(struct Node*)malloc(sizeof(struct Node));
- temp->data=data;
- temp->next=NULL;
- p->next=temp;
- p=p->next;
- }
- }
- void display()
- {
- struct Node *temp;
- temp=head;
- while(temp!=NULL)
- {
- printf("%d\n",temp->data);
- temp=temp->next;
- }
- }
- void reverseList()
- {
- struct Node *prev,*cur,*next;
- cur=head;
- prev=NULL;
- while(cur!=NULL)
- {
- next=cur->next;
- cur->next=prev;
- prev=cur;
- cur=next;
- }
- head=prev;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement