-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.c
155 lines (144 loc) · 3.81 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
////////////////////////////////////////////////////////////////////////////////
// Main File: testcases
// This File: linkedlist.c
// Other Files: 537malloc.c 537malloc.h range_tree.c range_tree.h
// linkedlist.h
// Semester: CS 537 Fall 2018
//
// Author: Youmin Han
// Email: youmin.han@wisc.edu
// CS Login: youmin
//
// Author: Xianjie Zheng
// Email: xzheng97@wisc.edu
// CS Login: xianjie
//
/////////////////////////// OTHER SOURCES OF HELP //////////////////////////////
// fully acknowledge and credit all sources of help,
// other than Instructors and TAs.
//
// Persons: NULL
//
// Online sources: NULL
//
//////////////////////////// 80 columns wide ///////////////////////////////////
#include "linkedlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Function that adds a new node at the end of a linked list
*
* @param
* headref: a reference to the head of the linked likst
* newdata: new data that will be saved into the linked list
*/
void append(struct LinklistNode** headref, void* newdata)
{
struct LinklistNode* newnode = (struct LinklistNode*) malloc(sizeof(struct LinklistNode));
struct LinklistNode* last = *headref;
newnode->data = newdata;
newnode->next = NULL;
// case when there is no node in the linked list yet
if (*headref == NULL)
{
*headref = newnode;
return;
}
// otherwise, append a new node to the end of the linked list
while (last->next != NULL){
last = last->next;
}
last->next = newnode;
return;
}
/*
* Function that removes the last node in a linked list
*
* @param
* headref: a reference to the head of the linked likst
* newdata: new data that will be saved into the linked list
*/
void removelastnode (struct LinklistNode** headref)
{
struct LinklistNode* temp = *headref;
struct LinklistNode* t = NULL;
// case when there is no node in the linked list yet
if (temp == NULL)
{
return;
}
else if(temp->next == NULL) {
free(temp);
temp = NULL;
}
// otherwise, append a new node to the end of the linked list
else {
while (temp->next != NULL){
t = temp;
temp = temp->next;
}
free(t->next);
t->next = NULL;
}
}
/*
* Function that counts the size of a linked list
*
* @param
* headref: a reference to the head of the linked likst
*/
int getsize(struct LinklistNode** headref)
{
struct LinklistNode* cursor = *headref;
int counter = 0;
// traverse the linked list and count the size
while(cursor != NULL)
{
counter++;
cursor = cursor->next;
}
return counter;
}
/*
* Function that grab the data stored in a linked list with specific location
*
* @param
* headref: a reference to the head of the linked likst
* index: the location of the data in the linked list
*/
void* getnodedata(struct LinklistNode** headref,int index)
{
if(*headref == NULL)
return NULL;
struct LinklistNode *cursor = *headref;
for(int i=0; i<index; i++)
{
if(cursor->next == NULL){
return NULL;
}
cursor = cursor->next;
}
return (void *)cursor->data;
}
/*
* Function that grab a node with specific location in a linked list
*
* @param
* headref: a reference to the head of the linked likst
* index: the location of the node in the linked list
*/
struct LinklistNode* getnode(struct LinklistNode** headref,int index)
{
if(*headref == NULL)
return NULL;
struct LinklistNode *cursor = *headref;
for(int i=0; i<index; i++)
{
if(cursor->next == NULL){
return NULL;
}
cursor = cursor->next;
}
return cursor;
}