Skip to content

Commit 503bf71

Browse files
committed
modified llst.c and added llst.h
1 parent 4df057e commit 503bf71

File tree

2 files changed

+210
-166
lines changed

2 files changed

+210
-166
lines changed

llst.c

Lines changed: 180 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,200 +1,214 @@
11
#include <stdio.h>
2-
#include <stdlib.h> //malloc
3-
4-
struct Node
5-
{
6-
int data;
7-
struct Node* link;
8-
};
9-
10-
struct Node* createll ();
11-
void printll (struct Node* h);
12-
void reversell (struct Node* h);
13-
void deletep (struct Node* h, int P);
14-
void insertp (struct Node*, int, int);
15-
16-
int main()
17-
{
18-
struct Node* head = NULL; //declared and initialized pointer to head node
19-
20-
//now, start inserting nodes to this ll
2+
#include "llst.h"
3+
4+
/* For visualization: 4 --> 10 --> 7 --> 5 --> 16 */
5+
6+
int main(){
7+
8+
int hdata;
9+
printf ("Enter desired head-node data\n");
10+
scanf("%d", &hdata);
11+
12+
create (hdata);
2113

14+
append(9);
15+
create (99);
2216

23-
int p = 3; //position to delete node
24-
int newdata = 55;
25-
struct Node* temp = NULL; //declared and initialized temp node pointer to add new nodes
17+
insert(8, 0);
2618

27-
head = createll();
28-
/*
29-
head = (struct Node*) malloc (sizeof(struct Node)); //allocated heap memory for head node
30-
head->data = n;
31-
head->link = NULL;
32-
33-
temp = (struct Node*) malloc (sizeof(struct Node)); //allocated heap memory for second node
34-
temp->data = ++n;
35-
temp->link = NULL;
36-
head->link = temp;
37-
38-
temp = (struct Node*) malloc (sizeof(struct Node)); //allocated heap memory for third node
39-
temp->data = ++n;
40-
temp->link = NULL;
41-
(head->link)->link = temp;
42-
43-
temp = (struct Node*) malloc (sizeof(struct Node)); //allocated heap memory for fourth node
44-
temp->data = ++n;
45-
temp->link = NULL;
46-
((head->link)->link)->link = temp;
19+
prepend(7);
20+
21+
insert (11, 3);
4722

48-
*/
23+
takeout(0);
24+
append(7);
4925

50-
printll (head);
26+
return 0;
27+
}
5128

52-
//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);
5429

55-
/* created a 3 ele list so far*/
56-
/* Now, reverse it and print it too*/
30+
void create (int head_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+
}
5744

58-
//reversell (head);
5945

60-
/* Now, delete pth node*/
46+
void print (void){
6147

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
6455

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+
}
6762

68-
return 0;
69-
}
7063

71-
struct Node* createll()
72-
{
73-
struct Node* h = NULL;
74-
struct Node* temp = NULL;
75-
int n = 3; //for incrementing node data; first node starts with data = 3
64+
void append (int NODE_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+
*/
7677

77-
h = (struct Node*) malloc (sizeof(struct Node));
78-
h->data = n;
79-
h->link = NULL;
80-
81-
temp = (struct Node*) malloc (sizeof(struct Node)); //allocated heap memory for second node
82-
temp->data = ++n;
83-
temp->link = NULL;
84-
h->link = temp;
85-
86-
temp = (struct Node*) malloc (sizeof(struct Node)); //allocated heap memory for third node
87-
temp->data = ++n;
88-
temp->link = NULL;
89-
(h->link)->link = temp;
90-
91-
temp = (struct Node*) malloc (sizeof(struct Node)); //allocated heap memory for fourth node
92-
temp->data = ++n;
93-
temp->link = NULL;
94-
((h->link)->link)->link = temp;
95-
96-
return h;
97-
}
78+
/* ITCO absence of Tail ptr */
9879

99-
void printll (struct Node* h)
100-
{
101-
struct Node* 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;
10583
}
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
10891

109-
void reversell (struct Node* h)
110-
{
111-
struct Node* prev = NULL;
112-
struct Node* curr = NULL;
113-
struct Node* 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;
12693
}
127-
curr->link = prev;
128-
h = curr;
129-
printll (h);
94+
iter->ptr = temp;
95+
96+
print();
97+
return;
13098
}
13199

132-
void deletep (struct Node* h, int P)
133-
{
134-
/*first check if there is a Pth node*/
135-
struct Node* temp = NULL;
136-
struct Node* prev = NULL;
137-
struct Node* next = NULL;
138-
int i;
139-
for (temp = h, i = 0; temp != NULL; temp = temp->link)
140-
{
141-
i++;
100+
101+
void insert (int node_data, int pos){
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+
int count = 0;
120+
121+
while((temp->ptr != NULL) && (count != (pos-1))){ // Traverse till (pos-1)
122+
temp = temp->ptr;
123+
count++;
142124
}
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;
147132
}
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+
else if (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;
162140
}
163141

142+
143+
}
144+
145+
146+
void prepend (int hNODE_data){
164147

148+
NODE* newnode = (NODE*) malloc (sizeof(NODE));
149+
newnode->data = hNODE_data;
150+
newnode->ptr = head;
151+
head = newnode;
152+
153+
print();
154+
return;
165155
}
166156

167-
void insertp (struct Node* h, int p, int nodedata)
168-
{
169-
//check if there is a p-1th node at all
157+
void takeout (int pos){
170158

171-
int count = 0;
172-
struct Node* temp = NULL;
173-
struct Node* 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
174165

175-
for (temp = h; temp != NULL; temp = temp->link)
176-
{
177-
count++;
178-
}
179-
printf("intermediateo/p count = %d\n", count);
166+
if (0 == pos){
167+
168+
NODE* temp = head;
169+
head = head->ptr;
170+
printf ("Takenout NODE data = %d\n", temp->data);
171+
free(temp);
180172

181-
if(count < (p-1)) //no p-1 th node at all
182-
{
183-
printf("ll has < p-1 nodes\n");
173+
print();
184174
return;
185175
}
186-
else //yes p-1 th node
187-
{
188-
newnode = (struct Node*) malloc (sizeof(struct Node));
189-
newnode->data = nodedata;
190-
newnode->link = NULL;
191-
192-
int i;
193-
for (i = 1, temp = h; i<p-1; temp = temp->link, i++)
194-
{
195-
196-
}
197-
newnode->link = temp->link;
198-
temp->link = newnode;
176+
NODE* iter = head;
177+
int count = 0;
178+
179+
NODE* temp = NULL;
180+
while (count != (pos-1)){ // exits when iter is at pos-1
181+
182+
iter = iter->ptr;
183+
count++;
184+
}
185+
temp = iter->ptr;
186+
iter->ptr = temp->ptr;
187+
printf ("Takenout NODE data = %d\n", temp->data);
188+
free(temp);
189+
190+
print();
191+
return;
192+
}
193+
194+
195+
void reverse (void){
196+
197+
NODE* iterp = NULL;
198+
NODE* iter = NULL;
199+
NODE* iterf = NULL;
200+
201+
iter = head;
202+
while (iter->ptr != NULL){
203+
204+
iterf = iter->ptr;
205+
iter->ptr = iterp;
206+
iterp = iter;
207+
iter = iterf;
199208
}
200-
}
209+
head = iter;
210+
head->ptr = iterp;
211+
212+
print();
213+
return;
214+
}

0 commit comments

Comments
 (0)