-
Notifications
You must be signed in to change notification settings - Fork 0
/
Two-way-stack.c
159 lines (155 loc) · 4.41 KB
/
Two-way-stack.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
int value;
struct node *next;
};
bool isempty(struct node *);
void sort( struct node *first);
struct node *add_node( struct node *first,int n);
bool ispresent(struct node *first, int n);
struct node *Delete( struct node *first,int n);
void print( struct node *first);
void reverse(struct node *);
void instruction(void){
printf("\nEnter a to add elements in the stack.\n");
printf("Enter d to delete elements from the stack.\n");
printf("Enter i to see instructions.\n");
printf("Enter p to print sorted stack.\n");
printf("Enter r to print stack in reverse ( behaves like a queue ).\n");
printf("Enter c to clear the screen.\n");
printf("Enter q to quit.\n");
}
struct node *first=NULL;
struct node *lastnode=NULL;
int inlist=0;
int main(){
int i,n;
char c;
int del;
instruction();
for( ; ; ){
printf("\nEnter mode:");
c=getch();
//scanf("%c",&c); fflush(stdin);
switch (c){
case 'i':
instruction();
break;
case 'q':
exit(0);
case 'a':
printf("\nEnter value to add: ");
scanf("%d",&n);
inlist++;
first=add_node( first,n);
sort(first);
break;
case 'c':
system("cls");
break;
case 'd':
printf("\nEnter the element of list to delete: ");
scanf("%d",&del);
first =Delete(first,del);
break;
case 'r':
print(first);
break;
case 'p':
reverse(first);
break;
}
}
getchar();
return 0;
}
struct node *Delete( struct node *first,int n){
struct node *cur,*prev;
bool found;
char *status = malloc( sizeof( char ));
if(!isempty(first)){
if( !ispresent( first,n) ){
printf("\nElement %d not found!\n\n",n);
return first;
}
else
inlist--;
if ( first->value == n ){
cur=first;
first=first->next;
free(cur);
found=true;
printf("\nFound in first node. Deleted successfuly!\n\n");
return first;
}
else{
for( cur = first,prev=NULL;cur !=NULL,cur->value !=n; prev=cur,cur=cur->next);
if( cur != NULL && cur->value == n){
prev->next=cur->next;
printf("Deleted successfuly!\n\n");
free(cur);
found = true;
return first;
}
}
}
else
printf("\n\nList is empty!\n\n");
}
struct node *add_node( struct node *first,int n){
struct node * new_node,*cur,*prev=NULL;
new_node=malloc( sizeof( struct node));
if(new_node==NULL){
printf("Memory full!\n");
return;
}
new_node->value=n;
new_node->next=first;
first=new_node;
return first;
}
void print( struct node *first){
printf("\n\n\t\tBehaves like a QUEUE.\n\n\t");
for(;first !=NULL;first=first->next ){
printf(" %d ->",first->value);
}
printf(" NULL");
printf("\n\n");
}
void reverse(struct node *first){
int invalue[inlist];
int i;
for(i=0;first !=NULL;first=first->next , i++ ){
invalue[i]=first->value ;
}
printf("\n\n\t\tSTACK.\n\n\t");
for(i=inlist-1;i>=1;i--)
printf(" %d ->",invalue[i]);
printf(" %d -> NULL\n\n",invalue[0]);
}
void sort( struct node *first){
struct node *cur,*prev=NULL,*new_node;
new_node=malloc( sizeof( struct node));
for( cur=first->next,prev=first ; cur !=NULL;prev=cur,cur=cur->next){
if(prev->value > cur->value){
new_node->value=prev->value;
prev->value=cur->value;
cur->value=new_node->value;
}
}
//printf("\nList has being sorted!");
}
bool isempty(struct node *first){
return first == NULL;
}
bool ispresent( struct node *first, int n){
struct node *cur,*prev;
for( cur = first,prev=NULL;cur !=NULL; prev=cur,cur=cur->next){
if( cur->value ==n)
return true;
}
return false;
}