0% found this document useful (0 votes)
1 views53 pages

DS Lab Mannual

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)
1 views53 pages

DS Lab Mannual

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/ 53

PDKV CSE Reg. no.

:411620104017

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

PROGRAM:

//SINGLY LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void create();

void insert_first();

void insert_last();

void insert_mid();

void delete_first();

void delete_last();

void delete_mid();

void display();

void count();

struct LL

int roll;

struct LL*next;

};

typedef struct LL node;

node*head=NULL;

void main()

int ch;

while(1)

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

printf("\n main menu");

printf("\n 1.create \n 2.display \n 3.insert_first \n 4.insert_last \n 5.insert_mid \n 6.delete_first \n


7.delete_last \n 8.delete_mid \n 9.count \n 10.exit");

printf("\n enter the choice");

scanf("%d",&ch);

switch(ch)

case 1:

create();

break;

case 2:

display();

break;

case 3:

insert_first();

break;

case 4:

insert_mid();

break;

case 5:

insert_last();

break;

case 6:

delete_first();

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

break;

case 7:

delete_last();

break;

case 8:

delete_mid();

break;

case 9:

count();

break;

case 10:

exit(0);

default:

printf("enter the correct choice:");

break;

void create()

//do

//{

node*temp,*newnode;

newnode=(node*)malloc(sizeof(node));

if(newnode==NULL)

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

printf("\n out of memory");

exit(0);

printf("\n enter the value:");

scanf("%d",&newnode->roll);

newnode->next=NULL;

if(head==NULL)

head=newnode;

else

temp=head;

while(temp->next!=NULL)

temp=temp->next;

temp->next=newnode;

//printf("\n enter choice:");

//scanf("%c",&c);

//}while(c=='y' || c=='Y');

void display()

node*temp;

if(head==NULL)

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

printf("\n SLL is empty");

return;

else

temp=head;

printf("\n contents are");

while(temp!=NULL)

printf("\n %d",temp->roll);

temp=temp->next;

}}

void count()

int count=0;

node*temp;

if(head==NULL)

printf("\n SLL is empty");

return;

else

temp=head;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

while(temp!=NULL)

count++;

temp=temp->next;

printf("\n total number of nodes in SLL is %d",count);

void insert_first()

node *newnode;

newnode=(node*)malloc(sizeof(node));

printf("\n enter the value:");

scanf("%d",&newnode->roll);

if(head==NULL)

head=newnode;

newnode->next=NULL;

else

newnode->next=head;

head=newnode;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

void insert_last()

node*newnode,*temp;

printf("enter the rollno:");

newnode=(node*)malloc(sizeof(node));

scanf("%d",&newnode->roll);

newnode->next=NULL;

if(head==NULL)

head=newnode;

else

temp=head;

while(temp->next!=NULL)

temp=temp->next;

temp->next=newnode;

void insert_mid()

node*newnode,*temp;

int ro,found=0;

newnode=(node*)malloc(sizeof(node));

printf("\m enter the rollno:");

scanf("%d",&newnode->roll);

printf("\n enter rollno after which insertion has to be done:");

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

scanf("%d",&ro);

newnode->next=NULL;

if(head==NULL)

head=newnode;

else

temp=head;

while(temp!=NULL)

if(temp->roll==ro)

found=1;

newnode->next=temp->next;

temp->next=newnode;

return;

else

temp=temp->next;

if(found==0)

printf("\n rollno not available");

void delete_first()

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

node*temp;

if(head==NULL)

printf("\n LL is empty");

return;

else

temp=head;

head=head->next;

printf("\n deleted element is %d",temp->roll);

free(temp);

void delete_last()

node*temp,*last;

if(head==NULL)

printf("\n SLL is empty");

return;

else if(head->next==NULL)

temp=head;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

printf("\n deleted is %d",temp->roll);

head=NULL;

free(temp);

else

last=head;

while(last->next!=NULL)

temp=last;

last=last->next;

temp->next=NULL;

printf("\n deleted element is %d",last->roll);

free(last);

void delete_mid()

node*curr=head,*prev;

int ro;

if(head==NULL)

printf("SLL is empty");

return;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

printf("\n enter rollno to delete:");

scanf("%d",&ro);

if(curr->roll==ro)

head=curr->next;

printf("\n deleted element is %d",curr->roll);

free(curr);

return;

else

while(curr!=NULL && curr->roll!=ro)

prev=curr;

curr=curr->next;

if(curr==NULL)

printf("\n roll no not available");

return;

prev->next=curr->next;

free(curr);

}}

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT:

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

In the above kindly change singly


word to doubly

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

PROGRAM:

//DOUBLY LINKED LIST

#include<stdio.h>

#include<conio.h>

struct dll

int roll;

struct dll *prev;

struct dll *next;

};

typedef struct dll node;

node *head=NULL;

void create();

void insert();

void delet();

void del_max();

void display();

void count();

void main()

int ch,i=0,n;

clrscr();

do

printf("\n****DLL****\n");

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

printf("\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.count\n6.del_max\n7.Exit\n");

printf("\nEnter the choice\n");

scanf("%d",&ch);

switch(ch)

case 1:

printf("\nEnter the number of nodes to be created\n");

scanf("%d",&n);

while(i<n)

create();

i++;

break;

case 2:

insert();

break;

case 3:

delet();

break;

case 4:

display();

break;

case 5:

count();

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

break;

case 6:

del_max();

break;

case 7:

exit(0);

}while(ch<=7);

void create()

node *newnode,*temp;

newnode=(node*)malloc(sizeof(node));

printf("\nEnter the roll no\n");

scanf("%d",&newnode->roll);

if(head==NULL)

newnode->next=NULL;

newnode->prev=NULL;

head=newnode;

return;

else if(newnode->roll<=head->roll)

newnode->next=head;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

head=newnode;

else

temp=head;

while((temp->next!=NULL)&&(temp->roll<=newnode->roll))

temp=temp->next;

if(temp->next!=NULL){

printf("%d",temp->roll);

newnode->next=temp;

newnode->prev=temp->prev;

temp->prev=newnode;

// temp->next=newnode;

else{

temp->next=newnode;

newnode->prev=temp->next;

} }

void insert()

create();

printf("\nNode is inserted\n");

void delet()

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

node *prev,*temp;

int key;

if(head==NULL)

printf("\nSLL is empty\n");

return;

printf("\nEnter the roll no to be deleted\n");

scanf("%d",&key);

if(head->roll==key)

temp=head->next;

free(head);

printf("\nNode is deleted\n");

head=temp;

temp->prev=NULL;

else

temp=head;

while(temp->next!=NULL)

if(temp->next->roll==key)

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

temp->next->prev=temp;

temp->next=temp->next->next;

printf("\nNode is deleted\n%d",temp->next->roll);

free(temp->next);

//temp->next=prev;

break;

temp=temp->next;

return;

if(temp==NULL)

printf("\nNode not found\n");

void del_max()

node *temp,*prev;

temp=head;

if(temp==NULL)

printf("\nSLL is empty\n");

return;

while(temp->next!=NULL)

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

prev=temp;

temp=temp->next;

prev->next=NULL;

printf("\nThe deleted roll no is %d",temp->roll);

free(temp);

void display()

node *temp;

temp=head;

printf("\nThe contents are\n");

while(temp!=NULL)

printf("%d--->",temp->roll);

temp=temp->next;

void count()

node *temp;

int co=0;

temp=head;

while(temp!=NULL)

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

temp=temp->next;

co++;

printf("Number of nodes are %d ",co);

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT:

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

PROGRAM:

//STACK USING LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void pop();

void push(int value);

void display();

struct node

int data;

struct node*link;

};

struct node*top=NULL,*temp;

void main()

int ch,data;

while(1)

printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit");

printf("\n Enter the choice:");

scanf("%d",&ch);

switch(ch)

case 1:

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

printf("\n Enter a new element");

scanf("%d",&data);

push(data);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(0);

void display()

temp=top;

if(temp==NULL)

printf("\n Empty");

printf("\n the contents of stack are....");

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->link;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

void push(int data)

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

temp->data=data;

temp->link=top;

top=temp;

display();

void pop()

if(top!=NULL)

{printf("\n The popped element is %d",top->data);

top=top->link;

else

printf("\n stack underflow");

display();

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT:

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

PROGRAM:

//QUEUE USING LINKED LIST

#include<stdio.h>

#include<conio.h>

struct queue

int data;

struct queue*next;

};

typedef struct queue node;

node *front=NULL,*rear=NULL;

main()

int ch;

void insert();

void delet();

void display();

while(1)

printf("\n Queue using Linkedlist");

printf("\n 1.Insert \n 2.Delete \n 3.Display \n 4.Exit");

printf("\nEnter the choice:");

scanf("%d",&ch);

switch(ch)

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

case 1:

insert();

break;

case 2:

delet();

break;

case 3:

display();

break;

case 4:

exit(0);

return 0;

void insert()

node *nn,*temp;

nn=(node*)malloc(sizeof(node));

printf("\n Enter Data:");

scanf("%d",&nn->data);

nn->next=NULL;

if(front==NULL)

front=nn;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

rear=nn;

else

rear->next=nn;

rear=rear->next;

void delet()

node *temp;

if(front==NULL)

printf("\n Queue is Empty");

else

temp=front;

printf("\n Deleted data is %d",temp->data);

front=front->next;

temp->next=NULL;

free(temp);

void display()

node *temp;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

temp=front;

while(temp!=NULL)

printf("%d ->",temp->data);

temp=temp->next;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT:

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017
AIM: TO WRITE A C PROGRAM FOR THE EVALUATION OF POSTFIX EXPRESSION.

OUTPUT: THUS THE C PROGRAM FOR EVALUTION OF POSTFIX EXPRESSION WAS


IMPLEMENTED AND EXECUTED SUCCESSFULLY.

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

PROGRAM:

//EVALUATION OF POSTFIX EXPRESSION

#include<stdio.h>

#include<conio.h>

#include<math.h>

struct stack

int top;

float a[50];

}s;

void main()

char pf[50];

float d1,d2,d3;

int i;

clrscr();

s.top=-1;

printf("\n \t ***Evaluation of Postfix Expressions***\n");

printf("\n \n Enter the postfix expression:\n");

//gets(pf);

fflush(stdin);

scanf("%s",pf);

for(i=0;pf[i]!='\0';i++)

switch(pf[i])

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

s.a[++s.top]=pf[i]-'0';

break;

case '+':

d2=s.a[s.top--];

d1=s.a[s.top--];

s.a[++s.top]=d1+d2;

break;

case '-':

d2=s.a[s.top--];

d1=s.a[s.top--];

s.a[++s.top]=d1-d2;

break;

case '*':

d2=s.a[s.top--];

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

d1=s.a[s.top--];

s.a[++s.top]=d1*d2;

break;

case '/':

d2=s.a[s.top--];

d1=s.a[s.top--];

s.a[++s.top]=d1/d2;

break;

case '^':

d2=s.a[s.top--];

d1=s.a[s.top--];

s.a[++s.top]=pow(d1,d2);

break;

printf("\n \n The value of expression is %f",s.a[s.top]);

getch();

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT:

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT: THUS THE C PROGRAM TO CONVERT THE GIVEN INFIX EXPRESSION TO POSTFIX
EXPRESSION WAS IMPLEMENTED AND EXECUTED SUCCESSFULLY.

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

PROGRAM:

//CONVERSION OF INFIX EXPRESSION TO POSTFIX EXPRESSION

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 20

int top=-1;

char pop();

char stack[MAX];

void push(char item);

int prcd(char);

int prcd(char symbol){

switch(symbol){

case '+':

case '-':return 2;

break;

case '*':

case '/':return 4;

break;

case '^':

case '$':return 6;

break;

case '(':

case ')':

case '#':return 1;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

break;

int isoperator(char symbol){

switch(symbol){

case '+':

case '-':

case '*':

case '/':

case '^':

case '$':

case '(':

case ')':return 1;

break;

default:

return 0;

void convertip(char infix[],char postfix[]){

int i,symbol,j=0;

stack[++top]='#';

for(i=0;i<strlen(infix);i++){

symbol=infix[i];

if(isoperator(symbol)==0){

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

postfix[j]=symbol;

j++;

else{

if(symbol=='(')

push(symbol);

else if(symbol==')'){

while(stack[top]!='('){

postfix[j]=pop();

j++;

pop();//pop out (.

else{

if(prcd(symbol)>prcd(stack[top]))

push(symbol);

else{

while(prcd(symbol)<=prcd(stack[top])){

postfix[j]=pop();

j++;

push(symbol);

}//end of else.

}//end of else.

}//end of else.

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

}//end of for.

while(stack[top]!='#'){

postfix[j]=pop();

j++;

postfix[j]='\0';//null terminate string.}

void main(){

char infix[20],postfix[20];

clrscr();

printf("\n\t***Infix To Postfix Expression***\n");

printf("Enter the valid infix string:\n");

gets(infix);

convertip(infix,postfix);

printf("The corresponding postfix string is:\n");

puts(postfix);

getch();}

void push(char item){

top++;

stack[top]=item;}

char pop(){

char a;

a=stack[top];

top--;

return a;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT:

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT: THUS, THE C PROGRAM FOR THE DEMONSTRATION OF


INSERTION AND DELETION IN BINARY SEARCH TREE WAS
IMPLEMENTED AND EXECUTED SUCCESSFULLY.

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

PROGRAM:

//DEMONSTRATION OF INSERTION AND DELETION IN BINARY SEARCH TREE

#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
// A utility function to create a new BST node
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// A utility function to do inorder traversal of BST
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key)
{

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

/* If the tree is empty, return a new node */


if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
/* return the (unchanged) node pointer */
return node;
}

/* Given a non-empty binary search tree, return the node with minimum key value found in that
tree. Note that the entire tree does not need to be searched. */
struct node * minValueNode(struct node* node)
{
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
return current;
}
/* Given a binary search tree and a key, this function deletes the key and returns the new root */
struct node* deleteNode(struct node* root, int key)
{
struct node *temp;
if (root == NULL) return root;
// If the key to be deleted is smaller than the root's key, then it lies in left subtree
if (key < root->key)
root->left = deleteNode(root->left, key);
// If the key to be deleted is greater than the root's key, then it lies in right subtree
else if (key > root->key)
root->right = deleteNode(root->right, key);

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

// if key is same as root's key, then this is the node to be deleted


else
{
// node with only one child or no child
if (root->left == NULL)
{
temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
temp = root->left;
free(root);
return temp;
}
// node with two children: Get the inorder successor (smallest in the right subtree)
temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);}
return root;
}
int main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node *root = NULL;

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

root = insert(root, 50);


root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
printf("Inorder traversal of the given tree \n");
inorder(root);
printf("\nDelete 20\n");
root = deleteNode(root, 20);
printf("Inorder traversal of the modified tree \n");
inorder(root);
printf("\nDelete 30\n");
root = deleteNode(root, 30);
printf("Inorder traversal of the modified tree \n");
inorder(root);
printf("\nDelete 50\n");
root = deleteNode(root, 50);
printf("Inorder traversal of the modified tree \n");
inorder(root);
return 0;
}

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗
PDKV CSE Reg. no.:411620104017

OUTPUT:

CS8381 Data Structures Laboratory


@SAMUEL GLADLY💫🤗

You might also like