-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
113 lines (105 loc) · 2.26 KB
/
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
#include"stack.h"
#include<malloc.h>
void inits(istack *s){
s->p = calloc(1,sizeof(node));
s->p->next = NULL;
s->p->prev = NULL;
s->width = 0;
s->store = s->p;
return;
}
void ipushs(istack *s, int a){
s->p->val = a;
pntr temp;
temp = s->p;
s->p = calloc(1,sizeof(node));
s->p->prev = temp;
s->p->next = NULL;
temp->next = s->p;
s->width++;
return;
}
void inserts(istack *s, int a,int loc){
node* newnode = (node *)malloc(sizeof(node));
newnode->val = a;
node *temp = s->store;
if( loc > s->width + 1)
return;
else if(loc == 0){
newnode->next = s->store;
s->store->prev = newnode;
newnode->prev = NULL;
s->store = newnode;
s->width++;
return;
}
else if(loc == s->width + 1){
newnode->next = NULL;
s->p->next = newnode;
newnode->prev = s->p;
s->p = newnode;
s->width++;
return;
}
else if(loc > 0){
while(loc > 1){
temp = temp->next;
loc--;
}
newnode->next = temp->next;
newnode->next->prev = newnode;
newnode->prev = temp;
temp->next = newnode;
s->width++;
return;
}
}
int ipops(istack *s){
//head pointer is ahead of the last element in the stack
//printw("%d\t",s->width);
pntr temp;
temp = s->p;
s->width--;
s->p = s->p->prev;
//printw("%d\t",s->width);
s->p->next = NULL;
int num = s->p->val;
free(temp);
return num;
}
int ipoploc(istack *s,int x){
//head pointer is ahead of the last element in the stack
//printw("%d\t",s->width);
pntr temp = s->store;
int num;
int i;
if(x == 0){
s->store = s->store->next;
s->store->prev = NULL;
num = temp->val;
free(temp);
s->width--;
return num;
}
for(i = 0; i < x; i++)
temp = temp->next;
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
s->width--;
num = temp->val;
free(temp);
return num;
}
int isEmptys(istack *s){
if(s->p->prev == NULL)
return 1;
else
return 0;
}
int isFull(istack *s){
return 0;
}
int ipeeps(istack *s){
if(!isEmptys(s))
return s->p->prev->val;
}