Vous êtes sur la page 1sur 69

St.

MARTIN’S ENGINEERING COLLEGE


Dhulapally, Secunderabad - 500100
NBA & NAAC A+ Accredited
LIST
OF EXPERIMENTS

1. Write a program that uses functions to perform the following operations on singly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
2. Write a program that uses functions to perform the following operations on doubly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
3. Write a program that uses functions to perform the following operations on circular linked
list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
4. Write a program that implement stack (its operations) using
i) Arrays ii) Pointers
5. Write a program that implement Queue (its operations) using
i) Arrays ii) Pointers
6. Write a program that implements the following sorting methods to sort a given list of integers
in ascending order
i) Bubble sort ii) Selection sort iii) Insertion sort
7. Write a program that use both recursive and non recursive functions to perform the following
searching operations for a Key value in a given list of integers:
i) Linear search ii) Binary search
8. Write a program to implement the tree traversal methods.
9. Write a program to implement the graph traversal methods.

Department of CSE CS307PC-Data Structures Lab


1. Write a program that uses functions to perform the following operations on singly linked
list.
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
void create();
void insert();
void del();
void display();
struct node
{
int data;
struct node *link;
};
struct node *first = null, *last = null ,*next, *curr, *prev;
int ch;
void main()
{
clrscr();
printf(“singly linked list \n”);
do
{
printf(“\n 1.create \n 2.insert \n 3.delete \n 4.exit \n ”);
Department of CSE CS307PC-Data Structures Lab
printf(“Enter your choice”);
scanf(“%d”, &ch);
switch(ch)
{
case 1: create();
display();
break;
case 2: insert();
display();
break;
case 3: del();
display();
break;
case 4: exit(0);
}
}
while(ch<=3);
}
void create()
{
curr = (struct node *) malloc(sizeof(struct node));
printf(“Enter the data: ”);
scanf(“%d”, &curr -> data);
curr -> link = null;
first = curr;
last = curr;

Department of CSE CS307PC-Data Structures Lab


}
void insert()
{
int pos, c = 1;
curr=(struct node *)malloc(sizeof(struct node));
printf(“Enter the data:”);
scanf(“%d”, &curr -> data);
printf(“Enter the position:”);
scanf(“%d”, &pos);
if((pos == 1) && (first != null))
{
curr -> link = first;
first = curr;
}
else
{
next = first;
while(c < pos)
{
prev = next;
next = prev -> link;
c++;
}
if(prev == null)
{
printf(“\n Invalid position”);

Department of CSE CS307PC-Data Structures Lab


}
else
{
curr -> link = prev -> link;
prev -> link = curr;
if(curr -> link == null)
{
last = curr;
}
}
}
void del()
{
int pos, c = 1;
printf(“Enter the position”);
scanf(“%d”, &pos);
if(first = null)
{
printf(“\n list is empty”);
}
else if(pos == 1) && (first -> link == null)
{
printf(“\n Deleted element is %d \n”, curr -> data);
free(curr);
}
else

Department of CSE CS307PC-Data Structures Lab


{
next = first;
while(c < pos)
{
prev = next;
next = next -> link;
c++;
}
prev -> link = next -> link;
next -> link = null;
if(next = null)
{
printf(“\n Invalid position”);
}
else
{
printf(“\n Deleted element is:%d\n”, next -> data);
free(next);
if(prev -> link == null)
{
last = prev;
}
}
}
}
void display()

Department of CSE CS307PC-Data Structures Lab


{
curr = first;
while(curr != null)
{
printf(“\n %d”, curr -> data);
curr = curr -> link;
}
}
Input & Output:
Singly linked list
1.create
2.insert
3.del
4.exit
Enter your choice 1
Enter the data:2
1.create
2.insert
3.del
4.exit
Enter your choice 2
Enter the data: 4
Enter the position: 2
2
4
1.create

Department of CSE CS307PC-Data Structures Lab


2.insert
3.del
4.exit
Enter your choice 4

2. Write a C program to perform the following operations on doubly linked list.:


i) Creation ii) Insertion iii) Deletion iv) Traversal
#include <stdio.h>
#include <stdlib.h>
typedef struct dubll
{
int data;
struct dubll *leftlink,*rightlink;
}*DUBLL;
DUBLL high,temp_node,low,last,pntr;
int flag=0;
DUBLL NodeAlloc();
DUBLL Search(int,int);

Department of CSE CS307PC-Data Structures Lab


void CreateItem();
void AppendItem();
void PrintItem();
void DeleteItem();
DUBLL Search(int item,int flag);
DUBLL NodeAlloc();
void InsertItem();
main(void)
{
int choice,Item;
high=NULL;
while(1)
{
//clrscr();
printf("\n \t\t\t***** M A I N M E N U *****\n\n");
printf("\n 1: Create Linked List \n 2: Append a Node to the List \n 3: Traverse the List \n 4:
Delete a Node from the List \n 5: Search a Node \n 6: Insert a Node to the List \n 7: Close
\n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&choice);
switch(choice)
{
case 1:
CreateItem();
puts("\nPress any key to go back to main menu.");
getch();
break;
case 2:

Department of CSE CS307PC-Data Structures Lab


AppendItem();
break;
case 3:
PrintItem();
puts("\nPress any key to go back to main menu.");
getch();
break;
case 4:
DeleteItem();
break;
case 5:
printf("Find an Item: ");
scanf("%d",&Item);
temp_node=Search(Item,0);
if(temp_node)
{
puts("The item is available in the Linked List.");
}
else
{
puts("The item is not found in the Linked List.");
}
getch();
break;
case 6:
InsertItem();

Department of CSE CS307PC-Data Structures Lab


break;
case 7:
exit(0);
default:
puts("Invalid choice.");
puts("\nPress any key to go back to main menu.");
getch();
break;
}
}
}
/* Function to Create the list*/
void CreateItem()
{
if(high==NULL)
{
printf("\n --Creating the list--");
temp_node=NodeAlloc();
printf("\n Enter starting data (as integer value) :");
scanf("%d",&temp_node->data);
high=temp_node;
}
else{ printf("\n List already created @ %d with %d as data.",high,high->data);}
}

/* Function to Append items to the list*/

Department of CSE CS307PC-Data Structures Lab


void AppendItem()
{
low=high;
if(high==NULL)
{
CreateItem();
}
else
{
temp_node=NodeAlloc();
printf("\n Enter Item (in integer) :");
scanf("%d",&temp_node->data);
temp_node->rightlink=NULL;

while(low->rightlink!=NULL)
low=low->rightlink;
low->rightlink=temp_node;
temp_node->leftlink=low;
last=low->rightlink;

}
}
/* Function to Traverse the list both ways and print the data*/
void PrintItem()
{
DUBLL temp_node;

Department of CSE CS307PC-Data Structures Lab


if(high==NULL)
{
printf("\n List is not available. Please create a list first.");
getch();
CreateItem();
}
temp_node=high;
last=low->rightlink;
printf("\n--Printing The List In Forward direction--\n");

while(temp_node!=NULL) //In forward direction


{
printf("\t %d",temp_node->data);
temp_node = temp_node->rightlink;
}
printf("\n");
printf("\n--Printing The List In Backward direction--\n");
temp_node=high;
if(temp_node->rightlink==NULL){printf("%d",temp_node->data);return; }
while(last!=NULL) //In backward direction
{
printf("\t %d",last->data);
last = last->leftlink;
}
}

Department of CSE CS307PC-Data Structures Lab


/* Function to Delete items of the list*/
void DeleteItem()
{
int value;
DUBLL temp_node;
if(high==NULL)
{
printf("\n List is not available. Please create a list first.");
getch();
CreateItem();
}
printf("\n Item to delete :");
scanf("%d",&value);
pntr=Search(value,1);
pntr->leftlink->rightlink=pntr->rightlink;
pntr->rightlink->leftlink=pntr->leftlink;
temp_node=pntr;
free(temp_node);
}
/* Function to Search an item from the list*/
DUBLL Search(int item,int flag)
{
temp_node = high;
if(high==NULL)
{
printf("\n List is not available. Please create a list first.");

Department of CSE CS307PC-Data Structures Lab


getch();
CreateItem();
}
while(temp_node!=NULL)
{
if(temp_node->data==item )
{
if(flag==0)
{
return(1);
}
else
{
return(temp_node);
}
}
temp_node=temp_node->rightlink;
}
}
/* Function to Allocate nodes*/
DUBLL NodeAlloc()
{
DUBLL tmep_node;
tmep_node=malloc(sizeof(struct dubll));
if(tmep_node==NULL)
{

Department of CSE CS307PC-Data Structures Lab


printf("\n No memory available. Node allocation cannot be done.");
}
tmep_node->rightlink=tmep_node->leftlink=NULL;
return(tmep_node);
}
/* Function to Insert items in the middle of the list*/
Void InsertItem()
{
int node;
DUBLL temp_node;

if(high==NULL)
{
printf("\n List is not available. Please create a list first.");
getch();
CreateItem();
}
temp_node=NodeAlloc();
printf("Position At which node to be inserted: ___ & New Item Value: ___ ");
scanf("%d",&node);
scanf("%d",&temp_node->data);
pntr=Search(node,1);
if(pntr->rightlink==NULL){printf("\n The operation is not possible."); getch();
return;}
temp_node->leftlink=pntr; //creating link to new node
temp_node->rightlink=pntr->rightlink;

Department of CSE CS307PC-Data Structures Lab


pntr->rightlink->leftlink=temp_node;
pntr->rightlink=temp_node;
printf("\n Item has been Inserted.");
getch();
}
Output:
***** M A I N M E N U *****
1: Create Linked List
2: Append a Node to the List
3: Traverse the List
4: Delete a Node from the List
5: Search a Node
6: Insert a Node to the List
7: Close
Enter your Option [1]
Creating the list
Enter starting data (as integer value) :10
Press any key to go back to main menu.

***** M A I N M E N U *****
1: Create Linked List
2: Append a Node to the List
3: Traverse the List
4: Delete a Node from the List
5: Search a Node
6: Insert a Node to the List

Department of CSE CS307PC-Data Structures Lab


7: Close
Enter your Option [2]
Enter Item (in integer):20

***** M A I N M E N U *****
1: Create Linked List
2: Append a Node to the List
3: Traverse the List
4: Delete a Node from the List
5: Search a Node
6: Insert a Node to the List
7: Close
Enter your Option [2]
Enter Item (in integer):30

***** M A I N M E N U *****
1: Create Linked List
2: Append a Node to the List
3: Traverse the List
4: Delete a Node from the List
5: Search a Node
6: Insert a Node to the List
7: Close
Enter your Option [2]
Enter Item (in integer) :40

Department of CSE CS307PC-Data Structures Lab


***** M A I N M E N U *****
1: Create Linked List
2: Append a Node to the List
3: Traverse the List
4: Delete a Node from the List
5: Search a Node
6: Insert a Node to the List
7: Close
Enter your Option [2]
Enter Item (in integer):50

***** M A I N M E N U *****
1: Create Linked List
2: Append a Node to the List
3: Traverse the List
4: Delete a Node from the List
5: Search a Node
6: Insert a Node to the List
7: Close
Enter your Option [3]
Printing The List in Forward direction--
10 20 30 40 50
Printing The List in Backward direction--
50 40 30 20 10
Press any key to go back to main menu.

Department of CSE CS307PC-Data Structures Lab


***** M A I N M E N U *****
1: Create Linked List
2: Append a Node to the List
3: Traverse the List
4: Delete a Node from the List
5: Search a Node
6: Insert a Node to the List
7: Close
Enter your Option [7]

Department of CSE CS307PC-Data Structures Lab


3. Write a program that uses functions to perform the following operations on circular
linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head = NULL, *x, *y, *z;
void create();
void ins_at_beg();
void ins_at_pos();
void del_at_beg();
void del_at_pos();
void traverse();
Department of CSE CS307PC-Data Structures Lab
void main()
{
int ch;
printf("\n 1.Creation \n 2.Insertion at beginning \n 3.Insertion at remaining");
printf("\n4.Deletion at beginning \n5.Deletion at remaining \n6.traverse \n7.Exit\n ");
while (1)
{
printf("\n Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
ins_at_beg();
break;
case 3:
ins_at_pos();
break;
case 4:
del_at_beg();
break;
case 5:
del_at_pos();
break;

Department of CSE CS307PC-Data Structures Lab


case 6:
traverse();
break;
default:
exit(0);
}
}
}
/*Function to create a new circular linked list*/
void create()
{
int c;

x = (struct node*)malloc(sizeof(struct node));


printf("\n Enter the data:");
scanf("%d", &x->data);
x->link = x;
head = x;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d", &c);
while (c != 0)
{
y = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d", &y->data);
x->link = y;

Department of CSE CS307PC-Data Structures Lab


y->link = head;
x = y;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d", &c);
}
}
/*Function to insert an element at the beginning of the list*/
void ins_at_beg()
{
x = head;
y = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d", &y->data);
while (x->link != head)
{
x = x->link;
}
x->link = y;
y->link = head;
head = y;
}

/*Function to insert an element at any position the list*/


void ins_at_pos()
{
struct node *ptr;

Department of CSE CS307PC-Data Structures Lab


int c = 1, pos, count = 1;
y = (struct node*)malloc(sizeof(struct node));
if (head == NULL)
{
printf("cannot enter an element at this place");
}
printf("\n Enter the data:");
scanf("%d", &y->data);
printf("\n Enter the position to be inserted:");
scanf("%d", &pos);
x = head;
ptr = head;
while (ptr->link != head)
{
count++;
ptr = ptr->link;
}
count++;
if (pos > count)
{
printf("OUT OF BOUND");
return;
}
while (c < pos)
{
z = x;

Department of CSE CS307PC-Data Structures Lab


x = x->link;
c++;
}
y->link = x;
z->link = y;
}
/*Function to delete an element at any beginning of the list*/
void del_at_beg()
{
if (head == NULL)
printf("\n List is empty");
else
{
x = head;
y = head;
while (x->link != head)
{
x = x->link;
}
head = y->link;
x->link = head;
free(y);
}
}
/*Function to delete an element at any position the list*/
void del_at_pos()

Department of CSE CS307PC-Data Structures Lab


{
if (head == NULL)
printf("\n List is empty");
else
{
int c = 1, pos;
printf("\n Enter the position to be deleted:");
scanf("%d", &pos);
x = head;
while (c < pos)
{
y = x;
x = x->link;
c++;
}
y->link = x->link;
free(x);
}
}
/*Function to display the elements in the list*/
void traverse()
{
if (head == NULL)
printf("\n List is empty");
else
{

Department of CSE CS307PC-Data Structures Lab


x = head;
while (x->link != head)
{
printf("%d->", x->data);
x = x->link;
}
printf("%d", x->data);
}
}
Output:
$ cc circular_singly_ll.c
$ a.out
1. Creation
2. Insertion at beginning
3. Insertion at remaining
4. Deletion at beginning
5. Deletion at remaining
6. Traverse
7. Search
8. Sort
9. Update
10. Exit
Enter your choice: 6
List is empty
Enter your choice: 5
List is empty

Department of CSE CS307PC-Data Structures Lab


Enter your choice: 9
Empty list
Enter your choice: 7
Enter the element to search
12
List is empty nothing to search
Enter your choice: 1
Enter the data: 10
If you wish to continue press 1 otherwise 0:0
Enter your choice: 3
Enter the data: 20
Enter the position to be inserted: 5
OUT OF BOUND
Enter your choice: 2
Enter the data: 12
Enter your choice: 6
12->10
Enter your choice: 3
Enter the data: 13
Enter the position to be inserted: 3
Enter your choice: 3
Enter the data: 14
Enter the position to be inserted: 4
Enter your choice: 6
12->10->13->14
Enter your choice: 3

Department of CSE CS307PC-Data Structures Lab


Enter the data: 24
Enter the position to be inserted: 4
Enter your choice: 6
12->10->13->24->14
Enter your choice: 3
Enter the data: 10
Enter the position to be inserted: 100
OUT OF BOUND
Enter your choice: 4
Enter your choice: 6
10->13->24->14
Enter your choice: 5
Enter the position to be deleted: 4
Enter your choice: 6
10->13->24
Enter your choice: 5
Enter the position to be deleted: 2
Enter your choice: 6
10->24
Enter your choice: 9
Enter the value to be edited
23
Enter the value to be replace
24
Update not successful
Enter your choice: 9

Department of CSE CS307PC-Data Structures Lab


Enter the value to be edited
24
Enter the value to be replace
26
Update successful
Enter your choice: 6
10->26
Enter your choice: 7
Enter the element to search
26
Element found at position 1
Element not found
Enter your choice: 7
Enter the element to search
27
Element not found
Enter your choice: 8
Enter your choice: 6
10->26
Enter your choice: 10
26 10
Enter your choice: 11

Department of CSE CS307PC-Data Structures Lab


Department of CSE CS307PC-Data Structures Lab
4. Write a program that implement stack (its operations) using
i) Arrays ii) Pointers
#include<stdio.h>
#include<stdlib.h>
struct stackarr;
typedef struct stackarr * Stack;
struct stackarr
{
int Capacity;
int TopOfStack;
int *Array;
};
void MakeEmpty(Stack s)
{
s->TopOfStack = -1;
}
Stack CreateStack(int MaxElements)
{
Stack s;
s = (struct stackarr*) malloc(sizeof(struct stackarr));
s->Array = (int *)malloc(sizeof(int) * MaxElements);

Department of CSE CS307PC-Data Structures Lab


s->Capacity = MaxElements;
MakeEmpty(s);
return s;
}
void DisposeStack(Stack s)
{
if(s != NULL)
{
free(s->Array);
free(s);
}
}
int isFull(Stack s)
{
return s->TopOfStack == s->Capacity - 1;
}
int isEmpty(Stack s)
{
return s->TopOfStack == -1;
}
void Push(int x, Stack s)
{
if(isFull(s))
printf("Full Stack\n\n");
else
s->Array[++s->TopOfStack] = x;

Department of CSE CS307PC-Data Structures Lab


}
void Pop(Stack s)
{
if(isEmpty(s))
printf("Empty Stack\n\n");
else
s->TopOfStack--;
}
int Top(Stack s)
{
if(isEmpty(s))
printf("Empty Stack\n\n");
else
return s->Array[s->TopOfStack];
}
int TopAndPop(Stack s)
{
if(isEmpty(s))
printf("Empty Stack\n\n");
else
return s->Array[s->TopOfStack--];
}
void Display(Stack s)
{
int i;
if(isEmpty(s))

Department of CSE CS307PC-Data Structures Lab


printf("Empty Stack\n\n");
else
{
printf("The Stack Elements are :: ");
for(i=s->TopOfStack; i >= 0; i--)
printf("%d ",s->Array[i]);
printf("\n\n");
}
}
void main()
{
int n, x, ch, i;
Stack s;
printf("Enter the maximum number of elements in the stack :: ");
scanf("%d", &n);
s = CreateStack(n);
printf("ARRAY IMPLEMENTATION OF STACK ADT\n\n");
do
{
printf("\n\n1. PUSH\t 2. POP\t 3.TOP \t 4. TOPANDPOP\t 5. PRINT\t 6. QUIT\n\nEnter
the choice :: ");
scanf("%d", &ch);
switch(ch)
{
Case 1:
printf("Enter the element to be pushed :: ");
scanf("%d",&x);
Department of CSE CS307PC-Data Structures Lab
Push(x,s);
break;
Case 2:
Pop(s);
break;
Case 3:
printf("The Top element in the stack :: %d\n\n", Top(s));
break;
Case 4:
printf("The popped top element in the stack :: %d\n\n", TopAndPop(s));
break;
Case 5:
Display(s);
break;
}
}while(ch<6);
DisposeStack(s);
return 0;
}

Output:
Enter the maximum number of elements in the stack :: 5
ARRAY IMPLEMENTATION OF STACK ADT
1. PUSH 2. POP 3.TOP 4. TOPANDPOP 5. PRINT 6. QUIT
Enter the choice: 1
Enter the element to be pushed: 10

Department of CSE CS307PC-Data Structures Lab


1. PUSH 2. POP 3.TOP 4. TOPANDPOP 5. PRINT 6. QUIT
Enter the choice: 1
Enter the element to be pushed: 20
1. PUSH 2. POP 3.TOP 4. TOPANDPOP 5. PRINT 6. QUIT
Enter the choice: 1
Enter the element to be pushed: 30
1. PUSH 2. POP 3.TOP 4. TOPANDPOP 5. PRINT 6. QUIT
Enter the choice: 5
The Stack Elements are: 30 20 10
1. PUSH 2. POP 3.TOP 4. TOPANDPOP 5. PRINT 6. QUIT
Enter the choice: 2
1. PUSH 2. POP 3.TOP 4. TOPANDPOP 5. PRINT 6. QUIT
Enter the choice: 5
The Stack Elements are: 20 10
1. PUSH 2. POP 3.TOP 4. TOPANDPOP 5. PRINT 6. QUIT
Enter the choice:

Department of CSE CS307PC-Data Structures Lab


5. Write a program that implement Queue (its operations) using
i) Arrays ii) Pointers
#include<stdio.h>
#include<stdlib.h>
#define max 10
int insertq ( int queue[max], int *rear , int *data)
{
if ( *rear == max -1 )
return(-1);
else

Department of CSE CS307PC-Data Structures Lab


{
*rear = *rear + 1;
queue[*rear] = *data;
return(1);
}
}
int delq( int queue[max], int *front, int *rear , int *data)
{
if ( *front == *rear)
return(-1);
else
{
(*front)++;
*data = queue[*front];
return(1);
}
}
int main()
{
int queue[max],data;
int front,rear,reply,option;
//... init queue
front = rear = -1;
printf("\tMenu");
printf("\n----------------------------");
printf("\n 1. Insert element in queue");

Department of CSE CS307PC-Data Structures Lab


printf("\n 2. Delete element from queue");
printf("\n 3. Exit");
printf("\n----------------------------");
while(1)
{
printf("\nChoose operation : ");
scanf("%d",&option);
switch(option)
{
case 1 ://insert
printf("\nEnter Number : ");
scanf("%d",&data);
reply = insertq(queue,&rear,&data);
if ( reply == - 1)
printf("Queue is full");
else
printf("%d is inserted in queue.\n",data);
break;
case 2 : //dele
reply = delq(queue,&front,&rear,&data);
if ( reply == -1 )
printf("Queue is empty ");
else
printf("\nDeleted number is : %d\n", data);
break;
case 3 : exit(0);

Department of CSE CS307PC-Data Structures Lab


}
}
}
Output:
Menu
----------------------------
1. Insert element in queue
2. Delete element from queue
3. Exit
----------------------------
Choose operation : 1
Enter Number : 5
5 is inserted in queue.
Choose operation : 1
Enter Number : 9
9 is inserted in queue.
Choose operation : 1
Enter Number : 88
88 is inserted in queue.
Choose operation : 2
Deleted number is : 5
Choose operation : 2
Deleted number is : 9
Choose operation : 3

Department of CSE CS307PC-Data Structures Lab


6. Write a program that implements the following sorting methods to sort a given list of
integers in ascending order
i) Bubble sort ii) Selection sort iii) Insertion sort
#include<stdio.h>
Department of CSE CS307PC-Data Structures Lab
#include<stdlib.h>
//function prototypes
void bubble(int *,int);
void selection(int *,int);
void insertion(int *,int);
int main()
{
int count=0; //size of array
int choice=0,ch=0; //variables used to store user choice
int check=0; //used to check status
int i=0; //loop variable
printf("Enter the size of the list: ");
scanf("%d",&count);
//creating array of appropriate size
int list[count];
//filling in the array
for(i=0;i<count;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&list[i]);
}
//prints the list
printf("\nNumbers entered: ");
for(i=0;i<count;i++)
printf("%d,",list[i]);
printf("\n");

Department of CSE CS307PC-Data Structures Lab


//Creates Menu for the user
do{
printf("Menu:\n\n");
printf("1.Bubble sort\n2.Selection Sort\n3.Inserton sort\n4.Exit\nYour choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: //performs bubble sort
bubble(list,count);
break;
case 2: //performs selection sort
selection(list,count);
break;
case 3: //performs insertion sort
insertion(list,count);
break;
case 4: return 0;
default: printf("Invalid option\nRetry: ");
break;
}
printf("Do you want to continue(press 1 to continue any other number to exit): ");
scanf("%d",&ch);
} while(ch==1);
return 0;
}

Department of CSE CS307PC-Data Structures Lab


/* Bubble sort
Function: bubble(). This function sorts a given integer array in ascending order
using bubble sort algorithm.
Input: 1)Integer array
2)size of the array
Returns: This function does not return anything.The sorted array is printed after sorting.

*/
void bubble(int *list,int n)
{
int i,j;
int c;
for(i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (list[j] > list[j+1])
{
c=list[j];
list[j]=list[j+1];
list[j+1]=c;
}
}
}
printf("\nSorted list in ascending order:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d,",list[i]);
Department of CSE CS307PC-Data Structures Lab
printf("\n");
}
/* Selection sort
Function: selection(). This function sorts a given integer array in ascending order
using selection sort algorithm.
Input: 1) Integer array
2) Size of the array
Returns: This function does not return anything.The sorted array is printed after sorting.
*/
void selection(int *list,int n)
{
int i;
int j,min;
int k;
for(j=0;j<n-1;j++)
{
min=list[j];
k=j;
for(i=j+1;i<n;i++)
{
if(list[i]<min)
{
min=list[i];
k=i;
}
}

Department of CSE CS307PC-Data Structures Lab


list[k]=list[j];
list[j]=min;
}
printf("Sorted list is:");
for(i=0;i<n;i++)
{
printf("%d,",list[i]);
}
printf("\n");
}
/* Insertion sort
Function: insertion (). This function sorts a given integer array in ascending order
Using insertions sort algorithm.
Input: 1) Int eger array
2) Size of the array
Returns: This function does not return anything.The sorted array is printed after sorting.
*/
void insertion(int *list,int n)
{
int temp;
int i=0,j=0;
for(i=1;i<n;i++)
{
temp=list[i];
j=i-1;
while((j>=0)&&(list[j]>temp))

Department of CSE CS307PC-Data Structures Lab


{
list[j+1]=list[j];
j--;
}
list[j+1]=temp;
}
printf("Sorted list is: ");
for(i=0;i<n;i++)
{
printf("%d,",list[i]);
}
printf("\n");
}
Output:
Enter the size of the list: 6
Enter element 1: 6
Enter element 2: 5
Enter element 3: 4
Enter element 4: 3
Enter element 5: 2
Enter element 6: 1
Numbers entered: 6,5,4,3,2,1,
Menu:
1.Bubble sort
2.Selection Sort
3.Inserton sort

Department of CSE CS307PC-Data Structures Lab


4.Exit
Your choice: 1
Sorted list in ascending order:
1,2,3,4,5,6,
Do you want to continue(press 1 to continue any other number to exit): 1
Menu:
1.Bubble sort
2.Selection Sort
3.Inserton sort
4.Exit
Your choice: 2
Sorted list is:1,2,3,4,5,6,
Do you want to continue(press 1 to continue any other number to exit): 1

Menu:
1. Bubble sort
2. Selection Sort
3. Inserton sort
4. Exit
Your choice: 3
Sorted list is: 1, 2, 3,4,5,6,
Do you want to continue (press 1 to continue any other number to exit): 4

Department of CSE CS307PC-Data Structures Lab


\

7. Write a program that uses both recursive and non recursive functions to perform the
following searching operations for a Key value in a given list of integers:
i) Linear search ii) Binary search
Linear Search Using Recursive Functions
#include<stdio.h>
#include<conio.h>45
int lin_search(int[],int,int);
main()
{
int a[100],n,i,ele;
clrscr();
printf("Enter Size");
scanf("%d",&n);
printf("Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The array elements");
for(i=0;i<n;i++)
Department of CSE CS307PC-Data Structures Lab
printf("%4d",a[i]);
printf("\nEnter element to search");
scanf("%d",&ele);
i=lin_search(a,n-1,ele);
if(i!=-1)
printf("\nThe element %d found at %d location",ele,i+1);
else
printf("\nThe element %d is not found",ele);
getch();
}
int lin_search(int x[100],int n,int ele)
{
if(n<=0)
return -1;
if(x[n]==ele)
return n;
else
return lin_search(x,n-1,ele);
}
Output:
Enter Size5
Enter 5 elements25
30
12
54
60

Department of CSE CS307PC-Data Structures Lab


The array elements
25 30 12 54 60
Enter element to search
60
The element 60 found at 5 location
Linear Search Using Non-Recursive Functions
#include<stdio.h>
#include<conio.h>
main()
{
int a[100],i,n,ele,loc=-1;
clrscr();
printf("Enter Size");
scanf("%d",&n);
printf("Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The array elements");
for(i=0;i<n;i++)
printf("%4d",a[i]);
printf("\nEnter element to search");
scanf("%d",&ele);
for(i=0;i<=n-1;i++)
{
if(ele==a[i])
{

Department of CSE CS307PC-Data Structures Lab


loc=i;
break;
}
}
if(loc>=0)
printf("\nThe element %d found at %d location",ele,loc+1);
else
printf("\nThe element %d is not found",ele);
getch();
}
int lin_search(int x[100],int n,int ele)
{
if(n<=0)
return -1;
if(x[n]==ele)
return n;
else
return lin_search(x,n-1,ele);
}

Output:
Enter Size5
Enter 5 elements12
30
25
4

Department of CSE CS307PC-Data Structures Lab


85
The array elements 12 30 25 4 85
Enter element to search25
The element 25 found at 3 location
Implementation of binary search using non-recursive and Recursive functions.
#include <stdio.h>
#define MAX_LEN 10
/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else

Department of CSE CS307PC-Data Structures Lab


i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}
/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}
void read_list(int l[],int n)
{
int i;
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)

Department of CSE CS307PC-Data Structures Lab


scanf("%d",&l[i]);
}

void print_list(int l[],int n)


{
int i;
for(i=0;i<n;i++)
printf("%d\t",l[i]);
}
/*main function*/
void main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;
clrscr();
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);
if(ch<=2 & ch>0)
{
printf("\nEnter the number of elements : ");
scanf("%d",&num);

Department of CSE CS307PC-Data Structures Lab


read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);

switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1){
printf("Element is not found");
}
else
{
printf("Element is found at %d position",pos);
}
getch();
break;
case 2:printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();

Department of CSE CS307PC-Data Structures Lab


}
Output:
======================================================
MENU
=====================================================
[1] Binary Search using Recursion method
[2] Binary Search using Non-Recursion method

Enter your Choice:1

Enter the number of elements : 5


Enter the elements:
12
03
52
68
98

Elements present in the list are:


12 3 52 68 98

Enter the element you want to search:


68
Element is found at 3 position

8. Write a program to implement the tree traversal methods.

Department of CSE CS307PC-Data Structures Lab


#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
void inorder(struct node* root){
if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}
void preorder(struct node* root){
if(root == NULL) return;
printf("%d ->", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node* root) {
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}
struct node* createNode(value){

Department of CSE CS307PC-Data Structures Lab


struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node *root, int value) {
root->left = createNode(value);
return root->left;}
struct node* insertRight(struct node *root, int value){
root->right = createNode(value);
return root->right;
}
int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
printf("Inorder traversal \n");
inorder(root);
printf("\nPreorder traversal \n");
preorder(root);
printf("\nPostorder traversal \n");
postorder(root);}
The output of the code will be

Department of CSE CS307PC-Data Structures Lab


Inorder traversal
5 ->12 ->6 ->1 ->9 ->
Preorder traversal
1 ->12 ->5 ->6 ->9 ->
Postorder traversal
5 ->6 ->12 ->9 ->1 ->

Department of CSE CS307PC-Data Structures Lab


9. Write a program to implement the graph traversal methods.
#include<stdio.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();
void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}

Department of CSE CS307PC-Data Structures Lab


}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
do
{
for(i=1;i<=n;i++)
vis[i]=0;
printf("\nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S");
printf("\nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);
switch(ch)
{
case 1:bfs(s,n);
break;
case 2:

Department of CSE CS307PC-Data Structures Lab


dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
}
//**************BFS(breadth-first search) code**************//
void bfs(int s,int n)
{
int p,i;
add(s);
vis[s]=1;
p=delete();
if(p!=0)
printf(" %d",p);
while(p!=0)
{
for(i=1;i<=n;i++)
if((a[p][i]!=0)&&(vis[i]==0))
{
add(i);
vis[i]=1;
}
p=delete();

Department of CSE CS307PC-Data Structures Lab


if(p!=0)
printf(" %d ",p);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
bfs(i,n);
}
void add(int item)
{
if(rear==19)
printf("QUEUE FULL");
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
q[++rear]=item;
}
}
int delete()
{
int k;
if((front>rear)||(front==-1))

Department of CSE CS307PC-Data Structures Lab


return(0);
else
{
k=q[front++];
return(k);
}
}
//***************DFS(depth-first search) code******************//
void dfs(int s,int n)
{
int i,k;
push(s);
vis[s]=1;
k=pop();
if(k!=0)
printf(" %d ",k);
while(k!=0)
{
for(i=1;i<=n;i++)
if((a[k][i]!=0)&&(vis[i]==0))
{
push(i);
vis[i]=1;
}
k=pop();
if(k!=0)

Department of CSE CS307PC-Data Structures Lab


printf(" %d ",k);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
dfs(i,n);
}
void push(int item)
{
if(top==19)
printf("Stack overflow ");
else
stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return(0);
else
{
k=stack[top--];
return(k);
}
}
Output:
ENTER THE NUMBER VERTICES 3

Department of CSE CS307PC-Data Structures Lab


ENTER 1 IF 1 HAS A NODE WITH 1 ELSE 0 1
ENTER 1 IF 1 HAS A NODE WITH 2 ELSE 0 1
ENTER 1 IF 1 HAS A NODE WITH 3 ELSE 0 0
ENTER 1 IF 2 HAS A NODE WITH 1 ELSE 0 1
ENTER 1 IF 2 HAS A NODE WITH 2 ELSE 0 0
ENTER 1 IF 2 HAS A NODE WITH 3 ELSE 0 1
ENTER 1 IF 3 HAS A NODE WITH 1 ELSE 0 0
ENTER 1 IF 3 HAS A NODE WITH 2 ELSE 0 1
ENTER 1 IF 3 HAS A NODE WITH 3 ELSE 0 1
THE ADJACENCY MATRIX IS
110
101
011
MENU
1. B.F.S
2. D.F.S
ENTER YOUR CHOICE2
ENTER THE SOURCE VERTEX: 2
2 3 1 DO U WANT TO CONTINUE(Y/N) ?

Department of CSE CS307PC-Data Structures Lab

Vous aimerez peut-être aussi