DSA Lab Manual (3)
DSA Lab Manual (3)
DSA Lab Manual (3)
LABORATORY MANUAL
III SEMESTER
(BCSL305)
2023-2024
Allocating memory
There are two ways that memory gets allocated for data storage:
o For standard array declarations, this is why the size has to be constant
There are four functions malloc(), calloc(), realloc() and free() present in <stdlib.h> header file that are
used for Dynamic Memory Allocation. Dynamic Memory Allocation is considered as a very important
concept in the field of Data Structures and is used in almost every Data Structures like linked lists, stacks,
queues etc.
Method Description
malloc() Allocates a single block of requested memory.
calloc() Allocates multiple blocks of requested memory.
Method Description
realloc() Reallocates the memory occupied by malloc() or calloc() functions.
free() Frees the dynamically allocated memory.
CODE
#include<stdio.h>
#define noofdays 7
#include<stdlib.h>
#include<string.h>
struct day
{
char dayname[20];
int date;
char activity[50];
};
void main()
{
daylst cal;
cal=createcal();
adddetails(cal);
display(cal);
}
daylst createcal()
{
daylst temp;
int i;
temp=(daylst)malloc(noofdays*sizeof(struct day));
if(temp==NULL)
{
printf("Insufficient memory\n");
exit(0);
}
else
{
for(i=0;i<noofdays;i++)
{
strcpy(temp[i].dayname,"-------");
temp[i].date=0;
strcpy(temp[i].activity,"No Activity");
}
}
return temp;
}
int i;
printf("----------CALENDAfR------------------\n");
printf("DAY NO\tDAY NAME\t\tDATE\tACTIVITY\n");
for(i=0;i<noofdays;i++)
printf("%d\t%s\t\t%d\t%s\n",i+1,cal[i].dayname,cal[i].date,cal[i].activity);
}
OUTPUT
Strings
In C, string is implemented as an array of characters. A string constant in C is always terminated
by a NULL character.
For example, the string “AGMRCET” is stored in memory as shown below:
|A|G|M|R|C|E|T| \0|
Code
//pattern searching and replacing
#include<stdio.h>
#include<string.h>
int main()
{
char mainstr[200],patternstr[50],replacestr[50],resstr[200];
int i,j,k,noofmatches=0,plen,rlen,t=0;
for(i=0;i<strlen(mainstr)-plen;i++)
{
for(j=0;j<plen;j++)
{
if(mainstr[i+j]!=patternstr[j])
break;
}
if(j==plen)
{
noofmatches++;
t=0;
for(k=0;k<i;k++)
resstr[t++]=mainstr[k];
for(k=0;k<rlen;k++)
resstr[t++]=replacestr[k];
for(k=i+plen;k<strlen(mainstr);k++)
resstr[t++]=mainstr[k];
resstr[t]='\0';
for(k=0;k<strlen(resstr);k++)
mainstr[k]=resstr[k];
mainstr[k]='\0';
}
}
if(noofmatches>0)
{
printf("%d matches occured in the input text\n",noofmatches);
printf("After replacing the matched patterns the string is \n %s\n",resstr);
}
else
printf("Pattern string not found in the input text\n");
return 0;
}
Output
STACK
A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is
named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of
plates, etc. A real-world stack allows operations at one end only. For example, example, we can
place or remove a card or plate from the top of the stack only. Likewise, Stack ADT allows all
data operations at one end only. At any given time, we can only access the top element of a
stack. This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the
element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion
operation called PUSH operation and removal operation is called POP operation.
Stack Representation
A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can
either be a fixed size one or it may have a sense of dynamic resizing. Here, we have implemented
stack using arrays, which makes it a fixed size stack implementation.
Basic Operations
Stack operations rations may involve initializing the stack, using it and then de-initializing it.
Apart from these basic stuffs, a stack is used for the following two primary operations –
To use a stack efficiently, we need to check the status of stack as well. For the same purpose,
purpose, the following functionality is added to stacks –
peek() − get the top data element of the stack, without removing it.
overflow() − check if stack is full.
At all times, we maintain a pointer to the last pushed data on the stack. As this pointer always
represents the top of the stack, hence named top. The top pointer provides top value of the stack
without actually removing it.
Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps:
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation. In an array
implementation of pop() operation, the data element is not actually removed, instead top is
decremented to a lower position in the stack to point to the next value. But in linked-list
implementation, pop() actually removes data element and deallocates memory space. A Pop
operation may involve the following steps –
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Peek
Step 3 − If the stack is not empty, return the data element at which top is pointing.
Overflow
Underflow
CODE
//Stack operations
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void push();
void pop();
void display();
void overflow();
void underflow();
void palindrome();
int stack[20],top=-1;
int main()
{
int ch;
for(;;)
{
printf("---------------STACK OPERATIONS---------------------\n");
printf("1.PUSH\n2.POP\n3.DISPLAY\n4.OVERFLOW\n5.UNDERFLOW\n6.PEEK\
n7.PALINDROME\n8.EXIT\n");
printf("Enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:overflow();
break;
case 5:underflow();
break;
case 6:if(top==-1)
printf("Stack is empty\n");
else
printf("Peek of stack: %d\n",stack[top]);
break;
case 7:palindrome();
break;
case 8:exit(0);
default:printf("Invalid choice\n");
}
}
}
void push()
{
int e;
if(top==MAX-1)
{
printf("Stack is full\n");
return;
}
else
{
printf("\nEnter the element to be pushed \n");
scanf("%d",&e);
top=top+1;
stack[top]=e;
}
}
void pop()
{
if(top==-1)
{
printf("Stack is empty\n");
return;
}
else
{
printf("Popped element: %d\n",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("Stack is empty\n");
return;
}
else
{
printf("Stack Contents are:\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
void overflow()
{
int i;
printf("\nThere are currently %d elements in Stack\nPush %d elements for Stack to
Overflow", top+1, MAX - (top+1));
for(i=top;i<MAX;i++)
push();
printf("Stack overflow\n");
}
void underflow()
{
int i;
printf("\nThere are currently %d elements in Stack\nPop out %d elements for Stack to
Underflow", top+1, MAX - (top+1));
for(i=top;i>=0;i--)
pop();
printf("Stack underflow\n");
}
void palindrome()
{
int i,t[20],j=0;
if(top==-1)
{
printf("Stack is empty\n");
return;
}
else
{
for(i=top;i>=0;i--)
t[j++]=stack[i];
for(i=0;i<=top;i++)
{
if(stack[i]!=t[i])
break;
}
if(i==top+1)
printf("Stack elements form palindrome\n");
else
printf("Stack elements doesnot form palindrome\n");
}
}
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
1
Enter the element to be pushed
1
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
1
Enter the element to be pushed
2
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
1
Enter the element to be pushed
1
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
3
Stack Contents are:
1
2
1
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
7
Stack elements form palindrome
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
4
There are currently 3 elements in Stack
Push 2 elements for Stack to Overflow
Enter the element to be pushed
3
Enter the element to be pushed
4
Stack is full
Stack Overflow
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
2
Popped element: 4
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
6
Peek of stack: 3
-------STACK OPERATIONS---------
1.PUSH
2.POP
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.PEEK
7.PALINDROME
8.EXIT
Enter your choice
5
There are currently 4 elements in Stack
Pop out 4 elements for Stack to Underflow
Popped element: 3
Popped element: 1
Popped element: 2
Popped element: 1
Stack Underflow
Step 1: Read the infix expression. Iterate the given expression from left to right, one character at
a time
Step 3: If the scanned character is opening round bracket ( '(' ), push it into operator's stack.
Step 4: If the scanned character is closing round bracket ( ')' ), pop out operators from operator's
stack until we find an opening bracket ('(' ).
Step 5: If the scanned character is an operator and operator's stack is empty, push operator into
operators' stack.
Step 6: If the operator's stack is not empty, there may be following possibilities.
If the precedence of scanned operator is greater than the top most operator of operator's
stack, push this operator into operator’s stack.
If the precedence of scanned operator is less than the top most operator of operator's
stack, pop the operators from operator's stack until we find a low precedence operator
than the scanned character.
Step 8: Now pop out all the remaining operators from the operator's stack and push into postfix
expression.
Step 9: Exit
CODE
#include<stdio.h>
void main()
{
char infix[50],postfix[50],x,ch;
int i,j=0;
printf("Enter the infix expression\n");
scanf("%s",infix);
for(i=0;infix[i]!='\0';i++)
{
ch=infix[i];
if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')||(ch>='0' && ch<='9'))
postfix[j++]=ch;
else if(ch=='(')
stack[++top]=ch;
else if(ch==')')
{
while((x=stack[top--])!='(')
postfix[j++]=x;
}
else
{
while(priority(stack[top])>=priority(ch))
postfix[j++]=stack[top--];
stack[++top]=ch;
}
}
while(top!= -1)
postfix[j++]=stack[top--];
postfix[j]='\0';
printf("Infix expression = %s\n",infix);
printf("Postfix expression = %s\n",postfix);
}
return -1;
}
OUTPUT
Many apps' undo-redo functionality employs stacks to remember the prior operations. A new
action is added to the Stack each time it is completed. The top member of the Stack is popped to
undo the action, and the original procedure is then carried out.
4. Web browser history-
Stacks are used by web browsers to record the websites you visit. When you click the back
button, the previous URL is removed from the Stack and is added to the Stack each time you
visit a new page.
5. Parenthesis checking-
To determine if brackets are balanced or not, a stack data structure is utilized. An opening
parenthesis is popped off the Stack as a closing parenthesis is added onto it. The brackets are
balanced if the Stack is empty at the conclusion of the expression.
6. Expression Evaluation-
Expressions written in infix, postfix, and prefix notations are evaluated using a stack data
structure. The Stack is used to hold operators and operands, and the top pieces of the Stack are
used to carry out operations.
Tower of Hanoi
The Tower of Hanoi algorithm is a method to solve a mathematical game involving three rods
and a number of disks of different sizes. The algorithm aims to move all of the disks from the
first rod to the last, obeying specific rules. It is a powerful demonstration of recursion in
programming.
The Tower of Hanoi algorithm finds its application in the world of problem-solving, particularly
in puzzles and games.
The objective of the algorithm is to move the entire stack to the last rod, obeying these simple
rules:
2. Each move consists of taking the upper disk from one of the stacks and placing it on top
of another stack or on an empty rod.
The number of moves required to solve a Tower of Hanoi puzzle is 2n−1, where n is the number
of disks.
The Tower of Hanoi recursive algorithm involves the following major steps:
1. Recursively move n−1 disks from the source rod to the auxiliary rod.
2. Move the nth disk from the source rod to the target rod.
3. Finally, again recursively, move the n−1 disks from the auxiliary rod to the target rod.
CODE
void main()
{
char postfix[50],ch;
int i,top=-1;
float op1,op2,res=0,stack[20];
printf("Enter a valid postfix expression\n");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
ch=postfix[i];
if(ch>='0' && ch<='9')
stack[++top]=ch-'0';
else
{
op2=stack[top--];
op1=stack[top--];
switch(ch)
{
case '+':res=op1+op2;
break;
case '-':res=op1-op2;
break;
case '*':res=op1*op2;
break;
case '/':res=op1/op2;
break;
case '^':res=pow(op1,op2);
break;
case '%':res=(int)op1%(int)op2;
break;
default:printf("Invalid Operator\n");
}
stack[++top]=res;
}
}
printf("Value of %s expression is %f\n",res);
}
Output
{
tower(n-1,from,temp,to);
printf("Move disc %d from %c to %c\n",n,from,to);
tower(n-1,temp,to,from);
}
}
Output
Circular Queue
A circular queue is an extended version of a linear queue as it follows the First In First Out
principle with the exception that it connects the last node of a queue to its first by forming a
circular link. Hence, it is also called a Ring Buffer.
Implementation of a linear queue brings the drawback of memory wastage. However, memory is
a crucial resource that should be always protected by analyzing all the implications while
designing algorithms or solutions. In the case of a linear queue, when the rear pointer reaches the
MaxSize of a queue, there might be a possibility that after a certain number of dequeue()
operations, it will create an empty space at the start of a queue. Additionally, this newly created
empty space can never be re-utilized as the rear pointer reaches the end of a queue. Hence,
experts introduced the concept of the circular queue to overcome this limitation.
As shown in the figure above, the rear pointer arrives at the beginning of a queue with the help of
a circular link to re-utilize the empty space to insert a new element. This simple addition of a
circular link resolves the problem of memory wastage in the case of queue implementation.
Thus, this particular type of queue is considered the best version of a queue data structure.
Enqueue(x) Operation
Step 6: Exit
Dequeue() Operation
Step 6: Exit
Code
//circular queue operations
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void insert();
void cdelete();
void display();
void overflow();
void underflow();
char queue[20];
int r=-1,f=0,cnt=0;
int main()
{
int ch;
for(;;)
{
printf("\n-------Queue Operations---------\n");
printf("1.INSERT\n2.DELETE\n3.DISPLAY\n4.OVERFLOW\n5.UNDERFLOW\
n6.EXIT\n");
printf("Enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:cdelete();
break;
case 3:display();
break;
case 4:overflow();
break;
case 5:underflow();
break;
case 6:exit(0);
default:printf("Invalid choice\n");
}
}
}
void insert()
{
int e;
if(cnt==MAX)
{
printf("Queue is full\n");
return;
}
else
{
printf("Enter an element : \n");
scanf("%c",&e);
r=(r+1)%MAX;
queue[r]=e;
cnt=cnt+1;
}
}
void cdelete()
{
if(cnt==0)
{
printf("Queue is empty\n");
return;
}
else
{
printf("Deleted element is %c\n",queue[f]);
f=(f+1)%MAX;
cnt=cnt-1;
}
}
void display()
{
int i;
if(cnt==0)
{
printf("Queue is empty\n");
return;
}
else
{
printf(“Contents of the Queue is\n”);
if(f<r)
{
for(i=f;i<=r;i++)
printf("|%4c",queue[i]);
}
else
{
for(i=f;i<MAX;i++)
printf("|%4c",queue[i]);
for(i=0;i<=r;i++)
printf("|%4c",queue[i]);
}
}
}
void overflow()
{
int i;
for(i=cnt;i<=MAX;i++)
insert();
printf("Queue overflow\n");
}
void underflow()
{
int i;
for(i=cnt;i>=0;i--)
cdelete();
printf("Queue underflow\n");
}
Output
------Queue Operations------
1.INSERT
2.DELETE
3.DISPLAY
4.OVERFLOW
5.EXIT
Queue is Empty
------Queue Operations------
1.INSERT
2.DELETE
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.EXIT
Enter an element : I
------Queue Operations------
1.INSERT
2.DELETE
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.EXIT
Enter an element : N
------Queue Operations------
1.INSERT
2.DELETE
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.EXIT
Enter an element : D
Enter an element : I
Enter an element : A
Queue overflow
------Queue Operations------
1.INSERT
2.DELETE
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.EXIT
------Queue Operations------
1.INSERT
2.DELETE
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.EXIT
Queue is Full
------Queue Operations------
1.INSERT
2.DELETE
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.EXIT
Deleted element is I
------Queue Operations------
1.INSERT
2.DELETE
3.DISPLAY
4.OVERFLOW
5.UNDERFLOW
6.EXIT
Deleted element is N
Deleted element is D
Deleted element is I
Deleted element is A
Queue is Empty
Queue Underflow
Single linked list is a sequence of elements in which every element has link to its next element in
the sequence. In any single linked list, the individual element is called as "Node". Every "Node"
contains two fields, data field, and the next field. The data field is used to store actual value of
the node and next field is used to store the address of next node in the sequence.
The graphical representation of a node in a single linked list is as follows.
Insertion
In a single linked list, the insertion operation can be performed in the following three ways.
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Deletion
In a single linked list, the deletion operation can be performed in the following three ways:
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list
conditions)
Step 6 - If it is FALSE then set head = temp → next, and delete temp.
Code
//Singly linked list operations
#include<stdio.h>
#include<stdlib.h>
struct stud
{
char usn[ 10];
char name[50];
char branch[10];
int sem;
long long int phone;
struct stud *link;
};
studlst create(studlst);
studlst insert_first(studlst);
studlst insert_last(studlst);
studlst delete_first(studlst);
studlst delete_last(studlst);
studlst getnode();
void display(studlst);
void noofnodes(studlst);
int main()
{
int ch;
studlst lst;
lst=NULL;
for(;;)
{
printf("\n=========SINGLY LINKED LIST OPERATIONS==========\n");
printf("\n1.CREATE LIST\n2.DISPLAY\n3.INSERT FIRST\n4.INSERT LAST\
n5.DELETE FIRST\n6.DELETE LAST\n7.NODES COUNT\n8.EXIT\n");
printf("Enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:lst=create(lst);
break;
case 2:display(lst);
break;
case 3:lst=insert_first(lst);
break;
case 4:lst=insert_last(lst);
break;
case 5:lst=delete_first(lst);
break;
case 6:lst=delete_last(lst);
break;
case 7:noofnodes(lst);
break;
case 8:exit(0);
default: printf("Invalid choice\n");
}
}
for(i=1;i<=n;i++)
{
temp1=getnode();
temp1->link=lst;
lst=temp1;
}
return lst;
}
studlst getnode()
{
studlst temp;
temp=(studlst)malloc(sizeof(struct stud));
if(temp==NULL)
{
printf("Insufficient memory\n");
exit(0);
}
else
{
printf("Enter the details\n");
printf("USN: ");
scanf("%s",temp->usn);
printf("\nName: ");
scanf("%s",temp->name);
printf("\nBranch: ");
scanf("%s",temp->branch);
printf("\nSem: ");
scanf("%d",&temp->sem);
printf("\nPhone: ");
scanf("%lld",&temp->phone);
temp->link=NULL;
}
return temp;
}
{
studlst temp;
temp= getnode();
if(lst==NULL)
lst=temp;
else
{
temp->link=lst;
lst=temp;
}
return lst;
}
if(lst==NULL)
lst=temp;
else
{
cur=lst;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
}
return lst;
}
{
printf("%s\t\t%s\t%s\t%d\t%lld\n",temp->usn,temp->name,temp->branch,temp-
>sem,temp->phone);
temp=temp->link;
}
}
}
printf("List is Empty\n");
else
{
temp=lst;
lst=temp->link;
printf("Deleted node with USN %s\n",temp->usn);
free(temp);
}
return lst;
}
printf("List is Empty\n");
else
{
temp=lst;
while(temp->link!=NULL)
{
cur=temp;
temp=temp->link;
}
cur->link=NULL;
printf("Deleted node with USN %s\n",temp->usn);
free(temp);
}
return lst;
Output
=========SINGLY LINKED LIST OPERATIONS==========
1.CREATE LIST
2.DISPLAY
3.INSERT FIRST
4.INSERT LAST
5.DELETE FIRST
6.DELETE LAST
7.NODES COUNT
8.EXIT
7.NODES COUNT
8.EXIT
CODE
//doubly linked list operations
#include<stdio.h>
#include<stdlib.h>
struct emp
{
char ssn[ 10];
char name[50];
char dept[50];
char desig[50];
int sal;
long phone;
struct emp *llink;
struct emp *rlink;
};
emplst create(emplst);
emplst insert_first(emplst);
emplst insert_last(emplst);
emplst delete_first(emplst);
emplst delete_last(emplst);
emplst getnode();
void display(emplst);
int main()
{
int ch;
emplst lst;
lst=NULL;
for(;;)
{
printf("\n=========DOUBLY LINKED LIST OPERATIONS==========\n");
printf("\n1.CREATE LIST\n2.DISPLAY\n3.INSERT FIRST\n4.INSERT LAST\
n5.DELETE FIRST\n6.DELETE LAST\n7.EXIT\n");
case 6:lst=delete_last(lst);
break;
case 7:exit(0);
default: printf("Invalid choice\n");
}
}
emplst getnode()
{
emplst temp;
temp=(emplst)malloc(sizeof(struct emp));
if(temp==NULL)
{
printf("Insufficient memory\n");
exit(0);
}
else
{
printf("Enter the details\n");
printf("SSN: ");
scanf("%s",temp->ssn);
printf("\nName: ");
scanf("%s",temp->name);
printf("\nDepartment: ");
scanf("%s",temp->dept);
printf("\nDesignation: ");
scanf("%s",temp->desig);
printf("\nSalary: ");
scanf("%d",&temp->sal);
printf("\nPhone: ");
scanf("%ld",&temp->phone);
temp->llink=NULL;
temp->rlink=NULL;
}
return temp;
}
else
{
cur=lst;
while(cur->rlink!=NULL)
cur=cur->rlink;
}
for(i=1;i<=n;i++)
{
temp1=getnode();
cur->rlink=temp1;
temp1->llink=cur;
cur=temp1;
}
return lst;
}
{
temp->rlink=lst;
lst->llink=temp;
lst=temp;
}
return lst;
}
if(lst==NULL)
lst=temp;
else
{
cur=lst;
while(cur->rlink!=NULL)
cur=cur->rlink;
cur->rlink=temp;
temp->llink=temp;
}
return lst;
}
while(temp!=NULL)
{
printf("%s\t%s\t%s\t%s\t%d\t%ld\n",temp->ssn,temp->name,temp->dept,temp-
>desig,temp->sal,temp->phone);
temp=temp->rlink;
}
}
}
printf("List is Empty\n");
else
{
temp=lst;
lst=temp->rlink;
lst->llink=NULL;
printf("Deleted node with ssn %s\n",temp->ssn);
free(temp);
}
return lst;
}
printf("List is Empty\n");
else
{
temp=lst;
while(temp->rlink!=NULL)
{
cur=temp;
temp=temp->rlink;
}
cur->rlink=NULL;
temp->llink=NULL;
printf("Deleted node with ssn %s\n",temp->ssn);
free(temp);
}
return lst;
}
OUTPUT
=========DOUBLY LINKED LIST OPERATIONS==========
1.CREATE LIST
2.DISPLAY
3.INSERT FIRST
4.INSERT LAST
5.DELETE FIRST
6.DELETE LAST
7.EXIT
Enter your choice:
1
Enter number of employees
2
Enter the details
SSN: 1234
Name: Kiran
Department: CSE
Designation: Clerk
Salary: 35000
Phone: 9455665577
SSN: 12355
Name: Ramesh
Department: CSE
Designation: Peon
Salary: 30000
Phone: 9155665577
1.CREATE LIST
2.DISPLAY
3.INSERT FIRST
4.INSERT LAST
5.DELETE FIRST
6.DELETE LAST
7.EXIT
Enter your choice:
3
CODE
//polynomial addition
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct term{
int pc;
int px;
int py;
int pz;
struct term *link;
};
typedef struct term *poly1;
temp->pc=pc;
temp->px=px;
temp->py=py;
temp->pz=pz;
cur = p;
while(cur->link != p)
{
cur = cur->link;
}
cur->link = temp;
temp->link = p;
return p;
}
void display(poly1 p)
{
poly1 cur;
if (p->link == p)
{
printf("Polynomial is empty.n");
return;
}
cur = p->link;
while(cur!=p)
{
printf("%dx^%dy^%dz^%d ", cur->pc, cur->px, cur->py, cur->pz);
cur = cur->link;
if (cur != p)
{
printf("+ ");
}
}
printf("\n");
}
cur = poly->link;
do
{
termValue = cur->pc;
termValue *= pow(x, cur->px);
termValue *= pow(y, cur->py);
termValue *= pow(z, cur->pz);
result += termValue;
cur = cur->link;
} while (cur != poly);
return result;
}
do
{
cur1 = psum->link;
match = 0;
do
{
if(matchterm(cur1, cur2))
{
cur1->pc += cur2->pc;
match = 1;
break;
}
cur1 = cur1->link;
}while(cur1 != psum);
if(!match)
{
psum = addterm(psum, cur2->pc, cur2->px, cur2->py, cur2->pz);
}
cur2 = cur2->link;
}while(cur2 != p2);
return psum;
int main()
{
int n,x=1,y=2,z=3,iRes;
poly1 p1,p2,psum;
p1 = (poly1)malloc(sizeof(struct term));
p1->link = p1;
p2 = (poly1)malloc(sizeof(struct term));
p2->link = p2;
psum = (poly1)malloc(sizeof(struct term));
psum->link = psum;
p1=addterm(p1,6,2,2,1);
p1=addterm(p1,4,0,1,5);
p1=addterm(p1,3,3,1,1);
p1=addterm(p1,2,1,5,1);
p1=addterm(p1,2,1,1,3);
printf("\nPOLY1(x, y, z) = ");
display(p1);
p2=addterm(p2,1,1,1,1);
p2=addterm(p2,4,3,1,1);
printf("\nPOLY2(x, y, z) = ");
display(p2);
psum = addpoly(p1, p2, psum);
printf("\nPOLYSUM(x, y, z) = ");
display(psum);
iRes = fnEvaluatePolynomial(psum, x, y, z);
printf("nResult of POLYSUM(%d, %d, %d): %dn", x, y, z, iRes);
return 0;
}
OUTPUT
POLY1(x, y, z) = 6x^2y^2z^1 + 4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^5z^1 + 2x^1y^1z^3
POLY2(x, y, z) = 1x^1y^1z^1 + 4x^3y^1z^1
CODE
//binary search tree operations
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
typedef struct node *tree;
tree getnode();
tree create(tree);
void inorder(tree);
void preorder(tree);
void postorder(tree);
void search(tree,int);
int main()
{
tree t = NULL;
int ch,key,i,n,ele;
for(;;)
{
printf("\n===========BINARY SEARCH TREE
OPERATIONS==========\n");
printf("\n1.PREORDER\n2.INORDER\n3.POSTORDER\n4.SEARCH\n5.EXIT\
n");
printf("Enter your choice: \n");
scanf("%d",&ch);
switch(ch)
{
case 1: preorder(t);
break;
case 2: inorder(t);
break;
case 3: postorder(t);
break;
case 4: printf("Enter the searching element\n");
scanf("%d",&key);
search(t,key);
break;
case 5: exit(0);
tree getnode()
{
tree temp;
temp = (tree ) malloc (sizeof(struct node));
if(temp==NULL)
{
printf("Insufficient memory\n");
exit(0);
}
else
{
printf("Enter the node value\n");
scanf("%d",&temp->data);
temp->llink=NULL;
temp->rlink=NULL;
}
return temp;
}
tree create(tree t)
{
tree temp,prev,cur;
temp=getnode();
if(t==NULL)
return temp;
prev = NULL;
cur = t;
while(cur != NULL)
{
prev = cur;
if(temp->data == cur->data)
{
printf("Duplicate items not allowed\n");
free(temp);
return t;
}
return t;
void preorder(tree t)
{
if(t != NULL)
{
printf("%d\t",t->data);
preorder(t->llink);
preorder(t->rlink);
}
}
void inorder(tree t)
{
if(t != NULL)
{
inorder(t->llink);
printf("%d\t",t->data);
inorder(t->rlink);
}
}
void postorder(tree t)
{
if(t != NULL)
{
postorder(t->llink);
postorder(t->rlink);
printf("%d\t",t->data);
}
}
printf("Unsuccessful Search\n");
}
OUTPUT
Enter number of nodes
6
Enter 6 numbers
Enter the node value
50
Enter the node value
30
Enter the node value
70
Enter the node value
40
Enter the node value
60
Enter the node value
20
===========BINARY SEARCH TREE OPERATIONS==========
1.PREORDER
2.INORDER
3.POSTORDER
4.SEARCH
5.EXIT
Enter your choice:
1
50 30 20 40 70 60
===========BINARY SEARCH TREE OPERATIONS==========
1.PREORDER
2.INORDER
3.POSTORDER
4.SEARCH
5.EXIT
Enter your choice:
2
20 30 40 50 60 70
===========BINARY SEARCH TREE OPERATIONS==========
1.PREORDER
2.INORDER
3.POSTORDER
4.SEARCH
5.EXIT
Enter your choice:
3
20 40 30 60 70 50
CODE
//Graph Reachability using BFS/DFS
#include<stdio.h>
void bfs(int);
void dfs(int);
int graph[10][10],n,visited[20];
int main()
{
int i,j,start;
printf("Enter the number of vertices\n");
scanf("%d",&n);
for(i=0;i<n;i++)
visited[i]=0;
OUTPUT
Enter the number of vertices
4
Enter the adjacency matrix
0110
1001
1001
0110
Enter starting vertex
1
Vertices which can be reached from vertex 1 using BFS are:
1 2 3 4
Vertices which can be reached from vertex 1 using DFS are:
1 2 3 4
Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of
m memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let
the keys in K and addresses in L are Integers. Develop a Program in C that uses Hash
function H: K →L as H(K)=K mod m (remainder method), and implement hashing
technique to map a given key K to the address space L. Resolve the collision (if any) using
linear probing.
CODE
//Hashing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMP 100
#define MAX_TAB 50
struct EMPLOYEE
{
int key;
char name[50];
};
typedef struct EMPLOYEE *emp;
emp hashtable[MAX_TAB];
int cmphash(int, int);
void addemp(emp, int);
emp searchemp(int, int);
int main()
{
int m,i,n = 0;
int srchkey,empkey;
char empname[50];
emp temp,found;
FILE *file = fopen("employee.txt", "r");
printf("Enter the size of the hash table (m): ");
scanf("%d", &m);
for (i = 0; i < m; i++)
{
hashtable[i] = NULL;
}
if(file == NULL)
{
printf("Error opening file\n");
return 1;
}
while(fscanf(file, "%d %s", &empkey, empname) != EOF)
{
temp = (emp)malloc(sizeof(struct EMPLOYEE));
temp->key = empkey;
strcpy(temp->name, empname);
addemp(temp, m);
n++;
}
fclose(file);
printf("Enter a key to search for an employee record: ");
scanf("%d", &srchkey);
found = searchemp(srchkey, m);
if(found != NULL)
{
printf("Employee found with key %d:\n", found->key);
printf("Name: %sn", found->name);
}
else
{
printf("Employee with key %d not found\n", srchkey);
}
}
void addemp(emp e, int m)
{
int index = cmphash(e->key, m);
while(hashtable[index] != NULL)
{
index = (index + 1) % m;
}
hashtable[index] = e;
}
while(hashtable[index] != NULL)
{
if(hashtable[index]->key == key)
{
return hashtable[index];
}
index = (index + 1) % m;
}
return NULL;
}
OUTPUT
Enter the size of the hash table (m): 50
Enter a key to search for an employee record: 5216
Employee found with key 5216:
Name: sanjay