Skip to content

Commit 5f1c522

Browse files
committed
commit llst.c
1 parent a298692 commit 5f1c522

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

llst.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#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
21+
22+
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
26+
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;
47+
48+
*/
49+
50+
printll (head);
51+
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);
54+
55+
/* created a 3 ele list so far*/
56+
/* Now, reverse it and print it too*/
57+
58+
//reversell (head);
59+
60+
/* Now, delete pth node*/
61+
62+
deletep(head, p);
63+
printll (head);
64+
65+
insertp (head, p, newdata);
66+
printll (head);
67+
68+
return 0;
69+
}
70+
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
76+
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+
}
98+
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);
105+
}
106+
printf ("\n");
107+
}
108+
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); */
126+
}
127+
curr->link = prev;
128+
h = curr;
129+
printll (h);
130+
}
131+
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++;
142+
}
143+
printf("i = %d\n", i);
144+
if (i < P)
145+
{
146+
printf("there is no pth node\n");
147+
}
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);
162+
}
163+
164+
165+
}
166+
167+
void insertp (struct Node* h, int p, int nodedata)
168+
{
169+
//check if there is a p-1th node at all
170+
171+
int count = 0;
172+
struct Node* temp = NULL;
173+
struct Node* newnode = NULL;
174+
175+
for (temp = h; temp != NULL; temp = temp->link)
176+
{
177+
count++;
178+
}
179+
printf("intermediateo/p count = %d\n", count);
180+
181+
if(count < (p-1)) //no p-1 th node at all
182+
{
183+
printf("ll has < p-1 nodes\n");
184+
return;
185+
}
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;
199+
}
200+
}

0 commit comments

Comments
 (0)