You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//free(temp); //free function frees the heap location pointed to by its argument, essentially, no more of that piece of heap memory now available for use
53
-
//free(temp1);
54
29
55
-
/* created a 3 ele list so far*/
56
-
/* Now, reverse it and print it too*/
30
+
voidcreate (inthead_data){
31
+
32
+
if (head!=NULL){
33
+
printf ("List already exists. Consider using append(%d) or insert(%d) instead\n", head_data, head_data);
34
+
return;
35
+
}
36
+
head= (NODE*) malloc (sizeof(NODE));
37
+
head->data=head_data;
38
+
head->ptr=NULL;
39
+
tail=head;
40
+
41
+
print();
42
+
return;
43
+
}
57
44
58
-
//reversell (head);
59
45
60
-
/* Now, delete pth node*/
46
+
voidprint (void){
61
47
62
-
deletep(head, p);
63
-
printll (head);
48
+
NODE*iter=NULL;
49
+
iter=head;
50
+
if (iter==NULL){
51
+
printf ("Empty list. Nothing to print.\n");
52
+
return;
53
+
}
54
+
while (iter->ptr!=NULL){ // exits when temp points at Tail node
64
55
65
-
insertp (head, p, newdata);
66
-
printll (head);
56
+
printf ("%d --> ", iter->data);
57
+
iter=iter->ptr;
58
+
}
59
+
printf ("%d \n", iter->data); // BEAUTFCTN: to avoid printing an arrow after Tail node
60
+
return;
61
+
}
67
62
68
-
return0;
69
-
}
70
63
71
-
structNode*createll()
72
-
{
73
-
structNode*h=NULL;
74
-
structNode*temp=NULL;
75
-
intn=3; //for incrementing node data; first node starts with data = 3
64
+
voidappend (intNODE_data){
65
+
/*
66
+
if (head == NULL){
67
+
printf("Empty list. Call create() first\n");
68
+
return;
69
+
}
70
+
NODE* temp = (NODE*) malloc (sizeof(NODE));
71
+
temp->data = NODE_data;
72
+
temp->ptr = NULL;
73
+
tail->ptr = temp;
74
+
tail = temp;
75
+
return;
76
+
*/
76
77
77
-
h= (structNode*) malloc (sizeof(structNode));
78
-
h->data=n;
79
-
h->link=NULL;
80
-
81
-
temp= (structNode*) malloc (sizeof(structNode)); //allocated heap memory for second node
82
-
temp->data=++n;
83
-
temp->link=NULL;
84
-
h->link=temp;
85
-
86
-
temp= (structNode*) malloc (sizeof(structNode)); //allocated heap memory for third node
87
-
temp->data=++n;
88
-
temp->link=NULL;
89
-
(h->link)->link=temp;
90
-
91
-
temp= (structNode*) malloc (sizeof(structNode)); //allocated heap memory for fourth node
92
-
temp->data=++n;
93
-
temp->link=NULL;
94
-
((h->link)->link)->link=temp;
95
-
96
-
returnh;
97
-
}
78
+
/* ITCO absence of Tail ptr */
98
79
99
-
voidprintll (structNode*h)
100
-
{
101
-
structNode*temp=NULL;
102
-
for (temp=h; temp!=NULL; temp=temp->link)
103
-
{
104
-
printf("%d ", temp->data);
80
+
if (head==NULL){
81
+
printf("Empty list. Call create() first\n");
82
+
return;
105
83
}
106
-
printf ("\n");
107
-
}
84
+
NODE*temp= (NODE*) malloc (sizeof(NODE));
85
+
temp->data=NODE_data;
86
+
temp->ptr=NULL;
87
+
88
+
// Define iter NODE ptr to get to the end of LL
89
+
NODE*iter=head;
90
+
while (iter->ptr!=NULL){ // exits when iter points to Tail node
108
91
109
-
voidreversell (structNode*h)
110
-
{
111
-
structNode*prev=NULL;
112
-
structNode*curr=NULL;
113
-
structNode*next=NULL;
114
-
curr=h;
115
-
while (curr->link!=NULL)
116
-
{
117
-
next=curr->link;
118
-
curr->link=prev;
119
-
prev=curr;
120
-
curr=next;
121
-
122
-
/* Debugging stmnts:
123
-
printf("prev points to %d\n", prev->data);
124
-
printf("curr points to %d\n", curr->data);
125
-
printf("next points to %d\n\n", next->data); */
92
+
iter=iter->ptr;
126
93
}
127
-
curr->link=prev;
128
-
h=curr;
129
-
printll (h);
94
+
iter->ptr=temp;
95
+
96
+
print();
97
+
return;
130
98
}
131
99
132
-
voiddeletep (structNode*h, intP)
133
-
{
134
-
/*first check if there is a Pth node*/
135
-
structNode*temp=NULL;
136
-
structNode*prev=NULL;
137
-
structNode*next=NULL;
138
-
inti;
139
-
for (temp=h, i=0; temp!=NULL; temp=temp->link)
140
-
{
141
-
i++;
100
+
101
+
voidinsert (intnode_data, intpos){
102
+
103
+
104
+
if (0==pos){ // to cover the case of pos == 0
105
+
106
+
printf("Consider using prepend(%d) instead, but sure\n", node_data);
107
+
prepend(node_data);
108
+
109
+
return;
110
+
}
111
+
112
+
// else if // MODIFYLATER to cover pos > Tail node pos
113
+
114
+
NODE*newnode= (NODE*) malloc (sizeof(NODE));
115
+
newnode->data=node_data;
116
+
newnode->ptr=NULL; // to avoid indeterminacy
117
+
118
+
NODE*temp=head;
119
+
intcount=0;
120
+
121
+
while((temp->ptr!=NULL) && (count!= (pos-1))){ // Traverse till (pos-1)
122
+
temp=temp->ptr;
123
+
count++;
142
124
}
143
-
printf("i = %d\n", i);
144
-
if (i<P)
145
-
{
146
-
printf("there is no pth node\n");
125
+
126
+
if (count== (pos-1)){ // If reached the right index/pos
127
+
newnode->ptr=temp->ptr;
128
+
temp->ptr=newnode;
129
+
print();
130
+
131
+
return;
147
132
}
148
-
else
149
-
{
150
-
for (temp=h, i=0; i<P; i++, temp=temp->link)
151
-
{
152
-
if (i== (P-1))
153
-
{
154
-
prev=temp;
155
-
break;
156
-
}
157
-
}
158
-
temp=temp->link; //node to be deleted
159
-
next=temp->link;
160
-
prev->link=next;
161
-
free(temp);
133
+
134
+
elseif (temp->ptr==NULL){ // If reached the end of the LL
135
+
printf("No such index, thus appending \n");
136
+
append (node_data);
137
+
print();
138
+
139
+
return;
162
140
}
163
141
142
+
143
+
}
144
+
145
+
146
+
voidprepend (inthNODE_data){
164
147
148
+
NODE*newnode= (NODE*) malloc (sizeof(NODE));
149
+
newnode->data=hNODE_data;
150
+
newnode->ptr=head;
151
+
head=newnode;
152
+
153
+
print();
154
+
return;
165
155
}
166
156
167
-
voidinsertp (structNode*h, intp, intnodedata)
168
-
{
169
-
//check if there is a p-1th node at all
157
+
voidtakeout (intpos){
170
158
171
-
intcount=0;
172
-
structNode*temp=NULL;
173
-
structNode*newnode=NULL;
159
+
// if pos == 0 --> handle separately
160
+
// iter till pos-1
161
+
// iter->ptr = iter->ptr->ptr
162
+
// print taken out NODE data
163
+
// deallocate takenout NODE
164
+
// MODIFYLATER to cover pos == Tail NODE, as well as > Tail NODE
0 commit comments