0% found this document useful (0 votes)
50 views

Data Structure Lab File

The document contains program code for various operations on queues and linked lists in C language. It includes functions to insert, delete and display elements in a queue. It also includes functions to insert nodes at the beginning, end or at a specified position in a linked list. The code samples demonstrate how to implement queues using arrays and linked lists for operations like insertion, deletion, traversal etc.

Uploaded by

jayant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Data Structure Lab File

The document contains program code for various operations on queues and linked lists in C language. It includes functions to insert, delete and display elements in a queue. It also includes functions to insert nodes at the beginning, end or at a specified position in a linked list. The code samples demonstrate how to implement queues using arrays and linked lists for operations like insertion, deletion, traversal etc.

Uploaded by

jayant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

PROGRAM -7

AIM-QUEUE AND ITS OPERATION

PROGRAM :
#include<stdio.h>
#include<conio.h>
#define max 5
int q[10],front=0,rear=-1;
void main()
{
int ch;
void insert();
void delet();
void display();
clrscr();
printf("\nQueue operations\n");

printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");
}
}
}

void insert()
{
int x;
if(rear==max-1)
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
q[++rear]=x;
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit();
}
a=q[front++];
printf("Deleted element is:%d\n",a);
if(front>rear)
{
front=0;
rear=-1;
}
}

void display()
{
int i;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
exit();
}
for(i=front;i<=rear;i++)
printf("\t%d",q[i]);
printf("\n");
}
getch();
PROGRAM -8
AIM - BINARY TREE IMPLEMENTATION USING ARRAY

PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10],flr,choice,i,n,want;
clrscr();
for(i=0;i<10;i++)
{
printf("enter a element:");
scanf("%d",&a[i]);
}
do
{
printf("which element you want to traverse:");
scanf("%d",&choice);
for(i=0;i<10;i++)
{
if(a[i]==choice)
{
n=i;
break;
}
}
printf("in this %d element you want
to press",choice);
printf(" \n 1. for parent node\n 2.for left
child \n3. for right child\n");
scanf("%d",&want); switch(want)
{
case 1:
flr = floor((n-
1)/2);
for(i=0;i<10;i++)
{
if(i==flr)
{
printf("parent node is %d",a[i]);
}
}
break;
case 2:
flr = 2*n+1;
printf("left node is %d",a[flr]);
break;
case 3:
flr= 2*n+2;
printf("right node is %d",a[flr]);
break;
default:
printf("wrong choice:");
break;
}
printf("\n press 1 for continue and zero to exit");
scanf("%d",&choice);
}
while(choice!=0);
getch();
}
PROGRAM -9
AIM - LINEAR SEARCH USING ARRAY

PROGRAM :
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10], i, item;
clrscr();
printf("\nEnter SEVEN elements of an array:\n");
for (i=0; i<=6; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if(i > 9)
printf("\nItem does not exist.");
getch();
}
PROGRAM -10
AIM - BINARY SEARCH USING ARRAY

PROGRAM :
#include <stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search,array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search); first = 0;
last = n – 1;
middle = (first+last)/2;
while (first <= last)
{
if (array[middle] < search)
first = middle + 1;
else if (array[middle]==search)
{
printf("%d found at location %d.\n",search,middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the
list.\n",search);
return 0;
getch();
}
PROGRAM -11
AIM - BUBBLE SORT

PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5];
int i,j,swap;
clrscr();

printf("enter the no. of elements in the array:");


for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]>arr[j])
{
swap=arr[j];
arr[j]=arr[i];
arr[i]=swap;
}
}
}

printf("sorted list is\n");


for(j=0;j<5;j++)
{
printf("%d ",arr[j]);
}
getch();
}
AIM - INSERTION SORT

PROGRAM :
#include<stdio.h>
#include<conio.h>
void InsertionSort(int a[], int n)
{
int j, p;
int tmp;
for(p = 1; p < n; p++)
{
tmp = a[p];
for(j = p; j > 0 && a[j-1] > tmp; j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
int main()
{
int i, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
InsertionSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
AIM - SELECTION SORT

PROGRAM :
#include<stdio.h>
#include<conio.h>
int main()
{
int data[100],i,n,steps,temp;
printf("Enter the number of elements to be sorted:
");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(steps=0;steps<n;++steps)
for(i=steps+1;i<n;++i)
{
if(data[steps]>data[i])
{
temp=data[steps];
data[steps]=data[i];
data[i]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
PROGRAM -12
AIM – INSERTION IN LINK LIST
1>INSERTION AT THE BEGINNING OF LIST
2>INSERTION AT THE END OF LIST
3>INSERTION AT THE SPECIFIED POSITION

PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct node_t {
int data;
struct node_t *next;
} Node;

Node * insert_top(int, Node *);


Node * insert_bottom(int, Node *);
Node * insert_after(int, int, Node *);
Node * insert_before(int, int, Node *);
void print(Node *);
int count(Node *);

Node * insert_top(int num, Node *head) {


Node *new_node;
new_node = (Node *) malloc(sizeof(Node));
new_node->data = num;
new_node->next= head;
head = new_node;
return head;
}

Node * insert_bottom(int num, Node *head) {


Node *current_node = head;
Node *new_node;
while ( current_node != NULL && current_node->next !=
NULL)
{
current_node = current_node->next;
}
new_node = (Node *) malloc(sizeof(Node));
new_node->data = num;
new_node->next= NULL;
if (current_node != NULL)
current_node->next = new_node;
else
head = new_node;
return head;
}

Node * insert_after(int num, int prev_num, Node *head)


{
Node *current_node = head;
Node *new_node;
while ( current_node->data != prev_num) {
current_node = current_node->next;
}
new_node = (Node *) malloc(sizeof(Node));
new_node->data = num;
new_node->next= current_node->next;
current_node->next = new_node;
return head;
}

Node * insert_before(int num, int next_num, Node*head)


{
Node *current_node = head;
Node *new_node;
while ( current_node->next->data != next_num) {
current_node = current_node->next;
}
new_node = (Node *) malloc(sizeof(Node));
new_node->data = num;
new_node->next= current_node->next;
current_node->next = new_node;
return head;
}

void print(Node *head) {


Node *current_node = head;
while ( current_node != NULL) {
printf("%d ", current_node->data);
current_node = current_node->next;
}
}
int main()
{
Node *head = NULL;
int num, prev_num, next_num;
int option;
char * temp;
char ch;
/* Display Menu */
while(1) {
printf("\n***********************************\n");
printf("\n * Linked list operations: *\n");
printf("\n * 1. Insert at the top of list *\n");
printf("\n * 2. Insert at bottom of list *\n");
printf("\n * 3. Insert after an element *\n");
printf("\n * 4. Insert before an element *\n");
printf("\n * 5. Show all elements *\n");
printf("\n * 6. Quit *\n");
printf("\n***********************************\n");
printf("\n Choose an option [1-5] : ");
if (scanf("%d", &option) != 1) {
printf(" *Error: Invalid input. Try
again.\n");
scanf("%s", &temp); /*clear input buffer */
continue;
}

switch (option) {
case 1: /* Add to top*/
printf(" Enter a number to insert : ");
if (scanf("%d", &num) != 1) {
printf(" *Error: Invalid input.\n");
scanf("%s", &temp);
continue;
}
head = insert_top(num, head);
printf("Number %d added to the top of the
list", num);
printf("\nPress any key to continue...");
getch();
break;
case 2:
printf(" Enter a number to insert : ");
if (scanf("%d", &num) != 1) {
printf(" *Error: Invalid input. \n");
scanf("%s", &temp);
continue;
}
head = insert_bottom(num, head);
printf("%d added to the bottom of the list",
num);
printf("\nPress any key to continue...");
getch();
break;

case 3:
printf(" Enter a number to insert : ");
if (scanf("%d", &num) != 1) {
printf(" *Error: Invalid input.\n");
scanf("%s", &temp);
continue;
}

printf(" After which number do you want to


insert : ");
if (scanf("%d", &prev_num) != 1) {
printf(" *Error: Invalid input.\n");
scanf("%s", &temp);
continue;
}
if (head != NULL) {
head = insert_after(num, prev_num,
head);
printf("%d is inserted after %d", num,
prev_num);
}else {
printf("The list is empty", num,
prev_num);
}
printf("\nPress any key to
continue...");
getch();
break;
case 4: /* Insert Before */
printf(" Enter a number to insert : ");
if (scanf("%d", &num) != 1) {
printf(" *Error: Invalid input. \n");
scanf("%s", &temp);
continue;
}

printf(" Before which number do you want to


insert : ");
if (scanf("%d", &prev_num) != 1) {
printf(" *Error: Invalid input.\n");
scanf("%s", &temp);
continue;
}

if (head != NULL) {
head = insert_before(num, prev_num,head);
printf("Number %d inserted before %d",
num, prev_num);
}else {
printf("The list is empty", num,
prev_num);
}
printf("\nPress any key to continue...");
getch();
break;

case 5: /* Show all elements */


printf("\nElements in the list: \n [ ");
print(head);
printf("]\n\nPress any key to continue...");
getch();
break;

case 6: /* Exit */
return(0);
break;
default:
printf("Invalid Option. Please Try again.");
getch();
PROGRAM -13
AIM – DELETION FROM LINK LIST
1>DELETION FROM THE FRONT OF LIST
2>DELETION FROM THE END OF LIST
3>DELETION FROM THE SPECIFIED POSITION

PROGRAM :
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
struct node *header, *ptr, *ptr1, *temp;

void insert_end();
void delete_front();
void delete_end();
void delete_any();
void display();

void main()
{
int choice;
int cont = 1;
header = (struct node *) malloc(sizeof(struct node));
clrscr();

header->data = NULL;
header->link = NULL;

while(cont == 1)
{
printf("\n1. Insert at end\n");
printf("\n2. Delete from front\n");
printf("\n3. Delete from end\n");
printf("\n4. Delete from anywhere\n");
printf("\n5. Display linked list\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert_end();
break;
case 2:
delete_front();
break;
case 3:
delete_end();
break;
case 4:
delete_any();
break;
case 5:
display();
break;
}

printf("\n\nDo you want to continue? (1 / 0): ");


scanf("%d", &cont);
}

getch();
}

void insert_end()
{
int data_value;

printf("\nEnter data of the node: ");


scanf("%d", &data_value);
temp = (struct node *) malloc(sizeof(struct node));

ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}

void delete_front()
{
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion not
possible.\n");
}
else
{
ptr = header->link;
header->link= ptr->link;
free(ptr);
printf("\nNode deleted from the front.\n");
}
}

void delete_end()
{
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion
not possible.\n");
}
else
{
ptr = header;
while(ptr->link != NULL)
{
ptr1 = ptr;
ptr = ptr->link;
}
ptr1->link = ptr->link;
free(ptr);
printf("\nNode deleted from the end.\n");
}
}

void delete_any()
{
int key;
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion
not possible.\n");
}
else
{
printf("\nEnter the data of the node to be
deleted: ");
scanf("%d", &key);
ptr = header;
while((ptr->link != NULL) && (ptr->data != key))
{
ptr1 = ptr;
ptr = ptr->link;
}
if(ptr->data == key)
{
ptr1->link = ptr->link;
free(ptr);
printf("\nNode with data %d deleted.\n", key);
}
else
{
printf("\nValue %d not found. Deletion not
possible.\n", key);
}
}
}

void display()
{
printf("\nContents of the linked list are:\n");
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
printf("%d ", ptr->data);
}
}

You might also like