-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
178 lines (155 loc) · 5.11 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Name:- Aashutosh Dahal
CS 241, Lab08
linkedlist.c is the program that creates a linkedlist and perform a basic
operation like insertSorted list, removeItem from linkedlist, print, push, pop...
*/
#include <stdlib.h>
#include <stdio.h>
#include "linkedlist.h"
/* Alloc a new node with given data. */
struct ListNode* createNode(char data)
{
struct ListNode* node = malloc(sizeof(struct ListNode));
node->data = data;
node->next = NULL;
return node;
}
/* Insert data at appropriate place in a sorted list, return new list head. */
struct ListNode* insertSorted(struct ListNode* head, char data)
{
struct ListNode* newNode = createNode(data);
newNode->next = head;
if (head == NULL)
{
head = newNode;
}
else if (data < head->data)
{
/* new head of the node */
newNode->next = head;
head = newNode;
}
else
{
/* going through linked list to find the appropriate location to sort */
struct ListNode* currNode = head;
while(currNode->next != NULL && currNode->next->data < data)
{
currNode = currNode->next;
}
/* inserting the new node */
newNode->next = currNode->next;
currNode->next = newNode;
}
return head;
}
/* Remove data from list pointed to by headRef, changing head if necessary.
* Make no assumptions as to whether the list is sorted.
* Memory for removed node should be freed.
* Return 1 if data was present, 0 if not found. */
int removeItem(struct ListNode** headRef, char data)
{
struct ListNode* currHead;
if (*headRef == NULL)
{
return 0;
}
/* head node is the one to be removed */
if ((*headRef)->data == data)
{
struct ListNode* tempList = *headRef;
*headRef = (*headRef)->next;
free(tempList);
return 1;
}
/* going through the list to remove */
currHead = *headRef;
while(currHead->next != NULL && currHead->next->data != data)
{
currHead = currHead->next;
}
/* removing node */
if (currHead->next != NULL)
{
struct ListNode* tempList = currHead->next;
currHead->next = currHead->next->next;
free(tempList);
return 1;
}
/* nothing to remove */
return 0;
}
/* Treat list as a stack. (LIFO - last in, first out)
* Insert data at head of list, return new list head. */
struct ListNode* pushStack(struct ListNode* head, char data)
{
struct ListNode* newNode = malloc(sizeof(struct ListNode));
newNode->data = data;
newNode->next = head;
return newNode;
}
/* Treat list as a stack. (LIFO - last in, first out)
* Remove and return data from head of non-empty list, changing head.
* Memory for removed node should be freed. */
char popStack(struct ListNode** headRef)
{
struct ListNode* tempList = *headRef;
char data = tempList->data;
*headRef = tempList->next;
free(tempList);
return data;
}
/* Return length of the list. */
int listLength(struct ListNode* head)
{
struct ListNode* currList = head;
int counter = 0; /* counter for length */
while(currList != NULL)
{
counter ++;
currList = currList->next;
}
return counter;
}
/* Print list data on single line, separating values with a comma and
* a space and ending with newline. */
void printList(struct ListNode* head)
{
struct ListNode* currHead = head;
while(currHead != NULL)
{
printf("%c", currHead->data);
if(currHead->next!=NULL)
{
printf(", ");
}
currHead = currHead->next;
}
printf("\n");
}
/* Free memory used by the list. */
void freeList(struct ListNode* head)
{
while (head != NULL)
{
struct ListNode* tempList = head;
head = head->next;
free(tempList);
}
}
/* Reverse order of elements in the list */
void reverseList(struct ListNode** headRef)
{
struct ListNode* before = NULL;
struct ListNode* current = *headRef;
struct ListNode* after = NULL;
while (current != NULL)
{
after = current->next;
current->next = before;
before = current;
current = after;
}
*headRef = before;
}