1: /*
2: ==============================================================
3: Name : Singly Linked List
4: Author : Mujahid, Shivkumar
5: Version :
6: Copyright : Your copyright notice
7: Description : Hello World in C, Ansi-style
8: ===========================================================
9: */
10: #include<stdio.h>
11: #include<stdlib.h>
12: struct node
13: { int data;
14: struct node *link;
15: };
16: void display(struct node *);
17: void add(struct node **,int);
18: void addn(struct node **,int,int);
19: void delete(struct node**,int);
20: void reverse(struct node *);
21: struct node *rreverse(struct node *head);
22: int main(void)
23: { struct node *p=NULL;
24: int n,n1,n2;
25: do
26: { printf("\n1.Add element\n2.Add at a particular location\n3.delete an element\n4.print the list\n5.print list reverse\n6.print reverse(recursion)\nenter your choice");
27: scanf("%d",&n);
28: switch(n)
29: { case 1:
30: { printf("\nenter element");
31: scanf("%d",&n1);
32: add(&p,n1);
33: }break;
34: case 2:
35: { printf("\nenter element");
36: scanf("%d",&n1);
37: printf("\nat what location do you want to add?");
38: scanf("%d",&n2);
39: addn(&p,n1,n2);
40: }break;
41: case 3:
42: { printf("\nenter element");
43: scanf("%d",&n1);
44: delete(&p,n1);
45: }break;
46: case 4:
47: { display(p);
48: }break;
49: case 5:
50: reverse(p);
51: break;
52: case 6:
53: { p=rreverse(p);
54: display(p);
55: }break;
56: default:
57: printf("\nenter valid choice");
58: }
59: printf("\nDo you wish to continue?1.yes\t0.No");
60: scanf("%d",&n);
61: }while(n);
62: printf("\nthats it. Thank you!");
63: return 0;
64: }
65: void display(struct node *q)
66: { struct node *temp;
67: temp=(struct node *)malloc(sizeof(struct node));
68: temp=q;
69: while(temp->link!=NULL)
70: { printf("\n %d",temp->data);
71: temp=temp->link;
72: }
73: printf("\n %d",temp->data);
74: }
75: void add(struct node **q,int n)
76: { struct node *temp,*r;
77: temp=(struct node *)malloc(sizeof(struct node));
78: r=(struct node *)malloc(sizeof(struct node));
79: if(*q==NULL)
80: { temp->data=n;
81: temp->link=NULL;
82: *q=temp;
83: printf("\nAdded %d as the first element",n);
84: return;
85: }
86: else
87: { temp=*q;
88: while(temp->link!=NULL)
89: temp=temp->link;
90: r->data=n;
91: r->link=NULL;
92: temp->link=r;
93: ;
94: printf("\nAdded %d",n);
95: return;
96: }
97: }
98: void addn(struct node **q,int n,int loc)
99: { struct node *temp=*q,*r;
100: r=(struct node *)malloc(sizeof(struct node));
101: int i;
102: for(i=1;i<loc-1;i++)
103: temp=temp->link;
104: r->data=n;
105: r->link=temp->link;
106: temp->link=r;
107: }
108: void delete(struct node **q,int num)
109: { struct node *temp=*q,*r;
110: r=(struct node *)malloc(sizeof(struct node));
111: while(temp!=NULL)
112: { if(temp->data==num)
113: { if(temp==*q)
114: { *q=temp->link;
115: return;
116: }
117: else
118: { r->link=temp->link;
119: return;
120: }
121: }
122: else
123: { r=temp;
124: temp=temp->link;
125: }
126: }
127: printf("\n%d not found",num);
128: }
129: void reverse(struct node *q)
130: { struct node *temp=q,*r=NULL,*s;
131: while(temp!=NULL)
132: { s=r;
133: r=temp;
134: temp=temp->link;
135: r->link=s;
136: }
137: temp=r;
138: while(temp!=NULL)
139: { printf("\n%d",temp->data);
140: temp=temp->link;
141: }
142: return;
143: }
144: struct node *rreverse(struct node *head)
145: { if (head==NULL || head->link==NULL)
146: return head;
147: struct node *nhead=rreverse(head->link);
148: head->link->link=head;
149: head->link=NULL;
150: return nhead;
151: }
152: -------------OUTPUT---------------
153: 1.Add element
154: 2.Add at a particular location
155: 3.delete an element
156: 4.print the list
157: 5.print list reverse
158: 6.print reverse(recursion)
159: enter your choice1
160: enter element5
161: Added 5 as the first element
162: Do you wish to continue?1.yes 0.No1
163: 1.Add element
164: 2.Add at a particular location
165: 3.delete an element
166: 4.print the list
167: 5.print list reverse
168: 6.print reverse(recursion)
169: enter your choice1
170: enter element10
171: Added 10
172: Do you wish to continue?1.yes 0.No1
173: 1.Add element
174: 2.Add at a particular location
175: 3.delete an element
176: 4.print the list
177: 5.print list reverse
178: 6.print reverse(recursion)
179: enter your choice2
180: enter element55
181: at what location do you want to add?3
182: Do you wish to continue?1.yes 0.No1
183: 1.Add element
184: 2.Add at a particular location
185: 3.delete an element
186: 4.print the list
187: 5.print list reverse
188: 6.print reverse(recursion)
189: enter your choice4
190: 5
191: 10
192: 55
193: Do you wish to continue?1.yes 0.No1
194: 1.Add element
195: 2.Add at a particular location
196: 3.delete an element
197: 4.print the list
198: 5.print list reverse
199: 6.print reverse(recursion)
200: enter your choice5
201: 55
202: 10
203: 5
204: Do you wish to continue?1.yes 0.No1
205: 1.Add element
206: 2.Add at a particular location
207: 3.delete an element
208: 4.print the list
209: 5.print list reverse
210: 6.print reverse(recursion)
211: enter your choice6
212: 5
213: Do you wish to continue?1.yes 0.No1
214: 1.Add element
215: 2.Add at a particular location
216: 3.delete an element
217: 4.print the list
218: 5.print list reverse
219: 6.print reverse(recursion)
220: enter your choice3
221: enter element55
222: 55 not found
223: Do you wish to continue?1.yes 0.No0
224: thats it. Thank you!
No comments:
Post a Comment