0% found this document useful (0 votes)
8 views47 pages

Dsa Lab Finall 2.0

dsa lab 3rd sem

Uploaded by

pavan.k17819
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)
8 views47 pages

Dsa Lab Finall 2.0

dsa lab 3rd sem

Uploaded by

pavan.k17819
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/ 47

DATA STRUCTURES LABORATORY

LAB PROGRAM 01(running)

Develop a program in C for the following:


(a) Declare a calendar as an array of 7 elements(a dynamically created array) to
represent 7 days of a week. Each element of the array is a structure having three
fields. The first field is the name of the day(a dynamically allocated string),the
second field is the date of the day(a integer),the third field is the description of the
activity for a particular day(a dynamically allocated string).
(b)Write functions create(),read() and display();to create the calendar ,to read the data
from the keyboard and to print weeks activity details report on screen.

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

#define NUM_DAYS_IN_WEEK 7

typedef struct
{
char *acDayName;
int iDate;
char *acActivity;
}DAYTYPE;

void fnFreeCal(DAYTYPE*);
void fnDispCal(DAYTYPE*);
void fnReadCal(DAYTYPE*);
DAYTYPE *fnCreateCal();

int main()
{
DAYTYPE *weeklyCalendar=fnCreateCal();
fnReadCal(weeklyCalendar);
fnDispCal(weeklyCalendar);
fnFreeCal(weeklyCalendar);
return 0;
}

DAYTYPE *fnCreateCal()
{
DAYTYPE*Calendar=(DAYTYPE*)malloc(NUM_DAYS_IN_WEEK*sizeof(DAYTYPE))
;
int i;
for(i=0;i<NUM_DAYS_IN_WEEK;i++)
{
Calendar[i].acDayName=NULL;
Calendar[i].iDate=0;
Calendar[i].acActivity=NULL;
}
return Calendar;
}

void fnReadCal(DAYTYPE*Calendar)
{
char cChoice;
int i;
for(i=0;i<NUM_DAYS_IN_WEEK;i++)
{
printf("do you want to enter details for day %d[Y/N]:",i+1);
scanf("%c",&cChoice);
getchar();

if(tolower(cChoice)=='n')
continue;
printf("Day Name:");
char nameBuffer[50];
scanf("%s",nameBuffer);
Calendar[i].acDayName = strdup(nameBuffer);
printf("Date:");
scanf("%d",&Calendar[i].iDate);
printf("Activity:");
char activityBuffer[100];
scanf("%s",activityBuffer);
Calendar[i].acActivity = strdup(activityBuffer);
printf("\n");
getchar();
}
}

void fnDispCal(DAYTYPE*Calendar)
{
printf("\nWeek's Activity Details:\n");
int i;
for(i=0;i<NUM_DAYS_IN_WEEK;i++)
{
printf("Day %d\n",i+1);
if(Calendar[i].iDate==0)
{
printf("No Activity\n\n");
continue;
}
printf("Day Name:%s\n",Calendar[i].acDayName);
printf("Date:%d\n",Calendar[i].iDate);
printf("Activity:%s\n\n",Calendar[i].acActivity);
}
}

void fnFreeCal(DAYTYPE*Calendar)
{
int i;
for(i=0;i<NUM_DAYS_IN_WEEK;i++)
{
free(Calendar[i].acDayName);
free(Calendar[i].acActivity);
}
free(Calendar);
}

OUTPUT:

Do you want to enter details for day1[Y/N]:y


Day name: monday
Date:22
Activity: sports
Do you want to enter details for day2[Y/N]:y
Day name: tuesday
Date:23
Activity: yoga
Do you want to enter details for day3[Y/N]:n
Do you want to enter details for day4[Y/N]:n
Do you want to enter details for day5[Y/N]:n
Do you want to enter details for day6[Y/N]:n
Do you want to enter details for day7[Y/N]:n
nWeek's Activity Details:
Day1
Day name: monday
Date:22
Activity: sports
Day2
Day name: tuesday
Date:23
Activity: yoga
Day3
No Activity
Day4
No Activity
Day5
No Activity
Day6
No Activity
Day7
No Activity

LAB PROGRAM 02(running)


Develop a program in C for the following operations on strings.
(a)Read a main string (STR),a pattern string (PAT)and replace string(REP)
(b)Perform pattern matching operation: Find and replace all occurrences of PAT in
STR with REP if PAT exists in STR . Report suitable messages in case PAT does not
exist in STR.
Support the program with functions for each of the above operations.

#include <stdio.h>
#include <string.h>

void main() {
char s[20], pat[20], rep[20], ans[30];
int i, j, k, flag;
printf("\n Enter string: ");
scanf("%s", s);
printf("\n Enter pattern: ");
scanf("%s", pat);
printf("\n Enter replacement: ");
scanf("%s", rep);

for (i = 0, k = 0; s[i] != '\0'; i++) {


flag = 1;
for (j = 0; pat[j] != '\0'; j++) {
if (s[i + j] != pat[j]) {
flag = 0;
break;
}
}

if (flag == 1) {
for (j = 0; rep[j] != '\0'; j++, k++) {
ans[k] = rep[j];
}
i += strlen(pat) - 1; // Move past the pattern in the original string
} else {
ans[k++] = s[i];
}
}

ans[k] = '\0';
printf("%s\n", ans);
}
OUTPUT:

Enter string : ewit


Enter pattern : it
Enter replacement : cs
ewcs

LAB PROGRAM 03(running)

Develop a menu driven program in C for the following operations on STACK of


integers(Array implementation of stack with maximum size MAX).
(a) Push an element on to stack
(b) Pop an element from stack
(c)Demonstrate how stack can be used to check palindrome
(d)Demonstrate overflow and underflow situations on stack
(e)Display the status of stack
(f)Exit
Support the program with appropriate functions for each of the above operations.

#include<stdio.h>
#include<stdlib.h>
#define MAX 3
int top=-1,s[MAX];
void push(int item);
int pop();
void palindrome();
void display();
void main()
{
int choice,item;

printf("\n\n\n\n~~~~~Menu~~~~~:");
printf("\n 1.Push an element to stack and overflow demo");
printf("\n 2.Pop an element from stack and underflow demo");
printf("\n 3.Palindrome demo");
printf("\n 4.Display");
printf("\n 5.Exit");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter an element to be pushed:");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if (item!=-1)
printf("\n Element popped is : %d",item);
break;
case 3:
palindrome();
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("\n Please enter valid choice");
break;
}
}
}

void push(int item)


{
if (top==MAX-1)
{
printf("\n ~~~~Stack overflow~~~~");
return;
}
top=top+1;
s[top]=item;
}

int pop()
{
int item;
if (top==-1)
{
printf("\n ~~~~Stack underflow~~~~");
return -1;
}
item=s[top];
top=top-1;
return item;
}

void display()
{
int i;
if(top==-1)
{
printf("\n ~~~~Stack is empty~~~~");
return;
}
printf("\n stack elements are:\n");
for(i=top;i>=0;i--)
printf("|%d|\n",s[i]);
}

void palindrome()
{
int flag=1,i;
printf("\n Stack content are:\n");
for(i=top;i>=0;i--)
printf("|%d|\n",s[i]);
printf("\n Reverse of stack content are :\n");
for(i=0;i<=top;i++)
printf("|%d|\n",s[i]);
for(i=0;i<=top/2;i++)
{
if(s[i]!=s[top-i])
{
flag=0;
break;
}
}
if(flag==1)
{
printf("\n It is palindrome number");
}
else
{
printf("\n It is not a palindrome number");
}
}

OUTPUT:

~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:1
Enter an element to be pushed:2

~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:1
Enter an element to be pushed:3
OUTPUT:
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:1
Enter an element to be pushed:4

~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:1
Enter an element to be pushed:5
~~~~Stack underflow~~~~

~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:4
stack elements are:
|4|
|3|
|2|
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:3
stack elements are:
|4|
|3|
|2|
Reverse of stack content are:
|2|
|3|
|4|
It is not a palindrome number

~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:2
Element popped is : 4

~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:5
_

LAB PROGRAM 04(running)

Develop a program in C for converting an infix expression to postfix expression.


Program should support for both parenthesized expression with the operators :
+,-,/,*,%,^ and alphanumeric operands.

#include<stdio.h>
#include<stdlib.h>
void evaluate();
void push(char);
char pop();
int prec(char);
int top=-1;
char infix[30],postfix[30],stack[30];
void main()
{
printf("\n Enter the valid infix expression:");
scanf("%s",infix);
evaluate();
printf("\n Entered infix expression is :\n%s\n",infix);
printf("\n The corrresponding postfix expression is :\n%s\n",postfix);
}
void evaluate()
{
int i=0,j=0;
char symb,temp;
push('#');
for(i=0;infix[i]!='\0';i++)
{
symb=infix[i];
switch(symb)
{
case '(':
push(symb);
break;
case ')':
temp=pop();
while(temp!='(')
{
postfix[j]=temp;
j++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
case '$':
while(prec(stack[top])>=prec(symb))
{
temp=pop();
postfix[j]=temp;
j++;
}
push(symb);
break;
default:
postfix[j]=symb;
j++;
}
}
while(top>0)
{
temp=pop();
postfix[j]=temp;
j++;
}
postfix[j]='\0';
}
void push(char item)
{
top=top+1;
stack[top]=item;
}
char pop()
{
char item;
item=stack[top];
top=top-1;
return item;
}
int prec(char symb)
{
int p;
switch(symb)
{
case '#':
p=-1;
break;
case '(':
case ')':
p=0;
break;
case '+':
case '-':
p=1;
break;
case '*':
case '/':
case '%':
p=2;
break;
case '^':return p;
case '$':
p=3;
break;
}
return p;
}
OUTPUT:

Enter the valid infix expression:(a+b)*d/(f+a*d)+c


Entered infix expression is :
(a+b)*d/(f+a*d)+c
The corresponding postfix expression is :
ab+d*fad*+/c+

LAB PROGRAM 05(running)

Develop a program in C for the following stack applications


(a)Evaluation of suffix expression with single digit operands and operators :
+,-,/,*,%,^.
(b)Solving tower of Hanoi problem with n disks

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
int i, top = -1;
int op1, op2, res, s[20];
char postfix[90], symb;

void push(int item) {


top = top + 1;
s[top] = item;
}

int pop() {
int item;
item = s[top];
top = top - 1;
return item;
}

void main() {
printf("\n Enter a valid postfix expression:\n");
scanf("%s", postfix);
for (i = 0; postfix[i] != '\0'; i++) { // Removed the misplaced LAB PROGRAM 05
symb = postfix[i];
if (isdigit(symb)) {
push(symb - '0');
} else {
op2 = pop();
op1 = pop();
switch (symb) {
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
default:
push(0); // You might want to handle this case differently
}
}
}
res = pop();
printf("Result = %d\n", res);
}
OUTPUT:

Enter a valid postfix expression:


456+54-3*++
Result = 18

(b)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void tower(int n,int source,int temp,int destination)
{
if(n==0)
return;
tower(n-1,source,destination,temp);
printf("\nMove disc %d from %c to %c",n,source,destination);
tower(n-1,temp,source,destination);
}
void main()
{
int n;
printf("\nEnter the number of discs : \n");
scanf("%d",&n);
tower(n,'A','B','C');
printf("\n\nTotal Number of moves are : %d",(int)pow(2,n)-1);
}

OUTPUT:

Enter the number of discs:


3
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C
Total Number of moves are : 7

LAB PROGRAM 06(running)

Develop a menu driven program in C for the following operations on circular queue
of characters (Array implementation of queue with maximum size MAX).
(a)Insert an element on to circular QUEUE
(b)Delete an element from circular QUEUE
(c)Demonstrate overflow and underflow situations on circular QUEUE
(d)Display the status of circular QUEUE
(e)Exit
Support the program with appropriate functions for each of the above operations.

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

#define MAX 3

char cq[MAX];
int front = -1, rear = -1;

void insert(char item);


void delete_element();
void display();
void main() {
int ch;
char item;

while (1) {
printf("\n\n~~Menu~~:");
printf("\n==>1. Insertion and overflow demo");
printf("\n==>2. Deletion and underflow demo");
printf("\n==>3. Display");
printf("\n==>4. Exit");
printf("\nEnter Your choice : ");
scanf("%d", &ch);
__fpurge(stdin);
switch (ch) {
case 1:
printf("\n\nEnter an element to be inserted : ");
scanf(" %c", &item);
insert(item);
break;
case 2:
delete_element();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nPlease enter a valid choice");
break;
}
}
}

void insert(char item) {


if (front == (rear + 1) % MAX) {
printf("\n\n~~Circular Queue overflow~~");
} else {
if (front == -1) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX;
}
cq[rear] = item;
}
}

void delete_element() {
char item;
if (front == -1) {
printf("\n\n~~Circular Queue Underflow~~");
} else {
item = cq[front];
printf("\n\nDeleted element from the queue is : %c", item);
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
}

void display() {
int i;
if (front == -1) {
printf("\n\nCircular Queue is Empty");
} else {
printf("\nCircular Queue contents are : \n");
printf("Front[%d]-> ", front);
for (i = front; i != rear; i = (i + 1) % MAX) {
printf("%c ", cq[i]);
}
printf("%c", cq[i]);
printf(" <-[%d]Rear", rear);
}
}
OUTPUT:

~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :1
Enter an element to be inserted :3

~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :1
Enter an element to be inserted : 4

~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :3
Circular queue contents are:
Front[0]->0 0 0 0 <-Rear[1]
0304

~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :2
Deleted element from the queue is : 0 0
03

~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :4
_

LAB PROGRAM 07(running)

Develop a menu driven program in c for the following operations on singly linked list
(SSL) of student data with the field : USN, Name ,program, Sem, Ph.No.
(a)Create a SLL of N students Data by using front insertion
(b)Display the status of SLL and count the number of nodes in it.
(c)Perform insertion/deletion of end of SLL
(d)Perform insertion/deletion at front of SLL(Demonstration of stack)
(e)Exit

#include<stdio.h>
#include<stdlib.h>
struct node
{
char usn[25], name[25], branch[25];
int sem;
int phone;
struct node * link;
};
typedef struct node * NODE;

NODE start = NULL;


int count = 0;

NODE create()
{
NODE snode;
snode = (NODE) malloc(sizeof(struct node));

if (snode == NULL)
{
printf("\nMemory is not available");
exit(1);
}
printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld", snode -> usn, snode -> name, snode -> branch, & snode ->
sem, & snode -> phone);
snode -> link = NULL;
count++;
return snode;
}

NODE insertfront()
{
NODE temp;
temp = create();
if (start == NULL)
{
return temp;
}

temp -> link = start;


return temp;
}

NODE deletefront()
{
NODE temp;
if (start == NULL)
{
printf("\nLinked list is empty");
return NULL;
}

if (start -> link == NULL)


{
printf("\nThe Student node with usn:%s is deleted ", start -> usn);
count--;
free(start);
return NULL;
}
temp = start;
start = start -> link;
printf("\nThe Student node with usn:%s is deleted", temp -> usn);
count--;
free(temp);
return start;
}

NODE insertend()
{
NODE cur, temp;
temp = create();

if (start == NULL)
{
return temp;
}
cur = start;
while (cur -> link != NULL)
{
cur = cur -> link;
}
cur -> link = temp;
return start;
}

NODE deleteend()
{
NODE cur, prev;
if (start == NULL)
{
printf("\nLinked List is empty");
return NULL;
}

if (start -> link == NULL)


{
printf("\nThe student node with the usn:%s is deleted", start -> usn);
free(start);
count--;
return NULL;
}

prev = NULL;
cur = start;
while (cur -> link != NULL)
{
prev = cur;
cur = cur -> link;
}

printf("\nThe student node with the usn:%s is deleted", cur -> usn);
free(cur);
prev -> link = NULL;
count--;
return start;
}

void display()
{
NODE cur;
int num = 1;

if (start == NULL)
{

printf("\nNo Contents to display in SLL \n");


return;
}
printf("\nThe contents of SLL: \n");
cur = start;
while (cur != NULL)
{
printf("\n|%d| |USN:%s| |Name:%s| |Branch:%s| |Sem:%d| |Ph:%ld|", num, cur -> usn,
cur -> name, cur -> branch, cur -> sem, cur -> phone);
cur = cur -> link;
num++;
}
printf("\n No of student nodes is %d \n", count);
}

void stackdemo()
{
int ch;

printf("\n--------Stack Demo using SLL--------\n");


printf("\n1:Push operation \n2: Pop operation \n3: Display \n4:Exit \n");
while (1)
{
printf("\nEnter your choice for stack demo:");
scanf("%d", & ch);
switch (ch)
{
case 1:
start = insertfront();
break;
case 2:
start = deletefront();
break;
case 3:
display();
break;
default:
return;
}
}
return;
}

int main()
{
int ch, i, n;

printf("\n--------Menu--------");
printf("\nEnter your choice for SLL operation \n");
printf("\n1:Create SLL of Student Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n6:Exit \n");
while (1)
{
printf("\nEnter your choice:");
scanf("%d", & ch);

switch (ch)
{
case 1:
printf("\nEnter the no of students: ");
scanf("%d", & n);
for (i = 1; i <= n; i++)
start = insertfront();
break;

case 2:
display();
break;
case 3:
start = insertend();
break;

case 4:
start = deleteend();
break;

case 5:
stackdemo();
break;

case 6:
exit(0);

default:
printf("\n Please enter the valid choice");

}
}
}

OUTPUT:

Enter the number of students N:1


Enter data for node 1:
Enter USN:1ew22cs010
Enter name: anu
Enter the program name: cse
Enter semester:3
Enter the phone no:847654658

SLL OPERATIONS
==========
1.Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6.Exit
Enter your choice
1
Enter USN:1ew22cs011
Enter name: anoop
Enter the program name: cse
Enter semester:3
Enter the phone no:847846553
SLL OPERATIONS
==========
1.Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6.Exit
Enter your choice
2
Enter USN:1ew22cs012
Enter name: akash
Enter the program name: cse
Enter semester:3
Enter the phone no:65845489
SLL OPERATIONS
==========
1.Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6.Exit
Enter your choice
5
The contents of SLL are:
USN
1ew22cs011 anoop cse 3 847846553
1ew22cs010 anu cse 3 847654658
1ew22cs012 akash cse 3 65845489
SLL has 3 nodes
SLL OPERATIONS
==========
1.Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6.Exit
Enter your choice
3
Node deleted is anoop

LAB PROGRAM 08(running)

Develop a menu driven program in c for the following operations on doubly linked
list (DSL) of employee data with the field : SSN, Name ,dept, designation,
sal ,Ph.No.
(a)Create a DLL of N employees Data by using end insertion
(b)Display the status of DLL and count the number of nodes in it.
(c)Perform insertion/deletion of end of DLL
(d)Perform insertion/deletion at front of DLL
(e)Demonstration how this DLL can be used as doubly ended queue
(f)Exit

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

struct node {
char ssn[25], name[25], dept[10], designation[25];
int sal;
long int phone;
struct node *llink;
struct node *rlink;
};

typedef struct node* NODE;


NODE first = NULL;
int count = 0;

NODE create() {
NODE enode = (NODE)malloc(sizeof(struct node));
if (enode == NULL) {
printf("\nRunning out of memory");
exit(0);
}
printf("\nEnter the ssn, Name, Department, Designation, Salary, PhoneNo of the
employee: \n");
scanf("%s %s %s %s %d %ld", enode->ssn, enode->name, enode->dept,
enode->designation, &enode->sal, &enode->phone);
enode->llink = NULL;
enode->rlink = NULL;
count++;
return enode;
}

NODE insertfront() {
NODE temp = create();
if (first == NULL) {
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}
void display() {
NODE cur = first;
int nodeno = 1;

if (cur == NULL) {
printf("\nNo Contents to display in DLL");
return;
}

while (cur != NULL) {

printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Designation:%s|Salary:%d|Phone
no:%ld",
nodeno, cur->ssn, cur->name, cur->dept, cur->designation, cur->sal, cur->phone);
cur = cur->rlink;
nodeno++;
}
printf("\nNo of employee nodes is %d", count);
}

NODE deletefront() {
if (first == NULL) {
printf("\nDoubly Linked List is empty");
return NULL;
}

NODE temp = first;


printf("\nThe employee node with the ssn:%s is deleted", first->ssn);

first = first->rlink;
if (first != NULL) {
first->llink = NULL;
}

free(temp);
count--;
return first;
}

NODE insertend() {
NODE cur, temp = create();
if (first == NULL) {
return temp;
}
cur = first;
while (cur->rlink != NULL) {
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
return first;
}

NODE deleteend() {
if (first == NULL) {
printf("\nDoubly Linked List is empty");
return NULL;
}

NODE cur = first;


while (cur->rlink != NULL) {
cur = cur->rlink;
}

printf("\nThe employee node with the ssn:%s is deleted", cur->ssn);


if (cur->llink != NULL) {
cur->llink->rlink = NULL;
} else {
first = NULL; // List had only one element
}

free(cur);
count--;
return first;
}

void deqdemo() {
int ch;
printf("\nDemo Double Ended Queue Operation");

printf("\n1:InsertQueueFront\n2:DeleteQueueFront\n3:InsertQueueRear\n4:DeleteQueueRe
ar\n5:DisplayStatus\n6:Exit\n");
while (1) {

printf("\nenter your choice:");


scanf("%d", &ch);
switch (ch) {
case 1: first = insertfront(); break;
case 2: first = deletefront(); break;
case 3: first = insertend(); break;
case 4: first = deleteend(); break;
case 5: display(); break;
default: return;
}
}
}
int main() {
int ch, i, n;
printf("\n\n~~~Menu~~~");
printf("\n1:Create DLL of Employee Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront");
printf("\n7:Double Ended Queue Demo using DLL");
printf("\n8:Exit\n");
while (1) {

printf("\nPlease enter your choice: ");


scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter the no of Employees: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
first = insertend();
}
break;
case 2: display(); break;
case 3: first = insertend(); break;
case 4: first = deleteend(); break;
case 5: first = insertfront(); break;
case 6: first = deletefront(); break;
case 7: deqdemo(); break;
case 8: exit(0);
default: printf("\nPlease Enter a valid choice");
}
}
return 0;
}

OUTPUT:

Enter the number of Employees N:


Enter Data for node 1:
Enter SSN:1
Enter name: a
enter department: testing
Enter designation: se
Enter salary:10000
Enter Phone No:7546
DLL OPERATIONS
=========
1. Insert Rear
2. Delete Front
3. Insert Front
4. Delete Rear
5. Display
6. Exit
Enter your choice:
1
Enter SSN:2
Enter name: b
enter department: testing
Enter designation: ju
Enter salary:3000
Enter Phone No:9808
DLL OPERATIONS
=========
1. Insert Rear
2. Delete Front
3. Insert Front
4. Delete Rear
5. Display
6. Exit
Enter your choice:
5
The contents of DLL are:
SSN Name Dept Designation salary Phone.No
1 a testing se 10000 7546
2 b testing ju 3000 9808
DLL has 2 nodes.
DLL OPERATIONS
=========
1. Insert Rear
2. Delete Front
3. Insert Front
4. Delete Rear
5. Display
6. Exit
Enter your choice:
2
Node deleted for a

LAB PROGRAM 09(running)

Develop a program in C for the following operations singly circular linked list (SSL)
with header nodes
(a)Represent and evaluate a polynomial P(x,y,z)=6x^2y^2z-4yz^5+3x^3yz+2xy^5z-
2xyz^3
(b)Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z)
and store the result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations.

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define COMPARE(x, y)((x == y) ? 0 : (x > y) ? 1 : -1)

struct node
{
int coef;
int xexp, yexp, zexp;
struct node * link;
};
typedef struct node * NODE;

NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if (x == NULL)
{
printf("Running out of memory \n");
return NULL;
}
return x;
}

NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp -> coef = coef;
temp -> xexp = xexp;
temp -> yexp = yexp;
temp -> zexp = zexp;
cur = head -> link;
while (cur -> link != head)
{
cur = cur -> link;
}
cur -> link = temp;
temp -> link = head;
return head;
}

NODE read_poly(NODE head)


{
int i, j, coef, xexp, yexp, zexp, n;
printf("\nEnter the no of terms in the polynomial: ");
scanf("%d", & n);
for (i = 1; i <= n; i++)
{
printf("\n\tEnter the %d term: ", i);
printf("\n\t\tCoef = ");
scanf("%d", & coef);
printf("\n\t\tEnter Pow(x) Pow(y) and Pow(z): ");
scanf("%d", & xexp);
scanf("%d", & yexp);
scanf("%d", & zexp);
head = attach(coef, xexp, yexp, zexp, head);
}
return head;
}

void display(NODE head)


{
NODE temp;
if (head -> link == head)
{
printf("\nPolynomial does not exist.");
return;
}
temp = head -> link;

while (temp != head)


{
printf("%dx^%dy^%dz^%d", temp -> coef, temp -> xexp, temp -> yexp, temp ->
zexp);
temp = temp -> link;
if (temp != head)
printf(" + ");
}
}

int poly_evaluate(NODE head)


{
int x, y, z, sum = 0;
NODE poly;

printf("\nEnter the value of x,y and z: ");


scanf("%d %d %d", & x, & y, & z);

poly = head -> link;


while (poly != head)
{
sum += poly -> coef * pow(x, poly -> xexp) * pow(y, poly -> yexp) * pow(z, poly ->
zexp);
poly = poly -> link;
}
return sum;
}

NODE poly_sum(NODE head1, NODE head2, NODE head3)


{
NODE a, b;
int coef;
a = head1 -> link;
b = head2 -> link;

while (a != head1 && b != head2)


{
while (1)
{
if (a -> xexp == b -> xexp && a -> yexp == b -> yexp && a -> zexp == b -> zexp)
{
coef = a -> coef + b -> coef;
head3 = attach(coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
b = b -> link;
break;
}
if (a -> xexp != 0 || b -> xexp != 0)
{
switch (COMPARE(a -> xexp, b -> xexp))
{
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;

case 0:
if (a -> yexp > b -> yexp)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
else if (a -> yexp < b -> yexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
else if (a -> zexp > b -> zexp)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
else if (a -> zexp < b -> zexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
if (a -> yexp != 0 || b -> yexp != 0)
{
switch (COMPARE(a -> yexp, b -> yexp))
{
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
case 0:
if (a -> zexp > b -> zexp)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
else if (a -> zexp < b -> zexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
if (a -> zexp != 0 || b -> zexp != 0)
{
switch (COMPARE(a -> zexp, b -> zexp))
{
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
}
}
while (a != head1)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
}
while (b != head2)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
}
return head3;
}

void main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode();
head1 = getnode();
head2 = getnode();
head3 = getnode();

head -> link = head;


head1 -> link = head1;
head2 -> link = head2;
head3 -> link = head3;
while (1)
{
printf("\n--------Menu--------");
printf("\n1.Represent and Evaluate a Polynomial P(x,y,z)");
printf("\n2.Find the sum of two polynomials POLY1(x,y,z)");
printf("\nEnter your choice:");
scanf("%d", & ch);
switch (ch)
{
case 1:
printf("\n----Polynomial evaluation P(x,y,z)----\n");
head = read_poly(head);
printf("\nRepresentation of Polynomial for evaluation: \n");
display(head);
res = poly_evaluate(head);
printf("\nResult of polynomial evaluation is : %d \n", res);
break;

case 2:
printf("\nEnter the POLY1(x,y,z): \n");
head1 = read_poly(head1);
printf("\nPolynomial 1 is: \n");
display(head1);

printf("\nEnter the POLY2(x,y,z): \n");


head2 = read_poly(head2);
printf("\nPolynomial 2 is: \n");
display(head2);

printf("\nPolynomial addition result: \n");


head3 = poly_sum(head1, head2, head3);
display(head3);
break;
case 3:
exit(0);
}
}
}
OUTPUT:

--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:1

----Polynomial evaluation P(x,y,z)----


Enter the no of terms in the polynomial: 2

Enter the 1 term:


Coef = 3

Enter Pow(x) Pow(y) and Pow(z): 3 4 5

Enter the 2 term:


Coef = 2

Enter Pow(x) Pow(y) and Pow(z): 2 1 3

Representation of Polynomial for evaluation:


3x^3y^4z^5 + 2x^2y^1z^3
Enter the value of x,y and z: 2 3 5

Result of polynomial evaluation is : 6078000

--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:2

Enter the POLY1(x,y,z):

Enter the no of terms in the polynomial: 2

Enter the 1 term:


Coef = 1

Enter Pow(x) Pow(y) and Pow(z): 2 3 4

Enter the 2 term:


Coef = 2

Enter Pow(x) Pow(y) and Pow(z): 1 1 1

Polynomial 1 is:
1x^2y^3z^4 + 2x^1y^1z^1
Enter the POLY2(x,y,z):

Enter the no of terms in the polynomial: 2

Enter the 1 term:


Coef = 2

Enter Pow(x) Pow(y) and Pow(z): 2 3 1


Enter the 2 term:
Coef = 1

Enter Pow(x) Pow(y) and Pow(z): 1 1 1

Polynomial 2 is:
2x^2y^3z^1 + 1x^1y^1z^1
Polynomial addition result:
1x^2y^3z^4 + 2x^2y^3z^1 + 3x^1y^1z^1
--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:
=== Session Ended. Please Run the code again ===

LAB PROGRAM 10(running)

Develop a menu driven program in C for the following operations on Binary search
tree (BST) of integers.
(a)Create a BST of N integers:6,9,5,2,8,15,24,14,7,8,15,2
(b)Traverse the BST in inorder, preorder, postorder
(c)Search the BST for a given element (KEY) and report the appropriate message
(d)Exit

#include<stdio.h>
#include<stdlib.h>
struct BST{
int data;
struct BST * lchild;
struct BST * rchild;
};
typedef struct BST* NODE;
NODE create()
{
NODE temp;
temp=(NODE)malloc(sizeof(struct BST));
printf("\nEnter the value:");
scanf("%d",&temp->data);
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}

void insert(NODE root,NODE newnode);


void inorder(NODE root);
void preorder(NODE root);
void postorder(NODE root);
void search(NODE root);
void insert(NODE root,NODE newnode)
{
if(newnode->data<root->data)
{
if(root->lchild==NULL)
root->lchild=newnode;
else
insert(root->lchild,newnode);
}
if(newnode->data>root->data)
{
if(root->rchild==NULL)
root->rchild=newnode;
else
insert(root->rchild,newnode);
}
}

void search(NODE root){


int key;
NODE cur;
if(root==NULL){
printf("\nBST is empty:");
return;
}
printf("\nEnter Element to be searched:");
scanf("%d",&key);
cur=root;
while(cur!=NULL){
if(cur->data==key){
printf("\nKey element is present in BST:");
return;
}
if(key<cur->data)
cur=cur->lchild;
else
cur=cur->rchild;
}
printf("\nKey elements not found in BST:");
}

void inorder(NODE root)


{
if(root!=NULL)
{
inorder(root->lchild);
printf("%d",root->data);
inorder(root->rchild);
}
}

void preorder(NODE root)


{
if(root!=NULL)
{
printf("%d",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}

void postorder(NODE root)


{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%d",root->data);
}
}

void main()
{
int ch,key,val,i,n;
NODE root=NULL,newnode;
while(1)
{
printf("\n~~~BST MENU~~~~~");
printf("\n1.Creat a BST:");
printf("\n2.BST Traversal:");
printf("\n3.search:");
printf("\n4.Exit:");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the number of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
newnode=create();
if(root==NULL)
root=newnode;
else
insert(root,newnode);
}
break;

case 2:if(root==NULL)
printf("\nTree is not created:");
else
{
printf("\nThe preorder display:");
preorder(root);
printf("\nThe inorder display:");
inorder(root);
printf("\nThe postorder display:");
postorder(root);
}

break;
case 3:search(root);
break;
case 4:exit(0);
}
}
}
OUTPUT:

~~~BST MENU~~~~~
1.Creat a BST:
2.Search:
3.BST Transversal:
4.Exit:
Enter your choice:1
Enter the number of elements:3
Enter the value:2
Enter the value:1
Enter the value:3

~~~BST MENU~~~~~
1.Creat a BST:
2.Search:
3.BST Transversal:
4.Exit:
Enter your choice:3
The preorder display : 2 1 3
The inorder display : 1 2 3
The postorder display : 1 3 2

~~~BST MENU~~~~~
1.Creat a BST:
2.Search:
3.BST Transversal:
4.Exit:
Enter your choice:2
Enter element to be searched :1
Key element is present in BST
LAB PROGRAM 11(running)

Develop a program in C for the following operations on Graph (G) of cities.


(a)Create a graph of N cities using Adjacency matrix
(b)Print all the nodes reachable from a given starting node in a digraph using
DFS/BFS method.

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

int a[10][10], n, m, i, j, source, s[10], b[10];


int visited[10];

void create() {
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
}

void bfs() {
int q[10], u, front = 0, rear = -1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: \n%d", source);
while (front <= rear) {
u = q[front++];
for (i = 1; i <= n; i++) {
if (a[u][i] == 1 && visited[i] == 0) {
q[++rear] = i;
visited[i] = 1;
printf("\n%d", i);
}
}
}
for (i = 1; i <= n; i++) {
if (visited[i] == 0)
printf("\nThe vertex that is not reachable %d", i);
}
}

void dfs(int source) {


int v;
s[source] = 1;
b[source] = 1;
printf("\n%d ", source);
for (v = 1; v <= n; v++) {
if (a[source][v] == 1 && b[v] == 0) {
dfs(v);
}
}
}

void main() {
int ch;
while (1) {
printf("\n1. Create Graph\n2. BFS\n3. DFS\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: create(); break;
case 2: bfs(); break;
case 3:
printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d", &source);
printf("Reachable nodes\n");
dfs(source);
break;
default: exit(0);
}
}
}
OUTPUT:

Enter number of vertices in graph : 3


Enter the adjacency matrix:
3
3
2
1
5
4
3
6
2
Enter the starting vertex:2
==>1.BFS:Print all nodes reachable from a given starting node
==>2.DFS:Print all nodes reachable from a given starting node
==>3.Exit
Nodes reachable from starting vertex 2 are: 1
The vertex that is not reachable is 3

LAB PROGRAM 12(running)

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 an 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.

#include<stdio.h>
#include<stdlib.h>
int key[20],n,m;
int*ht,indx;
int count=0;
void insert(int key)
{
indx=key%m;
while(ht[indx]!=-1)
{
indx=(indx+1)%m;
}
ht[indx]=key;
count++;
}
void display()
{
int i;
if(count==0)
{
printf("\n hash table is empty");
return;
}
printf("\n hash table contents are:\n");
for(i=0;i<m;i++)
printf("\nT[%d]->%d",i,ht[i]);
}
void main()
{
int i;
printf("\nenter the number of employee records[N]:");
scanf("%d",&n);
printf("\n enter two digit memory location (m) for hash table:");
scanf("%d",&m);
ht=(int*)malloc(m*sizeof(int));
for(i=0;i<m;i++)
ht[i]=-1;
printf("\n enter four digit key values (k) for N employee records:\n");
for(i=0;i<n;i++)
scanf("%d",&key[i]);
for(i=0;i<n;i++)
{
if(count==m)
{
printf("\n~~~~~hash table is full.cannot insert the record %d key~~~~",i+1);
break;
}
insert(key[i]);
}
display();
}
OUTPUT:

enter the number of employee records[N]:2


enter two digit memory location (m) for hash table:21
enter four digit key values (k) for N employee records:
2345
2314
Hash table contents are:
T[0]->-1
T[1]->-1
T[2]->-1
T[3]->-1
T[4]->2314
T[5]->-1
T[6]->-1
T[7]->-1
T[8]->-1
T[9]->-1
T[10]->-1
T[11]->-1
T[12]->-1
T[13]->-1
T[14]->2345
T[15]->-1
T[16]->-1
T[17]->-1
T[18]->-1
T[19]->-1
T[20]->-1
***************************************

You might also like