DS Lab Mannual
DS Lab Mannual
:411620104017
PROGRAM:
#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;
};
node*head=NULL;
void main()
int ch;
while(1)
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();
break;
case 7:
delete_last();
break;
case 8:
delete_mid();
break;
case 9:
count();
break;
case 10:
exit(0);
default:
break;
void create()
//do
//{
node*temp,*newnode;
newnode=(node*)malloc(sizeof(node));
if(newnode==NULL)
exit(0);
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;
//scanf("%c",&c);
//}while(c=='y' || c=='Y');
void display()
node*temp;
if(head==NULL)
return;
else
temp=head;
while(temp!=NULL)
printf("\n %d",temp->roll);
temp=temp->next;
}}
void count()
int count=0;
node*temp;
if(head==NULL)
return;
else
temp=head;
while(temp!=NULL)
count++;
temp=temp->next;
void insert_first()
node *newnode;
newnode=(node*)malloc(sizeof(node));
scanf("%d",&newnode->roll);
if(head==NULL)
head=newnode;
newnode->next=NULL;
else
newnode->next=head;
head=newnode;
void insert_last()
node*newnode,*temp;
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));
scanf("%d",&newnode->roll);
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)
void delete_first()
node*temp;
if(head==NULL)
printf("\n LL is empty");
return;
else
temp=head;
head=head->next;
free(temp);
void delete_last()
node*temp,*last;
if(head==NULL)
return;
else if(head->next==NULL)
temp=head;
head=NULL;
free(temp);
else
last=head;
while(last->next!=NULL)
temp=last;
last=last->next;
temp->next=NULL;
free(last);
void delete_mid()
node*curr=head,*prev;
int ro;
if(head==NULL)
printf("SLL is empty");
return;
scanf("%d",&ro);
if(curr->roll==ro)
head=curr->next;
free(curr);
return;
else
prev=curr;
curr=curr->next;
if(curr==NULL)
return;
prev->next=curr->next;
free(curr);
}}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct dll
int roll;
};
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");
printf("\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.count\n6.del_max\n7.Exit\n");
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%d",&n);
while(i<n)
create();
i++;
break;
case 2:
insert();
break;
case 3:
delet();
break;
case 4:
display();
break;
case 5:
count();
break;
case 6:
del_max();
break;
case 7:
exit(0);
}while(ch<=7);
void create()
node *newnode,*temp;
newnode=(node*)malloc(sizeof(node));
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;
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()
node *prev,*temp;
int key;
if(head==NULL)
printf("\nSLL is empty\n");
return;
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)
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)
void del_max()
node *temp,*prev;
temp=head;
if(temp==NULL)
printf("\nSLL is empty\n");
return;
while(temp->next!=NULL)
prev=temp;
temp=temp->next;
prev->next=NULL;
free(temp);
void display()
node *temp;
temp=head;
while(temp!=NULL)
printf("%d--->",temp->roll);
temp=temp->next;
void count()
node *temp;
int co=0;
temp=head;
while(temp!=NULL)
temp=temp->next;
co++;
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void pop();
void display();
struct node
int data;
struct node*link;
};
struct node*top=NULL,*temp;
void main()
int ch,data;
while(1)
scanf("%d",&ch);
switch(ch)
case 1:
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");
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->link;
temp->data=data;
temp->link=top;
top=temp;
display();
void pop()
if(top!=NULL)
top=top->link;
else
display();
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct queue
int data;
struct queue*next;
};
node *front=NULL,*rear=NULL;
main()
int ch;
void insert();
void delet();
void display();
while(1)
scanf("%d",&ch);
switch(ch)
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));
scanf("%d",&nn->data);
nn->next=NULL;
if(front==NULL)
front=nn;
rear=nn;
else
rear->next=nn;
rear=rear->next;
void delet()
node *temp;
if(front==NULL)
else
temp=front;
front=front->next;
temp->next=NULL;
free(temp);
void display()
node *temp;
temp=front;
while(temp!=NULL)
printf("%d ->",temp->data);
temp=temp->next;
OUTPUT:
PROGRAM:
#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;
//gets(pf);
fflush(stdin);
scanf("%s",pf);
for(i=0;pf[i]!='\0';i++)
switch(pf[i])
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--];
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;
getch();
OUTPUT:
OUTPUT: THUS THE C PROGRAM TO CONVERT THE GIVEN INFIX EXPRESSION TO POSTFIX
EXPRESSION WAS IMPLEMENTED AND EXECUTED SUCCESSFULLY.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
int top=-1;
char pop();
char stack[MAX];
int prcd(char);
switch(symbol){
case '+':
case '-':return 2;
break;
case '*':
case '/':return 4;
break;
case '^':
case '$':return 6;
break;
case '(':
case ')':
case '#':return 1;
break;
switch(symbol){
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':return 1;
break;
default:
return 0;
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(infix);i++){
symbol=infix[i];
if(isoperator(symbol)==0){
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.
}//end of for.
while(stack[top]!='#'){
postfix[j]=pop();
j++;
void main(){
char infix[20],postfix[20];
clrscr();
gets(infix);
convertip(infix,postfix);
puts(postfix);
getch();}
top++;
stack[top]=item;}
char pop(){
char a;
a=stack[top];
top--;
return a;
OUTPUT:
PROGRAM:
#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)
{
/* 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);
OUTPUT: