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

Data Structure LAB Codes

Uploaded by

hjjhjkijnjkk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data Structure LAB Codes

Uploaded by

hjjhjkijnjkk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

DATA STRUCTURES

**MENU OPTIONS**

#include <stdio.h>
#include <stdlib.h>

void addition(int a, int b) {


printf("The Addition of %d and %d is %d\n", a, b, a + b);
}

void subtraction(int a, int b) {


printf("The Subtraction of %d and %d is %d\n", a, b, a - b);
}

void multiplication(int a, int b) {


printf("The Multiplication of %d and %d is %d\n", a, b, a * b);
}

division(int a, int b) {
if (b != 0) {
printf("The Division of %d by %d is %d\n", a, b, a / b);
} else {
printf("Division by zero is not allowed.\n");
}
}

int main() {
int a, b, c;

do {
printf("MENU OPTIONS\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\nPlease enter your Option: ");
scanf("%d", &c);
printf("\nYour Option is %d\n", c);

if (c >= 1 && c <= 4) {


printf("Enter the value of a:\n");
scanf("%d", &a);
printf("Enter the value of b:\n");
scanf("%d", &b);
}

switch (c) {
case 1:
addition(a, b);
break;
case 2:
subtraction(a, b);
break;
case 3:
multiplication(a, b);
break;
case 4:
division(a, b);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Enter a number between 1 and 5\n");
}
} while (c != 5);

return 0;
}

**STACK USING ARRAYS**

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int stack[MAX];
int top=-1,data,i;
void insertion(); //function declaration
void deletion();
void display();
void isEmpty();
void isFull();

void insertion()
{
if(top==MAX-1)
{
printf("Stack overflow \n");
}
else
{
top=top+1;
printf("Enter data to insert \n");
scanf("%d",&data);
stack[top]=data;
printf("data inserted successfully \n");
}
}
void deletion()
{

if(top ==-1)
{
printf("statck is empty \n");
}
else
{
top = top-1;
printf("element deleted successfully \n");
}
}
void display()
{

if(top==-1)
{
printf("Stack is empty \n");
}
printf("Stack Data \n");
for(i=top;i>=0;i--)
{
printf("%d \n",stack[i]);
}
}
void isEmpty()
{
if(top==-1)
{
printf("Statck is empty \n");
}
else
{
printf("stack contains data \n");

}
}
void isFull()
{
if(top==MAX-1)
{
printf("stack is full \n");
}
else
{
printf("stack is having space \n");
}
}

int main()
{

int ch;
do
{
printf("Stack operations \n");
printf("1.insertion \n");
printf("2.deletion \n");
printf("3.display \n");
printf("4.isEmpty \n");
printf("5.isFull \n");
printf("6.Quit \n");
printf("please Enter your choice \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertion(); //function call
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
isFull();
break;
case 6:
exit(0);
break;
default:
printf("Enter between 1-5 \n");
break;
}
}while(ch!=6);
return 10;
}

**STACK USING SINGLE LINKED LIST**

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL;
void insertion();
void insertion()
{
int value;
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter the data to insert:");
scanf("%d",&value);
temp->data=value;
temp->next=top;
top=temp;
printf("Data inserted successfully\n");
}
void deletion();
void deletion()
{
struct node *temp=top;
if(top==NULL)
{
printf("Stack is empty\n");
}
else
{
top=temp->next;
temp->next=NULL;
free(temp);
printf("Element deleted successfully\n");
}
}
void display();
void display()
{
struct node *temp=top;
if(top==NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Stack data\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}
void length();
void length()
{
int count=0;
struct node *temp=top;
if(top==NULL)
{
printf("Stack is empty");
}
else
{
while(temp!=NULL)
{
count+=1;
temp=temp->next;
}
printf("Number of elements %d\n",count);
}
}
int main()
{
int ch;
do
{
printf("Stack implementation using Single linked list:\n");
printf("Stack operation:\n");
printf("1.Insertion\n2.Deletion\n3.Display\n4.Length\n5.Exit\n");
printf("Enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
length();
break;
case 5:
exit(0);
default:
printf("Enter between 1-5");
break;
}
}while(ch!=4);
return 0;
}

**QUEUE USING ARRAYS**

#include <stdio.h>
#include <stdlib.h> //exit function

#define MAX 5
int front = -1, rear = -1, data,i;
int queue[MAX];

void insertion();
void deletion();
void display();

void insertion() {
if (rear == MAX - 1)
{
printf("Queue overflow\n");
}
else if (front==-1 && rear ==-1)
{
front = front+1;
rear = rear+1;
printf("Enter data \n");
scanf("%d",&data);
queue[rear]=data;
printf("Data inserted successfully\n");
}
else
{
rear=rear+1;
printf("Enter data for insertion\n");
scanf("%d",&data);
queue[rear]=data;
printf("Data inserted successfulyy\n");
}
}

void deletion()
{
if(front==-1 && rear==-1)
{
printf("queue is empty\n");
}
else
{
front=front+1;
printf("Data deleted successfully\n");
}
if(front>rear)
{
front=-1;
rear=-1;
}
}
void display()
{
if(front==-1)
{
printf("Queue is empty\n");
}
else
{
printf("Queue data\n");
for(i=front;i<=rear;i++)
{
printf("%d\n",queue[i]);
}
}
}

int main() {
int ch;
do {
printf("Queue Operations\n");
printf("1. Insertion\n");
printf("2. Deletion\n");
printf("3. Display\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch (ch) {
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Enter between 1-4\n");
break;
}
} while (ch != 4);
return 0;
}

**QUEUE USING SINGLE LINKED LIST**

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front=NULL,*rear=NULL;

void insertion();
void deletion();
void display();
void length();

void insertion()
{
int value;
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter data to insert: \n");
scanf("%d",&value);
temp->data=value;
temp->next=NULL;
if(front == NULL && rear == NULL)
{
front=temp;
rear=temp;
printf("element inserted successfully\n");
}
else
{
rear->next=temp;
rear=temp;
printf("element inserted successfully\n");
}
}

void deletion()
{
struct node *temp=front;
if(front==NULL)
{
printf("queue is empty\n");
}
else
{
front=front->next;
if(front==NULL)
{
rear=NULL;
}
temp->next=NULL;
free(temp);
printf("element deleted succesfully\n");
}
}

void display()
{
struct node *temp=front;
if(front==NULL && rear==NULL)
{
printf("Queue is empty\n");
}
else
{
printf("QUEUE DATA\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}

void length()
{
int count=0;
struct node *temp=front;
while(temp!=NULL)
{
count=count+1;
temp=temp->next;
}
printf("no of elements = %d\n",count);
}

int main()
{
int ch;
do
{
printf("QUEUE OPERATIONS\n");
printf("1.insertion\n");
printf("2.deletion\n");
printf("3.display\n");
printf("4.length\n");
printf("5.Quit\n");
printf("Enter your choise: \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
length();
break;
case 5:
exit(0);
default:
printf("Enter between 1-5");
break;
}
}while(ch!=5);
return 0;
}

**SINGLE LINKED LIST OPERATIONS**

#include <stdio.h>
#include <stdlib.h>
int value;

struct node
{
int data,p,value;
struct node *next;
};
struct node *root=NULL;
void insert_at_begin();
void display();
void insert_at_end();
int length();
void insert_at_after_specified_location();
void delet_at_begin();
void delet_at_end();
void delet_at_specified_location();

void delet_at_specified_location()
{
int loc, i = 1;
struct node *p = root;
struct node *q = NULL;

printf("Enter the location number: \n");


scanf("%d", &loc);

int len = length();


if (loc > len || loc <= 0)
{
printf("Invalid location number. List contains %d elements.\n", len);
return;
}
if (loc == 1)
{
root = p->next;
p->next = NULL;
free(p);
printf("Element deleted successfully\n");
return;
}
while (i < loc - 1)
{
p = p->next;
i++;
}
q = p->next;
p->next = q->next;
q->next = NULL;
free(q);
printf("Element deleted successfully\n");
}

void delet_at_end()
{
struct node *temp=root, *pre=NULL;
if(root==NULL)
{
printf("linked list is empty\n");
}
else
{
while(temp->next!=NULL)
{
pre=temp;
temp=temp->next;
}
pre->next=NULL;
free(temp);
printf("element deleted successfully\n");
}
}

void delet_at_begin()
{
struct node *temp=root;
if(root==NULL)
{
printf("linked list is empty\n");
}
else
{
root=temp->next;
temp->next=NULL;
free(temp);
printf("element deleted successfully\n");
}
}

void insert_at_after_specified_location()
{
int loc,len,i=1;
struct node *p=root;
struct node *temp=(struct node *)malloc(sizeof(struct node));
printf("enter data to insert \n");
scanf("%d",&value);
temp->data=value;
temp->next=NULL;
printf("enter location number\n");
scanf("%d",&loc);
len=length();
if(loc>len)
{
printf("invalid location number SLL contains %d elements\n",len);
}
else
{
while(i<loc)
{
p=p->next;
i=i+1;
}
temp->next=p->next;
p->next=temp;
printf("element inserted successfully\n");
}
}

int length()
{
int count = 0;
struct node* temp = root;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("Number of elements = %d\n",count);
return count;
}

void insert_at_end()
{
struct node *temp =(struct node*) malloc(sizeof(struct node*));
struct node *p = root;
printf("enter data to insert\n");
scanf("%d",&value);
temp->data=value;
temp->next=NULL;
if(root==NULL)
{
root=temp;
}
else
{
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
printf("element inserted succesfully\n");
}

void display()
{
struct node *temp=root;
if(temp==NULL)
{
printf("empty\n");
}
else
{
printf("SSL DATA\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}

void insert_at_begin()
{
struct node *temp =(struct node *) malloc(sizeof(struct node));
printf("enter data to insert\n");
scanf("%d",&value);
temp->data=value;
temp->next=NULL;
if(root == NULL)
{
root=temp;
printf("element inserted successfully\n");
}
else
{
temp->next=root;
root=temp;
printf("elements inserted successfully\n");
}
}
int main()
{
int ch;
do
{
printf("SINGLE LINKED LIST OPERATIONS\n");
printf("1.insert_at_begin\n");
printf("2.insert_at_end\n");
printf("3.insert_at_after_specified_location\n");
printf("4.Length\n");
printf("5.display\n");
printf("6.delet_at_begin\n");
printf("7.delet_at_end\n");
printf("8.delet_at_specified_location\n");
printf("9.Quit\n");
printf("Enter your choise\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_at_begin();
break ;
case 2:
insert_at_end();
break;
case 3:
insert_at_after_specified_location();
break;
case 4:
length();
break;
case 5:
display();
break;
case 6:
delet_at_begin();
break;
case 7:
delet_at_end();
break;
case 8:
delet_at_specified_location();
break;
case 9:
exit(0);
default:
printf("enter between 1-9\n");
break;
}
}while(ch!=9);
return 0;
}
**CIRCULAR LINKED LIST**

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *tail;

void insert_at_begin();
void insert_at_end();
void insert_at_specified_location();
void display();
int length();
void delet_at_begin();
void delet_at_end();
void delete_at_specified_location();

void insert_at_begin()
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
int value;
printf("Enter data to insert\n");
scanf("%d",&value);
temp->data=value;
if(tail == NULL)
{
tail=temp;
temp->next=tail;
printf("Data inserted successfully\n");
}
else
{
temp->next=tail->next;
tail->next=temp;
printf("Data inserted successfully\n");
}
}

void insert_at_end()
{
int value;
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter data to insert\n");
scanf("%d",&value);
temp->data=value;
if(tail == NULL)
{
tail=temp;
temp->next=tail;
printf("Data inserted successfully\n");
}
else
{
temp->next=tail->next;
tail->next=temp;
tail=temp;
printf("Data inserted successfully\n");
}
}

void insert_at_specified_location()
{
int len,loc;
printf("Enter location number\n");
scanf("%d",&loc);
len=length();
if(loc==1)
{
insert_at_begin();
}
else if(loc==len+1)
{
insert_at_end();
}
else if(loc>1 && loc<=len)
{
struct node *p;
int count=1;
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter data to inssert\n");
scanf("%d",&temp->data);
p=tail->next;
while(count<loc-1)
{
p=p->next;
count=count+1;
}
temp->next=p->next;
p->next=temp;
printf("Data inserted successfully\n");
}
else
{
printf("Invalid location number\n");
}
}

void display()
{
struct node *temp=tail->next;
if(tail == NULL)
{
printf("Circular linked list is empty\n");
}
else
{
printf("Circular Linked List Data\n");
do
{
printf("%d\n",temp->data);
temp=temp->next;
}while(temp!=tail->next);
}
}

int length()
{
int count=0;
if(tail!=NULL)
{
struct node *temp=tail->next;
do
{
count=count+1;
temp=temp->next;
}while(temp!=tail->next);
}
printf("no of elements = %d\n",count);
return count;
}

void delet_at_begin()
{
struct node *temp=tail->next;
if(tail==NULL)
{
printf("CLL is empty\n");
}
else if(tail==tail->next)
{
tail=NULL;
temp->next=NULL;
free(temp);
printf("Element deleted successfully\n");
}
else
{
tail->next=temp->next;
temp->next=NULL;
free(temp);
printf("Element deleted successfully\n");
}
}

void delet_at_end()
{
if (tail == NULL)
{
printf("CLL is empty\n");
return;
}
struct node *temp = tail;
if (tail->next == tail)
{
tail = NULL;
free(temp);
printf("Element deleted successfully\n");
}
else {
struct node *p = tail->next;

while (p->next != tail)


{
p = p->next;
}
p->next = tail->next;
free(tail);
tail = p;
printf("Element deleted successfully\n");
}
}

void delet_at_specified_location()
{
int loc, len, count = 1;
struct node *temp, *n;
printf("Enter the location number: ");
scanf("%d", &loc);
len = length();
if (tail == NULL)
{
printf("CLL is empty\n");
return;
}
if (loc <= 0 || loc > len)
{
printf("Invalid location. List has %d elements.\n", len);
return;
}
if (loc == 1)
{
delet_at_begin();
}
else if (loc == len)
{
delet_at_end();
}
else if (loc > 1 && loc < len)
{
temp = tail->next;
while (count < loc - 1)
{
temp = temp->next;
count++;
}
n = temp->next;
temp->next = n->next;
n->next = NULL;
free(n);
printf("Element Deleted Successfully\n");
}
}

int main()
{
int ch;
do
{
printf("\nCIRCULAR LINKED LIST\n");
printf("1.insert_at_begin\n");
printf("2.insert_at_end\n");
printf("3.insert_at_specified_location\n");
printf("4.display\n");
printf("5.length\n");
printf("6.delet_at_begin\n");
printf("7.delet_at_end\n");
printf("8.delet_at_specified_location\n");
printf("9.Quit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_at_begin();
break;
case 2:
insert_at_end();
break;
case 3:
insert_at_specified_location();
break;
case 4:
display();
break;
case 5:
length();
break;
case 6:
delet_at_begin();
break;
case 7:
delet_at_end();
break;
case 8:
delet_at_specified_location();
break;
case 9:
exit(0);
default:
printf("Enter between 1-9\n");
break;
}
}while(ch!=9);
return 0;
}
**DOUBLE LINKED LIST**

#include <stdio.h>
#include <stdlib.h>

struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL, *tail=NULL;

void insert_at_begin();
void insert_at_end();
void insert_at_specified_location();
void display();
int length();
void delet_at_begin();
void delet_at_end();
void delet_at_specified_location();

void insert_at_begin()
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter data to insert\n");
scanf("%d",&temp->data);
temp->next=NULL;
temp->prev=NULL;
if(head==NULL)
{
head=tail=temp;
printf("Element inserted successfully\n");
}
else
{
temp->next=head;
head->prev=temp;
head=temp;
printf("Element inserted successfully\n");
}
}

void insert_at_end()
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter data to insert\n");
scanf("%d",&temp->data);
temp->prev=NULL;
temp->next=NULL;
if(head==NULL)
{
head=tail=temp;
printf("Data inserted successfully\n");
}
else
{
tail->next=temp;
temp->prev=tail;
tail=temp;
printf("data inserted successfully\n");
}
}

void insert_at_specified_location()
{
int loc,len;
printf("Enter location Number: ");
scanf("%d",&loc);
len=length();
if(loc==1)
{
insert_at_begin();
}
else if(loc==len+1)
{
insert_at_end();
}
else if(loc>1 && loc<=len)
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
struct node *n=head;
int count=1;
printf("Enter Data to Insert: ");
scanf("%d",&temp->data);
temp->next=NULL;
temp->prev=NULL;
while(count<loc-1)
{
n=n->next;
count++;
}
temp->next=n->next;
n->next->prev=temp;
temp->prev=n;
n->next=temp;
printf("Element inserted Successfully\n");
}
else
{
printf("Invalid Location\n");
}
}

void display()
{
struct node *temp=head;
if(head==NULL)
{
printf("Double linked list is empty\n");
}
else
{
printf("DOUBLE LINKED LIST DATA\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}

int length()
{
int count=0;
if(head==NULL)
{
printf("Double linked list is empty\n");
}
else
{
struct node *temp=head;
while(temp!=NULL)
{
count+=1;
temp=temp->next;
}
printf("Total number of elements are %d\n",count);
}
return count;
}

void delet_at_begin()
{
struct node *temp = head;
if (head == NULL)
{
printf("Doubly linked list is empty\n");
return;
}
if (temp->next == NULL)
{
head = tail = NULL;
free(temp);
printf("Element deleted successfully\n");
}
else
{
head = temp->next;
head->prev = NULL;
free(temp);
printf("Element deleted successfully\n");
}
}

void delet_at_end()
{
struct node *temp = tail;
if (head == NULL)
{
printf("Doubly linked list is empty\n");
return;
}
if (temp->prev == NULL)
{
head = tail = NULL;
free(temp);
printf("Element deleted successfully\n");
}
else {
tail = temp->prev;
tail->next = NULL;
free(temp);
printf("Element deleted successfully\n");
}
}

void delet_at_specified_location()
{
int loc, len, count = 1;
printf("Enter location number: \n");
scanf("%d", &loc);
len = length();
if (loc <= 0 || loc > len)
{
printf("Invalid location number\n");
return;
}
if (loc == 1) {
delet_at_begin();
}
else if (loc == len)
{
delet_at_end();
}
else
{
struct node *temp = head;
struct node *n;
while (count < loc - 1)
{
temp = temp->next;
count++;
}
n = temp->next;
temp->next = n->next;
if (n->next != NULL)
{
n->next->prev = temp;
}
n->prev = NULL;
n->next = NULL;
free(n);
printf("Element deleted successfully\n");
}
}

int main()
{
int a;
do
{
printf("\nDOULBE LINKED LIST OPERATIONS\n");
printf("1.insert at begin\n");
printf("2.insert at end\n");
printf("3.insert at specified location\n");
printf("4.display\n");
printf("5.length\n");
printf("6.delet at begin\n");
printf("7.delet at end\n");
printf("8.delet at specified location\n");
printf("9.Quit\n");
printf("Enter your choice\n");
scanf("%d",&a);
switch(a)
{
case 1:
insert_at_begin();
break;
case 2:
insert_at_end();
break;
case 3:
insert_at_specified_location();
break;
case 4:
display();
break;
case 5:
length();
break;
case 6:
delet_at_begin();
break;
case 7:
delet_at_end();
break;
case 8:
delet_at_specified_location();
break;
case 9:
exit(0);
default:
printf("Enter between 1-9\n");
break;
}
}while(a!=9);
return 0;
}

You might also like