-
Notifications
You must be signed in to change notification settings - Fork 5
/
comdes
98 lines (88 loc) · 2.18 KB
/
comdes
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
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
using namespace std;
struct node
{
int num;
node *nextptr;
}*stnode; //node constructed
void listBanao(int n);
void reverse(node **stnode);
void listDhikhao();
int main()
{
int n,num,item;
cout<<"Enter the number of nodes: ";
cin>>n;
listBanao(n);
cout<<"\nLinked list data: \n";
listDhikhao();
cout<<"\nAfter reversing\n";
reverse(&stnode);
listDhikhao();
return 0;
}
void listBanao(int n) //function to create linked list.
{
struct node *frntNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
cout<<" Memory can not be allocated";
}
else
{
cout<<"Enter the data for node 1: ";
cin>>num;
stnode-> num = num;
stnode-> nextptr = NULL; //Links the address field to NULL
tmp = stnode;
for(i=2; i<=n; i++)
{
frntNode = (struct node *)malloc(sizeof(struct node));
if(frntNode == NULL) //If frntnode is null no memory cannot be allotted
{
cout<<"Memory can not be allocated";
break;
}
else
{
cout<<"Enter the data for node "<>num;
frntNode->num = num;
frntNode->nextptr = NULL;
tmp->nextptr = frntNode;
tmp = tmp->nextptr;
}
}
}
}
void reverse(node **stnode) //function to reverse linked list
{
struct node *temp = NULL;
struct node *prev = NULL;
struct node *current = (*stnode);
while(current != NULL) {
temp = current->nextptr;
current->nextptr = prev;
prev = current;
current = temp;
}
(*stnode) = prev;
}
void listDhikhao()//function to display linked list
{
struct node *tmp;
if(stnode == NULL)
{
cout<<"List is empty";
}
else
{
tmp = stnode;
while(tmp != NULL)
{
cout<num<<"\t";
tmp = tmp->nextptr;
}
}
}