forked from lugnitdgp/basic_datastructure_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_of_linkedlist.cpp
85 lines (77 loc) · 1.69 KB
/
reverse_of_linkedlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
#include<malloc.h>
//Declaring a node.
struct Node
{
int data; //data part of node.
struct Node *link; //pointer to next node.
};
struct Node *header; //First Node of list.(global)
//creation of linkedlist
void createlist(int n)
{
struct Node *temp=header;
temp=(struct Node*)malloc(sizeof(struct Node)); //allocating memory to first node.
if(temp==NULL)
printf("Overflow"); //if there is no space in memory
temp->link=NULL;
int i=1;
printf("Enter The Value Of Node%d: ",i);
scanf("%d",&temp->data);
if(header==NULL)
{
temp->link=NULL;
header=temp;
}
i=i+1;
while(i<=n)
{
struct Node* temp1=temp;
temp=(struct Node*)malloc(sizeof(struct Node)); //allocating memory to second node.
if(temp==NULL)
printf("Overflow");
printf("Enter The Value for Node%d: ",i);
scanf("%d",&temp->data);
temp1->link=temp;
temp->link=NULL;
i=i+1;
}
}
//display of linkedlist
void displaylist()
{
struct Node* temp=header;
printf("\nElements Of List:");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->link;
}
}
//function to reverselist
void reverse()
{
struct Node*prev=NULL;
struct Node* temp=header;
struct Node*next;
while(temp!=NULL)
{
next=temp->link;
temp->link=prev;
prev=temp;
temp=next;
}
header=prev;
printf("\n\nAfter Reversing\n ");
displaylist();
}
//Main Body
int main()
{
int num;
printf("Enter The Number Of Nodes For Linkedlist: ");
scanf("%d",&num);
createlist(num);//call to createlist
displaylist();//call to displaylist
reverse();//call to reverse list
}