-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked list reversal using stack.cpp
117 lines (115 loc) · 1.93 KB
/
linked list reversal using stack.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<stack>
#include<stdio.h>
#include<stdlib.h>
struct node{
int d;
struct node* next;
};
typedef struct node nd;
struct snode{
nd* ndptr;
struct snode* next;
};
typedef struct snode sd;
void push(snode **top,nd* val)
{
sd* newnode=(sd*)malloc(sizeof(sd));
newnode->ndptr=val;
newnode->next=(*top);
(*top)=newnode;
}
void pop(snode **top)
{ if((*top)==NULL){printf("Stack is empty\n");
return;
}
sd* temp=(*top);
(*top)=(*top)->next;
free(temp);
}
bool empty(sd* top)
{if(top==NULL)
return true;
else return false;
}
nd* tops(sd* top)
{
if(top==NULL)
{printf("stack is empty");
return NULL;
}
return (top->ndptr);
}
void insert(nd **ptr,int ndata)
{ nd* newnode=(nd*)(malloc(sizeof(nd)));
newnode->d=ndata;
newnode->next=NULL;
if (*ptr==NULL)
{ *ptr=newnode;
}
else
{nd* temp=*ptr;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
}
void deletenode(nd **ptr,int ndata)
{ nd* temp=*ptr;
nd* prev;
if(temp==NULL){printf("error\n");return;
}
if(temp->d==ndata){
*ptr=temp->next;
free(temp);
return;
}
while(temp->d!=ndata&&temp!=NULL)
{prev=temp;
temp=temp->next;
}
if(temp==NULL)
return;
prev->next=temp->next;
free(temp);
}
void printlist(nd* mov)
{
if(mov==NULL){
printf("empty list \n");
return;
}
while(mov)
{
printf("%d\n",mov->d);
mov=mov->next;
}
}
void reverse(nd** head)
{nd* temp=(*head);
sd* top=NULL;
while(temp!=NULL)
{push(&top,temp);
temp=temp->next;
}
temp=tops(top);
(*head)=temp;
pop(&top);
while(!empty(top)){
temp->next=tops(top);
pop(&top);
temp=temp->next;
}
temp->next=NULL;
}
int main()
{
nd* head=NULL;
insert(&head,1);
insert(&head,3);
insert(&head,12);
insert(&head,8);
printlist(head);
reverse(&head);
printf("after reversing linked list\n");
printlist(head);
}