Data structure singly linked list programs VTU Exams
1. 1|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
/* Lab10 */
/* Write a c program using dynamic variables and pointers to
construct a stack of integers using singly linked list &
to perform the following operations:
1.Push(Insert front)
2.Pop(Delete front)
3.Display
The program should print approproate messages for stack
overflow and stack empty. */
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
void ins_first(NODE**, int);
int del_first(NODE**);
void display(NODE*);
int main()
{
NODE *first=NULL;
int choice, item;
for(;;)
{
printf("Stack Menun");
printf("1.Push OR Insert Frontn");
printf("2.Pop OR Delete Frontn");
printf("3.Displayn");
printf("4.Exitn");
printf("Enter U R Choicen");
scanf("%d", &choice);
printf("nn");
switch(choice)
{
case 1 : printf("Enter The Item To Insertn");
scanf("%d", &item);
printf("nn");
ins_first(&first, item);
break;
case 2 : item=del_first(&first);
if(item!='0')
printf("Deleted Element Is %dnn", item);
break;
case 3 : printf("Contents Of Stack Aren");
display(first);
break;
2. 2|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
default : exit(0);
}
}
return 1;
}
void ins_first(NODE **first, int item)
{
NODE *newn;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=item;
newn->next=*first;
*first=newn;
}
int del_first(NODE **first)
{
int item;
NODE *temp;
if(*first==NULL)
{
printf("Stack IS Underflownn");
return('0');
}
temp=*first;
item=temp->info;
*first=temp->next;
free(temp);
return item;
}
void display(NODE *first)
{
NODE *temp;
temp=first;
if(first==NULL)
{
printf("Stack Is Emptynn");
}
else
{
while(temp->next!=NULL)
{
printf("%dn", temp->info);
temp=temp->next;
}
printf("%dnn", temp->info);
}
}
3. 3|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
INSERT A NODE AS LAST NODE IN THE LIST
ins_last(&first, item);
void ins_last(NODE **first, int item)
{
NODE *newn, *temp;
temp=*first; /* This Is Correct */
newn=(NODE*)malloc(sizeof(NODE));
newn->info=item;
newn->next=NULL; /* Insesrting Node As Last Node. */
if(*first==NULL)
*first=newn;
/* temp=*first; Error In Code Blocks I Should Write Above Only */
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn;
}
}
4. 4|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
DELETE THE LAST NODE FROM A LIST OF INTEGERS.
int del_last(NODE **first)
{
NODE *temp, *prev;
int item;
temp=*first;
prev=NULL;
if(*first==NULL)
{
printf("Empty List, Deletion Not Possiblen");
return('0');
}
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
if(prev==NULL)
{
*first=NULL;
item=temp->info;
free(temp);
return item;
}
else
{
item=temp->info;
prev->next=NULL;
free(temp);
return item;
}
}
5. 5|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
TO COUNT NO OF NODES IN A LINKED LIST
int count(NODE *first)
{
NODE *temp;
int total=0;
temp=first;
if(first==NULL)
{
return total;
}
while(temp!=NULL)
{
total+=1;
temp=temp->next;
}
return total;
}
***OR***
int count(NODE *first)
{
NODE *temp;
int total=0;
temp=first;
if(first==NULL)
{
return total;
}
while(temp->next!=NULL)
{
total+=1;
temp=temp->next;
}
total+=1;
return total;
}
6. 6|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
TO COUNT THE NUMBER OF NODES WITH VALUE K IN A LIST.
void search_item(NODE *first, int item)
{
NODE *temp;
int pos=0, times=0;
temp=first; /*IMP*/
if(first==NULL)
printf("LISt Is Emptyn");
else
{
while(temp!=NULL)
{
pos+=1;
if(temp->info==item)
{
printf("%d Found At %d Positionn", item, pos);
times+=1;
}
temp=temp->next;
}
printf("%d Found %d Times", item, times);
}
}
7. 7|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
WRITE A PROGRAM TO SEARCH A NODE WITH A VALUE K , IF IT IS NOT FOUND THEN INSERT VALUE AS
LAST NODE.
void k_insert(NODE **first, int item)
{
NODE *temp, *newn;
newn=(NODE*)malloc(sizeof(NODE));
int flag;
flag=0;
newn->info=item;
newn->next=NULL;
if(*first==NULL)
*first=newn;
else
{
temp=*first;
while(temp!=NULL && flag==0)
{
if(temp->info==item)
flag=1; /* flag=1 When Item Found */
temp=temp->next;
}
}
}
if(flag==1)
printf("%d Foundn", item);
else
{
temp=*first;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newn;
}
8. 8|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
WAP TO SEARCH A LIST FOR A NODE WITH VALUE K, IF IT IS FOUND THEN DELETE THAT NODE.
case 5 : printf("Enter The Key To Deleten");
scanf("%d", &key);
item=del_key(&first, key);
if(item!='0')
printf("%d Is Deleted.n", item);
break;
int del_key(NODE **first, int key)
{
int item;
NODE *temp, *prev;
if(*first==NULL)
{
printf("List IS Emptyn");
return('0');
}
temp=*first;
if(key==temp->info)
{
item=temp->info;
*first=temp->next;
free(temp);
return item;
}
prev=NULL;
while(temp!=NULL)
{
if(key==temp->info)
break;
prev=temp;
temp=temp->next;
}
if(temp!=NULL)
{
item=temp->info;
prev->next=temp->next;
free(temp);
return item;
}
printf("Key Not Found In The Listn");
return('0');
}
9. 9|P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
WAP TO INSERT A NODE INTO A LINKED LIST AT A GIVEN POSTION.
void ins_pos(NODE** first, int item, int pos)
{
NODE *newn, *temp, *prev;
int k;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=item;
if(*first==NULL || pos==1)
{
newn->next=*first;
*first=newn;
}
else
{
temp=*first;
prev=NULL;
k=1;
while(temp!=NULL && k<pos)
{
prev=temp;
temp=temp->next;
k+=1;
}
newn->next=prev->next;
prev->next=newn;
}
}
10. 10 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
WAP TO DELETE A NODE AT THE SPECIFIED POSTION.
int del_pos(NODE **first, int pos)
{
int item, k=0;
NODE *temp, *prev;
if(*first==NULL || pos<=0)
{
printf("Invalid Postionn");
return('0');
}
if(pos==1)
{
temp=*first;
item=temp->info;
*first=temp->next;
free(temp);
return(item);
}
temp=*first;
prev=NULL;
k=1;
while(temp!=NULL)
{
if(k==pos)
/* If Found Go Out Of The LOOP */
break;
prev=temp;
temp=temp->next;
k+=1;
}
if(k!=pos)
/* Pos Found So Delete The Node */
{
printf("Invalid Postionn");
return('0');
}
else
/*If End OF List, Invalid Postion */
{
item=temp->info;
prev->next=temp->next;
free(temp);
return(item);
}
}
11. 11 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
WRITE A C FUNCTION TO INSERT A NODE WITH VALUE X TO THE RIGHT OF NODE WITH VALUE Y IN A
SINGLE LINKED LIST.
case 7 : printf("What's The Value Of Yn");
scanf("%d", &y);
printf("What's The Value Of Xn");
scanf("%d", &x);
ins_x_right_to_y(&first, x, y);
break;
void ins_x_right_to_y(NODE **first, int x, int y)
{
NODE *newn, *temp, *prev;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=x;
temp=*first;
prev=NULL;
if(*first==NULL)
printf("List Is Empty, y Not Foundn");
if(y==temp->info)
/* if key found in 1st postion */
{
newn->next=temp->next;
temp->next=newn;
}
else
{
while(temp!=NULL)
{
if(y==temp->info)
break;
prev=temp;
temp=temp->next;
}
if(y==temp->info)
{
newn->next=temp->next;
temp->next=newn;
}
else
printf("Key Not Foundn");
}
}
12. 12 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
WRITE A C FUNCTION TO INSERT A NODE WITH VALUE X TO THE LEFT OF NODE WITH VALUE Y IN A
SINGLE LINKED LIST.
case 8 : printf("What's The Value Of Yn");
scanf("%d", &y);
printf("What's The Value Of Xn");
scanf("%d", &x);
ins_x_left_to_y(&first, x, y);
break;
void ins_x_left_to_y(NODE **first, int x, int y)
{
NODE *newn, *temp, *prev;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=x;
temp=*first;
prev=NULL;
if(*first==NULL)
printf("List Is Empty, y Not Foundn");
if(y==temp->info)
/* if key found in 1st postion */
{
newn->next=*first;
*first=newn;
}
else
{
while(temp!=NULL)
{
if(y==temp->info)
break;
prev=temp;
temp=temp->next;
}
if(y==temp->info)
{
newn->next=prev->next;
prev->next=newn;
}
else
printf("Key Not Foundn");
}
}
13. 13 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
TO SEARCH FOR NODE WITH VALUE Y IN A LIST, IF IT IS FOUND THEN DELETE THE NODE(SAY X) NEXT
TO IT.
case 9 : printf("Enter Value For Yn");
scanf("%d", &y);
item=del_x_right_to_y(&first, y);
if(item!=0)
printf("%d Is Deleted.n", item);
break;
int del_x_right_to_y(NODE **first, int y)
{
NODE *temp, *prev, *after;
int x, flag=0;
if(*first==NULL)
{
printf("List IS Emptyn");
return 0;
}
temp=*first;
if(y==temp->info && temp->next==NULL)
{
printf("Item Found In 1st Positionn");
printf("No Further Element Present Right To It. So Deletion Not Possiblen");
return 0;
}
prev=NULL;
after=NULL;
while(temp!=NULL)
{
if(y==temp->info)
{
after=temp->next;
flag=1;
break;
}
prev=temp;
temp=temp->next;
}
if(flag==0)
{
printf("Y Not Foundn");
return 0;
}
14. 14 | P a g e
}
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
if(flag==1)
{
if(temp->next!=NULL)
{
x=after->info;
temp->next=after->next;
free(after);
return x;
}
else
{
printf("Item Found In Last Position, No item Present Right To itn");
printf("So Deletion Not Possiblen");
return 0;
}
}
15. 15 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
TO SEARCH FOR NODE WITH VALUE Y IN A LIST, IF IT IS FOUND THEN DELETE THE NODE(SAY X)
LEFT(BACK) TO IT.
case 10 : printf("Enter Value For Yn");
scanf("%d", &y);
item=del_x_left_to_y(&first, y);
if(item!=0)
printf("%d Is Deleted.n", item);
break;
int del_x_left_to_y(NODE **first, int y)
{
NODE *temp, *prev, *p2p;
int x, flag=0;
if(*first==NULL)
{
printf("List IS Emptyn");
return 0;
}
temp=*first;
if(y==temp->info)
{
printf("Item Found In 1st Positionn");
printf("No Further Element Present Left To It. So Deletion Not Possiblen");
return 0;
}
prev=NULL;
p2p=NULL;
while(temp!=NULL)
{
if(y==temp->info)
{
flag=1;
break;
}
p2p=prev;
prev=temp;
temp=temp->next;
}
if(flag!=1)
{
printf("Y Not Found In Listn");
return 0;
}
16. 16 | P a g e
}
else
{
if(p2p==NULL)
{
x=prev->info;
*first=prev->next;
free(prev);
return x;
}
else
{
x=prev->info;
p2p->next=prev->next;
free(prev);
return x;
}
}
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
17. 17 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
WAP TO REVERSE A LIST WITHOUT CREATING NEW NODES.
18. 18 | P a g e
5
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
SecondSemester M.C.A. Degree Examination, July 2006
a. Write an algorithm to perform each of the following operations on a linked list.
i) Append an element to the end of a list.(Page No 3)
ii) Free all the nodes in a list. (Page No 18)
iii) Delete every second element from a list. (Page No 19)
iv) Concatenate two lists. (Page No 20 And 23)
v) Delete the last element from a list. ( Page No 4)
(10 Marks))
ii) Free all the nodes in a list.
case 11 : printf("Deleting All The Nodes In A List...n");
free_all_nodes(&first);
break;
void free_all_nodes(NODE **first)
{
NODE *temp, *arc;
int item;
if(*first==NULL)
{
printf("List Is Already Emptyn");
}
else
{
temp=*first;
while(temp!=NULL)
{
item=temp->info;
*first=temp->next;
arc=temp;
temp=temp->next;
free(arc);
printf("%d Is Deletedn", item);
}
printf("All The Elements In The List Is Sucessfully Deletedn");
}
}
19. 19 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
iii) Delete every second element from a list.
void del_every_2nd_element(NODE **first)
{
NODE *temp, *prev, *arc;
int item, k;
if(*first==NULL)
printf("List Is Emptyn");
else
{
temp=*first;
prev=NULL;
k=0;
while(temp!=NULL)
{
k+=1;
if(k%2==0)
{
item=temp->info;
prev->next=temp->next;
arc=temp;
free(arc);
printf("%d Is Deletedn", item);
}
prev=temp;
temp=temp->next;
}
if(k==1)
printf("Only 1 Element Present In List So Deletion Not Possiblen");
else
printf("All 2nd Elements In The List Is Sucessfully Deletedn");
}
}
20. 20 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
iv) Concatenate two lists.
void concat(NODE **first1, NODE **first2)
{
NODE *temp;
if(*first1==NULL)
*first1=*first2;
else
{
temp=*first1;
while(temp->next!=NULL)
temp=temp->next;
temp->next=*first2;
}
}
Also See Page No 23 For Better Understanding..
21. 21 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
Assume A Linked List Of Integers In Assending Order And Develop The Program To Insert A Node With
Value Into A Appropriate Postion.
case 13 :
printf("Enter The Item To Insertn");
scanf("%d", &item);
ins_ele_assend_list(&first, item);
break;
void ins_ele_assend_list(NODE **first,int item)
{
NODE *newn, *temp, *prev;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=item;
if(*first==NULL)
{
newn->next=NULL;
*first=newn;
}
else
{
temp=*first;
prev=NULL;
while(temp!=NULL && item>=temp->info)/* OR while(temp!=NULL && temp->info<=item) */
{
prev=temp;
temp=temp->next;
}
if(prev==NULL)
{
newn->next=*first;
*first=newn;
}
else
{
newn->next=prev->next;
prev->next=newn;
}
}
}
22. 22 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
Write A C Routine To Delete All The Nodes Whose Information Field Contains The Value X From A
Singly Linked List.
void del_all_x(NODE **first, int x)
{
NODE *temp, *prev;
int count=0, item, k=0;
if(*first==NULL)
printf("List Is Emptyn");
else
{
temp=*first;
prev=NULL;
while(temp!=NULL)
{
if(temp->info==x && prev==NULL)
/* If We Miss prev==NULL From if Condition Mean Time If We Give The Input As 12 13 12 All These
Elements Are deleted */
{
item=temp->info;
*first=temp->next;
count+=1;
}
else if(temp->info==x)
{
item=temp->info;
prev->next=temp->next;
free(temp);
count+=1;
}
else
prev=temp;
temp=temp->next;
}
if(count>0)
printf("x=%d Found %d Times & It's Deletedn", x, count);
else
printf("x=%d Not Foundn", x);
}
}
23. 23 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
/* Concatenate two lists. */
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
void ins_first(NODE**, int);
int del_first(NODE**);
void display(NODE*);
void concat(NODE**, NODE**);
int main()
{
NODE *first1=NULL;
NODE *first2=NULL;
int choice, item;
for(;;)
{
printf("Stack Menun");
printf("1.In First List To Insert Frontn");
printf("2.In Second List To Insert Frontn");
printf("3.To Display First Listn");
printf("4.To Display Second Listn");
printf("5.To Concate Both List & To Displayn");
printf("6.Exitn");
printf("Enter U R Choicen");
scanf("%d", &choice);
printf("nn");
switch(choice)
{
case 1 :
printf("First Listn");
printf("Enter The Item To Insertn");
scanf("%d", &item);
printf("nn");
ins_first(&first1, item);
break;
case 2 :
printf("Second Listn");
printf("Enter The Item To Insertn");
scanf("%d", &item);
printf("nn");
ins_first(&first2, item);
break;
case 3 :
printf("Contents Of First Listn");
display(first1);
break;
case 4 :
printf("Contents Of Second Listn");
display(first2);
break;
24. 24 | P a g e
Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.
case 5
:
printf("Concating List Wait....n");
concat(&first1, &first2);
printf("After Concating Elements In List Isn");
display(first1);
break;
default : exit(0);
}
}
return 1;
}
void ins_first(NODE **first, int item)
{
NODE *newn;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=item;
newn->next=*first;
*first=newn;
}
void display(NODE *first)
{
NODE *temp;
temp=first;
if(first==NULL)
{
printf("List Is Emptynn");
}
else
{
while(temp->next!=NULL)
{
printf("%dn", temp->info);
temp=temp->next;
}
printf("%dnn", temp->info);
}
}
void concat(NODE **first1, NODE **first2)
{
NODE *temp;
if(*first1==NULL)
*first1=*first2;
else
{
temp=*first1;
while(temp->next!=NULL)
temp=temp->next;
temp->next=*first2;
}
}