posted Mar 12, 2011, 7:32 AM by Neil Mathew
[
updated Mar 12, 2011, 7:37 AM
]
SOURCE CODE:
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
| #include<stdio.h>
int count; // Total number of nodes in Singly Linked List.
struct node
{
int info;
struct node * next;
}*start,*ptr;
int INS(int ITEM, int pos)
{
struct node * newnode;
newnode= (struct node *) malloc ( sizeof(struct node) ) ;
newnode->info=ITEM;
newnode->next=NULL;
// ERROR HANDLING #1 - No Memory Left (Overflow)
if( newnode == NULL )
{
printf(" OVERFLOW: No Memory Left to create newnode. ");
return 0;
}
// ERROR HANDLING #2 - Position Over Limits
if( pos<=0 || pos>count+1 )
{
printf(" Position entered is over limits. ");
return 0;
}
// Condition 1: First node of LL
if( start==NULL )
{
start=newnode;
count=1;
}
// Condition 2: Adding node to first position of LL
else if( pos==1)
{
newnode->next=start;
start=newnode;
count++;
}
// Condition 3: Adding node to any other position of LL
else
{
int i;
ptr=start;
for(i=2; i<=pos-1; i++)
ptr=ptr->next;
newnode->next = ptr->next;
ptr->next = newnode;
count++;
}
return 1; //successful
}
int DEL(int pos)
{
struct node *temp;
//ERROR HANDLING #1 - Underflow
if(start == NULL)
{
printf(" UNDERFLOW: No Node left to delete. ");
return 0;
}
//ERROR HANDLING #2 - Position Over Limits
if( pos<=0 || pos>count )
{
printf(" Position entered is over limits. ");
return 0;
}
// Condition 1: Deleting First Node OR Last Remaining Node
if( pos==1 )
{
temp=start;
start=start->next;
free(temp);
count--;
}
// Condition 2: Deleting any other node
else
{
int i;
ptr=start;
for(i=2; i<=pos-1; i++)
ptr=ptr->next;
temp=ptr->next;
ptr->next=temp->next;
free(temp);
count--;
}
return 1;
}
int TRAVERSE()
{
ptr=start;
// ERROR HANDLING
if(start==NULL)
{
printf(" Linked List Empty. No Nodes to display. ");
return 0;
}
// TRAVERSING
int i;
printf("\n [");
for(i=1; i<=count; i++, ptr=ptr->next)
{
printf(" - %d",ptr->info);
}
printf(" - ]");
return 1;
}
int main()
{
int item,pos=1;
int ch,success;
start=NULL;
count=0;
do
{
printf("\n\n MENU:");
printf("\n 1. Insert a node. ");
printf("\n 2. Delete a node. ");
printf("\n 3. Display the Singly Linked List. ");
printf("\n 4. EXIT. \n Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: //INSERT
printf("\n ENTER ITEM: ");
scanf("%d", &item);
if(start!=NULL)
{
printf("\n ENTER POSITION: ");
scanf("%d", &pos);
}
success=INS(item,pos);
break;
case 2: //DELETE
if(start!=NULL)
{
printf("\n ENTER POSITION: ");
scanf("%d", &pos);
}
success=DEL(pos);
break;
case 3: //DISPLAY
printf("\n The Nodes: \n");
success=TRAVERSE();
break;
case 4:
printf("\n Exiting.. ");
break;
default: printf(" INVALID CHOICE. ");
}
}
while(ch!=4); //end of do while loop
return 0;
}
|
OUTPUT:
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 1
ENTER ITEM: 34
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 1
ENTER ITEM: 56
ENTER POSITION: 2
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 1
ENTER ITEM: 67
ENTER POSITION: 3
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 3
The Nodes:
[ - 34 - 56 - 67 - ]
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 1
ENTER ITEM: 12
ENTER POSITION: 1
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 3
The Nodes:
[ - 12 - 34 - 56 - 67 - ]
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 1
ENTER ITEM: 0
ENTER POSITION: 6
Position entered is over limits.
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 2
ENTER POSITION: 3
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 3
The Nodes:
[ - 12 - 34 - 67 - ]
MENU:
1. Insert a node.
2. Delete a node.
3. Display the Singly Linked List.
4. EXIT.
Your Choice: 4
Exiting..
|
|