-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
76 lines (66 loc) · 1.6 KB
/
linkedlist.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
// Assignment 1: Heapsort
// Brandon Vowell
// CS 201
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "node.h"
#include "dsnode.h"
#include "linkedlist.h"
static lldatastructure *createStructure(void);
lldatastructure *newStructure(void) {
lldatastructure *q = createStructure();
q->head = malloc(sizeof(dsnode *));
q->tail = malloc(sizeof(dsnode *));
q->head = NULL;
q->tail = NULL;
return q;
}
void enqueue(lldatastructure *q, node *nd) {
dsnode *val = assignDSNode(nd);
if(q->head == NULL) {
q->head = val;
q->tail = val;
}
else {
val->next = q->head;
q->head->previous = val;
q->head = val;
}
}
node *dequeue(lldatastructure *q) {
dsnode *valueToReturn = q->tail;
q->tail = q->tail->previous;
if(q->tail != NULL)
q->tail->next = NULL;
return valueToReturn->value;
}
void push(lldatastructure *stk, node *nd) {
dsnode *val = assignDSNode(nd);
if(stk->head == NULL) {
stk->head = val;
stk->tail = val;
}
else {
val->next= stk->head;
stk->head->previous = val;
stk->head = val;
}
return;
}
node *pop(lldatastructure *stk) {
dsnode *valueToReturn = stk->head;
stk->head = stk->head->next;
if(stk->head != NULL)
stk->head->previous = NULL;
return valueToReturn->value;
}
////////////////////////////////////////////////
lldatastructure *createStructure() {
lldatastructure *p = malloc(sizeof(lldatastructure));
if(p == 0) {
fprintf(stderr, "out of memory\n");
exit(-1);
}
return p;
}