SlideShare a Scribd company logo
1 of 24
Download to read offline
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|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|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|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|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|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|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|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|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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | P a g e

Ashoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

WAP TO REVERSE A LIST WITHOUT CREATING NEW NODES.
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 | 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 | 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 | 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 | 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 | 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 | 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;
}
}

More Related Content

What's hot

Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sortUmmar Hayat
 
Data Structure Notes Part-1
Data Structure Notes Part-1 Data Structure Notes Part-1
Data Structure Notes Part-1 NANDINI SHARMA
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfSreedhar Chowdam
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methodsnarmadhakin
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File Rahul Chugh
 
Array sorting
Array sortingArray sorting
Array sortingALI RAZA
 
Insertion into linked lists
Insertion into linked lists Insertion into linked lists
Insertion into linked lists MrDavinderSingh
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 

What's hot (20)

Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Python list
Python listPython list
Python list
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
Data Structure Notes Part-1
Data Structure Notes Part-1 Data Structure Notes Part-1
Data Structure Notes Part-1
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdf
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Linked List
Linked ListLinked List
Linked List
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Red black tree
Red black treeRed black tree
Red black tree
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Array sorting
Array sortingArray sorting
Array sorting
 
Stacks
StacksStacks
Stacks
 
Insertion into linked lists
Insertion into linked lists Insertion into linked lists
Insertion into linked lists
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 

Viewers also liked

Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaiCreateWorld
 
Why Code 'for' Android and Motodev Studio
Why Code 'for' Android and Motodev StudioWhy Code 'for' Android and Motodev Studio
Why Code 'for' Android and Motodev StudioCodeAndroid
 
Android studio 2.0: default project structure
Android studio 2.0: default project structureAndroid studio 2.0: default project structure
Android studio 2.0: default project structureVyara Georgieva
 
Android Development - Process & Tools
Android Development - Process & ToolsAndroid Development - Process & Tools
Android Development - Process & ToolsLope Emano
 
My Project Report Documentation with Abstract & Snapshots
My Project Report Documentation with Abstract & SnapshotsMy Project Report Documentation with Abstract & Snapshots
My Project Report Documentation with Abstract & SnapshotsUsman Sait
 
Android College Application Project Report
Android College Application Project ReportAndroid College Application Project Report
Android College Application Project Reportstalin george
 

Viewers also liked (7)

Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU Karnataka
 
Why Code 'for' Android and Motodev Studio
Why Code 'for' Android and Motodev StudioWhy Code 'for' Android and Motodev Studio
Why Code 'for' Android and Motodev Studio
 
Android studio 2.0: default project structure
Android studio 2.0: default project structureAndroid studio 2.0: default project structure
Android studio 2.0: default project structure
 
Android Development - Process & Tools
Android Development - Process & ToolsAndroid Development - Process & Tools
Android Development - Process & Tools
 
My Project Report Documentation with Abstract & Snapshots
My Project Report Documentation with Abstract & SnapshotsMy Project Report Documentation with Abstract & Snapshots
My Project Report Documentation with Abstract & Snapshots
 
Android College Application Project Report
Android College Application Project ReportAndroid College Application Project Report
Android College Application Project Report
 
Android report
Android reportAndroid report
Android report
 

Similar to Data structure singly linked list programs VTU Exams

Data structure doubly linked list programs
Data structure doubly linked list programsData structure doubly linked list programs
Data structure doubly linked list programsiCreateWorld
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdfpasqualealvarez467
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfFOREVERPRODUCTCHD
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
DS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docxDS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docxVeerannaKotagi1
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
Data structure circular list
Data structure circular listData structure circular list
Data structure circular listiCreateWorld
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdfanupambedcovers
 
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdfI will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdffunkybabyindia
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdfChange the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdffatoryoutlets
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfbrijmote
 
These are the 4 functions #include KiostreamP using namespac.pdf
These are the 4 functions #include KiostreamP using namespac.pdfThese are the 4 functions #include KiostreamP using namespac.pdf
These are the 4 functions #include KiostreamP using namespac.pdfJUSTSTYLISH3B2MOHALI
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfarkmuzikllc
 
C++ Program to Implement Singly Linked List #includeiostream.pdf
 C++ Program to Implement Singly Linked List #includeiostream.pdf C++ Program to Implement Singly Linked List #includeiostream.pdf
C++ Program to Implement Singly Linked List #includeiostream.pdfangelsfashion1
 

Similar to Data structure singly linked list programs VTU Exams (20)

Data structure doubly linked list programs
Data structure doubly linked list programsData structure doubly linked list programs
Data structure doubly linked list programs
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
DS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docxDS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docx
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Data structure circular list
Data structure circular listData structure circular list
Data structure circular list
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
 
week-20x
week-20xweek-20x
week-20x
 
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdfI will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdfChange the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdf
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdf
 
data structure
data structuredata structure
data structure
 
Tugas1
Tugas1Tugas1
Tugas1
 
These are the 4 functions #include KiostreamP using namespac.pdf
These are the 4 functions #include KiostreamP using namespac.pdfThese are the 4 functions #include KiostreamP using namespac.pdf
These are the 4 functions #include KiostreamP using namespac.pdf
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
 
C++ Program to Implement Singly Linked List #includeiostream.pdf
 C++ Program to Implement Singly Linked List #includeiostream.pdf C++ Program to Implement Singly Linked List #includeiostream.pdf
C++ Program to Implement Singly Linked List #includeiostream.pdf
 

Recently uploaded

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

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; } }