-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapstack.c
92 lines (77 loc) · 1.52 KB
/
heapstack.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
#include <stdio.h>
#include <stdarg.h>
#include "clinked.c"
#define ENDLIST (const char *)NULL
void swapnodes(struct node* c1, struct node* c2) {
void *t = c1->value;
c1->value = c2->value;
c2->value = t;
}
void movetoend(struct node* l) {
for(;;) {
if (l->next == NULL) break;
swapnodes(l, l->next);
l=l->next;
}
}
int countlist(struct node* l) {
int c = 1;
for (struct node* i=l;i->next!=NULL;i=i->next) {
c++;
}
return c;
}
struct node* getlast(struct node *cursor) {
for (;;) {
if (cursor->next==NULL) break;
else cursor = cursor->next;
}
return cursor;
}
void reverselist(struct node* l) {
int c = countlist(l);
struct node* j = getlast(l);
for (int i=1; i<=c; i++) {
if (j->prev == NULL) {
break;
}
movetoend(j->prev);
j=j->prev;
}
}
struct node* push(struct node* list, char* val) {
if (list==NULL) {
struct node *head = makenode(NULL, NULL, val);
return head;
} else {
struct node* t = makenode(NULL, list, val);
list->prev=t;
list=t;
return list;
}
}
struct node* pop(struct node *list) {
struct node* next = list->next;
next->prev = NULL;
freeitem(list);
return next;
}
struct node* newlist(char *firstarg, ...) {
va_list args;
va_start(args, firstarg);
struct node* list = processlist(1,&firstarg);
char* nextarg = va_arg(args, char*);
for (;;) {
if (nextarg==NULL) break;
else {
list=push(list,nextarg);
nextarg=va_arg(args, char*);
}
}
va_end(args);
reverselist(list);
return list;
}
char *getvalue(struct node *list) {
return list->value;
}