-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllist.c
139 lines (131 loc) · 3.79 KB
/
llist.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int value;
struct node *next;
} node_t;
node_t *head = NULL;
/* Iterating over list */
void printLinkedList(node_t *head)
{
printf("\nstart of function printLinkedList \n");
node_t *current = head;
while (current != NULL)
{
printf("\n %d", (current)->value);
current = current->next;
}
printf("\nend of function printLinkedList \n");
}
void push(node_t **head, int data)
{
printf("\n pushing %d to list", data);
node_t *current = *head;
while (current->next != NULL)
{
current = current->next;
}
current->next = (node_t *)malloc(sizeof(node_t));
current->next->value = data;
current->next->next = NULL;
}
void addToStart(node_t **head, int data)
{
printf("Adding %d to start of list", data);
// allocate new memory
node_t *newAddress = (node_t *)malloc(sizeof(node_t));
newAddress->value = data;
// make newAddress's next point to head
newAddress->next = *head;
// put newAddress in head
*head = newAddress;
}
int removeFirst(node_t **head)
{
printf("removing first item from list = %d", (*head)->value);
int return_value = (*head)->value;
(*head) = (*head)->next;
return return_value;
}
int remove_last(node_t **head)
{
printf("\nremoving last item from list\n");
/*
pseudo code: how we will remove the last, in order to remove the last, we will have to traverse to the last node, after traversing to the last node, the ret_value will be value of last node and last_node->next is null, we have to keep reference of a node previous to it, and make the next of the previous node to equal null and
that means first we have to write the while loop to go to end
*/
/* boundary condition if the current item is the only item, we return it and set head to point to null */
int ret_value;
if ((*head)->next == NULL)
{
ret_value = (*head)->value;
free(*head);
return ret_value;
}
node_t *current = *head;
while (current->next->next != NULL)
{
current = current->next;
// when this while loop ends, current next will be null, this this the time, we have to first set the ret_value
}
ret_value = current->next->value;
free(current->next);
return ret_value;
}
int remove_nthItemByIndex(node_t **head, int n)
{
/* pseudo code
we want to remove nth Item, in order to remove nth item, we have to reach nth item first, then
it"s value should be returned and whatever it is pointing to , should be stored in it"s previous thing
cases
case 1: length of list is less than n;
then we cannot return anything, we have to throw error.
case 2:
*/
int current_index = -1;
int return_value;
if (*head != NULL && n == 0)
{
current_index += 1;
free(*head);
return_value = (*head)->value;
return return_value;
}
node_t *current = *head;
while (current->next != NULL)
{ /* while there is next node available, we increment the index,
if this is the index to be removed, then we get the node value and return that node value from the function, and mark current->next = current -> next -> next(with this we have dropped the next element reference)
+ we have to create a temp variable, which will store initial current -> next, then we can call free on this temporary variable
else
current should be = current-> next, i.e. we should repeat same on current->next
*/
current_index++;
if (n == current_index)
{
return_value = current->next->value;
current->next = current->next->next;
}
}
}
int main()
{
head = (node_t *)malloc(sizeof(node_t));
head->value = 1;
head->next = (node_t *)malloc(sizeof(node_t));
head->next->value = 2;
head->next->next = NULL;
printLinkedList(head);
push(&head, 3);
push(&head, 4);
push(&head, 5);
push(&head, 6);
printLinkedList(head);
addToStart(&head, 0);
printLinkedList(head);
removeFirst(&head);
printLinkedList(head);
printf(remove_last(&head));
printLinkedList(head);
return 0;
}