We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
329 DATA STRUCTURES THROUGH C
Bane 74
Simple link list program to insert and delete the nodes.
#include
#include
#include
struct node
t
int info ;
struct node *next ;
+a
‘typedef struct node NODE ;
NODE *start ;
void createemptylist (NODE *start)
di
‘start = NULL ;
+
void traversinorder (NODE *P)
{
while (P != NULL)
{
printf("%d \n", P > info) ;
P= P > next ;
s
I
void insertatbegin (int item)
{
NODE *P ;
P = (NODE *) malloc (sizeof (NODE)) ;
P > info = item ;
if (start == NULL)
P next = NULL ;
else
P + next = start ;
start =P;
+
void insert_at_end (int item)
i
NODE *P, *loc ;
P = (NODE *) malloc (sizeof (NODE)) ;
P > info = item ;
P > next = NULL ;
if (start == NULL)
start =P;
Een RRR Te ENE RAT ETDATA STRUCTURES THROUGH Cc
else
loc = start ;
while (loc + next != NULL)
loc = loc — next ;
loc - next = ptr ;
,
void dele_beg (void)
1
NODE *P ;
if (start == NULL)
return ;
else
{
P= start ;
start = start + next ;
printf(“No deleted is = %d", P + info) ;
free (P) ;
}
void dele_end( )
t
NODE #P, *loc ;
if (start
ULL)
{ return; }
else if ((start) > next == NULL)
{ P= start;
start = NULL ;
printf (“No deleted is = %d", P + info) ;
free (P) 5
+
else
{ loc = start ;
P= start > next j
while (P next = NULL)
{
loc =
p= Pert j
y
Joc + next = NULL i
free (P) i
321322
es rae
i
i
f
i
[
'
|
Sone See a
DATA STRUCTURES THROUGH C
void insert_spe (NODE *start, int item)
{
NODE *P, *loc ;
‘int temp, k ;
for (k= 0; loc = start ; k < temp ; k++)
{
Joc = loc + next ;
if (loc == NULL)
f c
printf (“node in the list at Jess than one\n") ;
return ;
}
}
P = (NODE *) malloc (sizeof (NODE)) ;
P > info = item ;
P next = loc + next ;
Joc + next = P ;
}
dele_spe( )
{
node *P, *temp ;
int i, loc ;
printf ("enter the position to delete\n") ;
scanf(“%d", & loc) ;
If(start == NULL)
{
printf(“empty list") ;
else
{
ptr = start ;
fori = 15 i < Toc ; i449
4
temp = ptr ;
Ptr = ptr > next ;
}
Print f("No deleted 1s = xd”, ptr _, aa
temp -+ next = ptr + next ; il
free(ptr) ;
,DATA STRUCTURES THROUGH 323
void main( ) a
; &
int choice, item, after ;
char ch ;
clrser()
createemptylist(start) ;
printf (“1. Insert element at begin \n") ;
printf ("2. Insert element at end position\n") ;
printf ("3. Insert specific the position\n")
printf ("4. Traverse the list in order\n”) ;
printf ("5. Delete from the begin\n”) ;
printf (6. Delete from the last\n") ;
: printf (7. Delete the element from the specific position\n") ;
printf (“8. eExit\n") ;
printf (“enter your choice\n") ;
scanf (“%d", &choice) ;
switch (choice)
{
case 1: printf (“Enter the item\n") ;
scanf("%d", &item) ;
insertatbegin (item) ;
break ;
case 2: printf (“enter the item\n") ;
scanf("%d", &item) ;
insert_at_end (item) ;
break ;
case 3: printf (“Enter the item\n") ;
scanf ("%d", &item) ;
‘insert_spe (start, item) ;
break ;
case 4: printf (“\ntraverse the list\n") ;
traversinorder (start)
break ;
case 5: printf (“Delete the item\n") j
dele_beg( ) ;
break ;
case 6; printf (“belete the element\n")
end (start) ;324
DATA STRUCTURES THROUGH C
case 7: printf ("Delete the element”) ;
dele_spe (start)
break ;
case 8 : return ;
}
Fflush(stdin) ;
printf (“do you want to continue\n”)
scanf("%e", &ch) ;
}
while ((ch = ty’) I] (ch = *y")) i
getch( ) ;
Output of the simple link list
1. Insert element at begin
2. Insert element at end position
3. Insert element at specific position
4. Traverse the list in order
5. Delete ftom the begin
6. Delete trom the last
7. Delete the element ftom the specific
8. Ext poster
enter your choice
1
Enter the tem
8
do you want to continue
y
1. Insert element at begin
2. Insert element at end position
3. Insert specific the position
4, Traverse the list in order
5, Delete from the begin
6. Delete from the last
7. Delete the element from the specitic postion
8. Ext
enter your choice
2
Enter the item
7
0 do you want to continue
y
1. ingot element at begin