-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.c
More file actions
113 lines (87 loc) · 1.91 KB
/
Copy pathLinkedList.c
File metadata and controls
113 lines (87 loc) · 1.91 KB
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 "LinkedList.h"
#include <stdlib.h>
#include <stdio.h>
typedef struct listnode{
Element data;
struct listnode *next;
}ListNode;
struct linkedlist{
ListNode *head;
ListNode *tail;
};
//--helper
ListNode *newNode(Element data){
ListNode *new = (ListNode *)malloc(sizeof(ListNode));
new->next = NULL;
new->data = data;
return new;
}
//--end
LinkedList* initList(){
LinkedList * list = (LinkedList *)malloc(sizeof(LinkedList));
list->head = NULL;
return list;
}
void addlistHead(LinkedList *list, Element data){
ListNode *new = newNode(data);
new->next = list->head;
list->head = new;
if(list->tail == NULL)
list->tail = new;
return;
}
void addlistTail(LinkedList *list, Element data){
ListNode *new = newNode(data);
if(list->tail == NULL){
list->head = list->tail = new;
return;
}else{
list->tail->next = new;
list->tail = new;
return;
}
}
Element deletelistTop(LinkedList *list){
Element temp;
if(list->head == NULL){
temp.key = -1;
return temp;
}
temp = list->head->data;
ListNode *old = list->head;
list->head = old->next;
if(list->head == NULL)
list->tail = NULL;
free(old);
return temp;
}
Element getlistTop(LinkedList *list){
Element temp;
if(list->head == NULL){
temp.key = -1;
return temp;
}
temp = list->head->data;
return temp;
}
void destroylist(LinkedList *list){
ListNode *now = list->head;
while(now != NULL){
ListNode *temp = now;
now = now->next;
free(temp);
}
free(list);
return;
}
void printList(LinkedList *list){
ListNode *now = list->head;
while(now != NULL){
printf("%d ", now->data.key);
now = now->next;
}
printf("\n");
}
bool listEmpty(LinkedList *list){
return list->head == NULL;
}