0% found this document useful (0 votes)
105 views54 pages

Data Structures With C: Third Semester Cs

This document contains a laboratory manual for the third semester Data Structures course. It provides four programs as examples for creating and manipulating sequential files, string functions without libraries, converting IP addresses to integers, and implementing a stack with push, pop and display operations. The programs demonstrate creating records in a file, searching and displaying records, copying and concatenating strings, parsing an IP address, and building a stack to perform basic operations.

Uploaded by

manoharprajapati
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views54 pages

Data Structures With C: Third Semester Cs

This document contains a laboratory manual for the third semester Data Structures course. It provides four programs as examples for creating and manipulating sequential files, string functions without libraries, converting IP addresses to integers, and implementing a stack with push, pop and display operations. The programs demonstrate creating records in a file, searching and displaying records, copying and concatenating strings, parsing an IP address, and building a stack to perform basic operations.

Uploaded by

manoharprajapati
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 54

DATA STRUCTURES WITH C

LABORATORY MANUAL

THIRD SEMESTER CS

FACULTY: Mr.Manohar Prajapati


BMIT

Program 1.
Write a c program to create a sequential file with atleast 5 records. Write necessary
functions
a. To display all the records in the file.
b. To search for a specific record based on the USN. In case the record is not
found, suitable message should be displayed. Both options in this case must be
demonstrated.

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

typedef struct
{
int usn;
char name[25];
int marks1;
int marks2;
int marks3;
}STUDENT;

int search_record(int key_usn,FILE *fp)


{
STUDENT st;
for(;;)
{
fscanf(fp,"%d %s %d %d %d",&st.usn, st.name, &st.marks1, &st.marks2,
&st.marks3);
if (feof(fp))break;
if (key_usn==st.usn)return 1;
}
return 0;
}

DEPARTMENT OF CSE Data Structure Lab Manual


2
BMIT

void display_records(FILE *fp,int pass)


{
STUDENT st;
printf("USN\tName\t\t\t Marks1\tMarks2\tMarks3\n");
for(;;)
{
fscanf(fp,"%d %s %d %d %d" ,&st.usn, st.name, &st.marks1, &st.marks2, &st.marks3);
if(feof(fp))break;
if(pass==st.usn)
{
cprintf("%-5d %-10s",st.usn,st.name);
printf("\t\t");
cprintf(" %-5d %-5d %-5d",st.marks1,st.marks2,st.marks3);
printf("\n");
}
else
printf("%-5d %-10s\t\t %-5d %-5d %5d\n", st.usn, st.name, st.marks1,
st.marks2, st.marks3);
}
}

void append_record(FILE *fp)


{
STUDENT st;
printf("USN : ");
scanf("%d",&st.usn);
printf("Name : ");
scanf("%s",st.name);
printf("Marks1 : ");
scanf("%d",&st.marks1);
printf("Marks2 : ");
scanf("%d",&st.marks2);
printf("Marks3 : ");
scanf("%d",&st.marks3);
fprintf(fp,"%d %s %d %d %d\n",st.usn,st.name,st.marks1,st.marks2,st.marks3);

DEPARTMENT OF CSE Data Structure Lab Manual


3
BMIT

void main()
{
STUDENT st;
char fname[12];
FILE *fp;
int key_usn;
int key = 0;
int flag;
int(choice);
clrscr();
printf("Enter the file name : ");
scanf("%s",fname);
for(;;)
{
printf("\n\n1.Insert Record\n2.Search Record\n");
printf("3.Display Record\n4.Quit\n\n\n");
printf("Enter the choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
fp = fopen(fname,"a+");
if(fp==NULL)
{
printf("File Open Failed");
break;
}
append_record(fp);
fclose(fp);
break;
case 2:
fp = fopen(fname,"r");

DEPARTMENT OF CSE Data Structure Lab Manual


4
BMIT

if(fp==NULL)
{
printf("File Open Failed");
break;
}
printf("Enter USN : ");
scanf("%d",&key_usn);
flag = search_record(key_usn,fp);
if (flag==0)
printf("Unsuccessful search\n");
else
{
printf("Successful search\n");
fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File Open Failed");
break;
}
display_records(fp,key_usn);
}
fclose(fp);
break;

case 3:
fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File Open Failed");
break;
}
display_records(fp,key);
fclose(fp);
break;
default:

DEPARTMENT OF CSE Data Structure Lab Manual


5
BMIT

exit(0);
}
}
}
Program 2.
Write and demonstrate the following C program.
a) newstrcpy that does the same job as strcpy
b) newstrcat that does the same job as strcat without using any library functions.

#include<stdio.h>
#include<conio.h>

void newstrcpy(char *dst,char *source)


{
while(*source!='\0')
{
*dst=*source;
dst++;
source++;
}
*dst='\0';
}

void newstrcat(char *s1,char *s2)


{
while(*s1!='\0')
s1++;
while(*s2!='\0')
{
*s1=*s2;
s1++;
s2++;
}
*s1='\0';
}

DEPARTMENT OF CSE Data Structure Lab Manual


6
BMIT

void main()
{
char str1[20],str2[20];
int choice;
clrscr();
while(choice!=3)
{
printf("\nEnter the first string:");
scanf("%s",str1);
printf("\nEnter the second string:");
scanf("%s",str2);
printf("\nEntered string 1: %s , string 2 : %s",str1,str2);
printf("\nEnter1 to copy and 2 to concatinate:");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: newstrcpy(str1,str2);
break;
case 2: newstrcat(str1,str2);
break;
default:printf("undefined operation");
}

printf("String 1: %s\n",str1);
printf("String 2: %s\n",str2);
printf("press 2 to continue or 3 to exit:");
scanf("%d",&choice);
}
getch();
}

asdasd

DEPARTMENT OF CSE Data Structure Lab Manual


7
BMIT

Program - 3
Write a C program which accepts the IP address in decimal dot format and converts it
into a 32 bit long integer using strtok library functions and unions.

#include<stdio.h>

union x
{
unsigned long add;
char c[4];
}ip;

main()
{
char ch, **end,inp[16],*p;
int i=0;
clrscr();
printf("Enter the IP address: ");
fflush(stdin);
gets(inp);
p=strtok(inp,".");
ip.c[3]=strtol(p,end,10);
for(i=2;i>=0;i--)
{
p=strtok(NULL,".");
ip.c[i]= strtol(p,end,10);
}

DEPARTMENT OF CSE Data Structure Lab Manual


8
BMIT

printf("Converted address is : %lu",ip.add);


getch();
}

Program 4.
Write a C program to construct a stack of integers to perform the
following operations:
a.Push
b.Pop
c.Display
It must print appropriate messages for Overflow, Underflow and empty.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define stack_size 2

int ele,i;
int flag=1;
struct stack
{
int item[5];
int top;
}s;

void push(struct stack *ptr, int ele)


{
if(ptr->top==stack_size-1)
printf("\n Overflow.\n");
else

DEPARTMENT OF CSE Data Structure Lab Manual


9
BMIT

{
ptr->top++;
ptr->item[ptr->top]=ele;
}
}

void pop(struct stack *ptr)


{
if(ptr->top==-1)
{
printf("\nUnderflow.\n");
}
else
printf("\nThe popped value is %d.",(ptr->item[(ptr->top)--]));
}

void display(struct stack *ptr)


{
if(ptr->top==-1)
printf("\nEmpty Stack.\n");
else
{
printf("\n\n");
for(i=0;i<=ptr->top; i++)
printf("%d\t",ptr->item[i]);
}
}

main()
{
clrscr();
s.top=-1;
while(1)
{

DEPARTMENT OF CSE Data Structure Lab Manual


10
BMIT

printf("\n\n1.PUSH\n2.POP\n3.DISPLAY\n4.Exit");
printf("\nEnter your Choice: ");
scanf("%d",&i);

switch(i)
{
case 1: printf("\nEnter the element: ");
scanf("%d",&ele);
push(&s,ele);
break;
case 2: pop(&s);
break;
case 3: display(&s);
break;
case 4: exit(0);
break;
default: printf("\nInvalid Input.");
}
}
getch();
}

DEPARTMENT OF CSE Data Structure Lab Manual


11
BMIT

Program – 5

Write a C program to convert & print a valid parenthesized infix expression to postfix.
The expression should consist of single character operands and binary operators.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>

#define stack_size 25

int elem,i;
char str[25],res[25];

char stack[25];
int top;

void push(char ele)


{
if(top==stack_size-1)
printf("\n Overflow.\n");

DEPARTMENT OF CSE Data Structure Lab Manual


12
BMIT

else
{
top++;
stack[top]=ele;
}
}

char pop()
{
if(top==-1)
{
printf("\nUnderflow.\n");
return (-1);
}
else
return (stack[top--]);
}

void convert()
{
int i=0,j=0;

for(i=0;str[i]!='\0';i++)
{
if(isalpha(str[i]) || isdigit(str[i]))
res[j++]=str[i];
else
switch(str[i])
{
case '(': push(str[i]); break;
case '$': while(stack[top]=='$')
res[j++]=pop();
push(str[i]);
break;
case '/':

DEPARTMENT OF CSE Data Structure Lab Manual


13
BMIT

case '*':while(stack[top]=='$' || stack[top]=='/'


||stack[top]=='*')
res[j++]=pop();
push(str[i]);
break;
case '+':
case '-':while(stack[top]=='$'||stack[top]=='*'||
stack[top]=='/'||stack[top]=='+'
||stack[top]=='-')
res[j++]=pop();
push(str[i]);
break;
case ')': while(stack[top]!='(')
res[j++]=pop();
pop();
break;
}
}
while(top>-1)
res[j++]=pop();
res[j]='\0';
printf("Postfix Expression is: %s ",res);
}

main()
{
clrscr();
top=-1;
printf("Enter a valid infix expression: ");
scanf("%s",str);
convert();
getch();
}

DEPARTMENT OF CSE Data Structure Lab Manual


14
BMIT

Program 6.

Wrie a C program to evaluate a valid postfix expression using a stack. Assume that the
postfix expression is read as a single line consisting of non negative singe digit operands
and binary arithmatic operators. The operators are +,-,*,/.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>
#define stack_size 10

int elem,i;
char str[20];

struct stack
{
int item[10];
int top;
}s;

DEPARTMENT OF CSE Data Structure Lab Manual


15
BMIT

void push(struct stack *ptr, int ele)


{
if(ptr->top==stack_size-1)
printf("\n Overflow.\n");
else
{
ptr->top++;
ptr->item[ptr->top]=ele;
}
}

int pop(struct stack *ptr)


{
if(ptr->top==-1)
{
printf("\nUnderflow.\n");
return (-1);
}
else
return (ptr->item[(ptr->top)--]);
}

int evaluate()
{
int a,b,res;
for(i=0;str[i]!='\0';i++)
{
if(isdigit(str[i]))
push(&s,str[i]-'0');
else
{
b=pop(&s);
a=pop(&s);
s

DEPARTMENT OF CSE Data Structure Lab Manual


16
BMIT

witch(str[i])
{
case '/': res=a/b;break;
case '*': res=a*b;break;
case '-': res=a-b;break;
case '+': res=a+b;break;
}
push(&s,res);
}
}
res=pop(&s);
return res;
}

main()
{
clrscr();
s.top=-1;
printf("Enter a valid postfix expression: ");
scanf("%s",str);
printf("\n\n Final result is : %d",evaluate());
getch();
}

DEPARTMENT OF CSE Data Structure Lab Manual


17
BMIT

Program 7:
Write a program to simulate the working of queue of integers using an array. Provide the
following operations: a] Insert b] Delete c] Display

#include<stdio.h>
#include<conio.h>
#define MAX 50
struct queue
{
int ele[MAX];
int rear;
};
void insert(struct queue *,int);
int removeElement(struct queue *);
int isEmpty(struct queue *);
int isFull(struct queue *);
void displayQueue(struct queue *);

DEPARTMENT OF CSE Data Structure Lab Manual


18
BMIT

int menu();
void main()
{
int element,choice;
struct queue q;
q.rear=-1;
do
{
choice=menu();
switch (choice)
{
case 1:
if(isFull(&q))
printf("\n The queue is full");
else
{
printf("\n Enter the element to be inserted : ");
scanf("%d",&element);
insert(&q,element);
}
break;
case 2:
if(isEmpty(&q))
printf("\n Queue is empty");
else
printf("\n Element removed = %d",removeElement(&q));
break;
case 3:
if(isEmpty(&q))
printf("\n The queue is empty");
else
printf("\n The queue is not empty");
break;
case 4:
if(isFull(&q))

DEPARTMENT OF CSE Data Structure Lab Manual


19
BMIT

printf("\n The queue is full");


else
printf("\n The queue is not full");
break;
case 5:
displayQueue(&q);
break;
case 6:
printf("\n Thank You");
break;
default:
printf("\n Please enter your choice correctly");
break;
}
}while(choice!=6);
getch();
}
int menu()
{
int ch;
getch();
clrscr();
printf("\n 1-Insert an element into the queue");
printf("\n 2-Remove an element from the queue");
printf("\n 3-Check whether the queue is empty");
printf("\n 4-Check whether the queue is full");
printf("\n 5-Display the elements of the queue");
printf("\n 6-Exit");
printf("\n Enter your choice : \t");
scanf("%d",&ch);
return ch;
}
void insert(struct queue *pq, int e)
{
pq->rear++;

DEPARTMENT OF CSE Data Structure Lab Manual


20
BMIT

pq->ele[pq->rear]=e;
}
int removeElement(struct queue *pq)
{
int eleRem=pq->ele[0],i;
for(i=1;i<=pq->rear;i++)
pq->ele[i-1]=pq->ele[i];
pq->rear--;
return eleRem;
}
int isEmpty(struct queue *pq)
{
if(pq->rear==-1)
return 1;
else
return 0;
}
int isFull(struct queue *pq)
{
if(pq->rear ==(MAX-1))
return 1;
else
return 0;
}
void displayQueue(struct queue *pq)
{
int i;
if(isEmpty(pq))
printf("\n The queue is empty");
else
{
printf("\n Elements of the queue from front to rear are : ");
for(i=0;i<=pq->rear;i++)
printf("\t %d",pq->ele[i]);
}

DEPARTMENT OF CSE Data Structure Lab Manual


21
BMIT

Program 7:
Write a program to simulate the working of circular queue of integers using an array.
Provide the following operations: a] Insert b] Delete c] Display

#include<stdio.h>
#include<conio.h>
#define MAX 50

struct queue
{
int ele[MAX];
int front,rear;

DEPARTMENT OF CSE Data Structure Lab Manual


22
BMIT

};

void insert(struct queue *,int);


int removeElement(struct queue *);
int isEmpty(struct queue *);
int isFull(struct queue *);
void displayQueue(struct queue *);
int menu();

void main()
{
int element,choice;
struct queue q;
q.rear=MAX-1;
q.front=MAX-1;

do
{
choice=menu();

switch (choice)
{
case 1:
if(isFull(&q))
printf("\n The queue is full");
else
{
printf("\n Enter the element to be inserted : ");
scanf("%d",&element);
insert(&q,element);
}
break;

DEPARTMENT OF CSE Data Structure Lab Manual


23
BMIT

case 2:
if(isEmpty(&q))
printf("\n Queue is empty");
else
printf("\n Element removed = %d",removeElement(&q));
break;

case 3:
if(isEmpty(&q))
printf("\n The queue is empty");
else
printf("\n The queue is not empty");
break;

case 4:
if(isFull(&q))
printf("\n The queue is full");
else
printf("\n The queue is not full");
break;

case 5:
displayQueue(&q);
break;

case 6:
printf("\n We are now going to exit the queue program");
printf("\n Thank You");
break;

default:
printf("\n Please enter your choice correctly");

DEPARTMENT OF CSE Data Structure Lab Manual


24
BMIT

break;
}
}while(choice!=6);
getch();
}
int menu()
{
int ch;
printf("\n\n\n\n Press any key to continue...........");
getch();
clrscr();
printf("\n Circular Queue Implementation Program");
printf("\n 1-Insert an element into the queue");
printf("\n 2-Remove an element from the queue");
printf("\n 3-Check whether the queue is empty");
printf("\n 4-Check whether the queue is full");
printf("\n 5-Display the elements of the queue");
printf("\n 6-Exit");
printf("\n Enter your choice (1-6) : \t");
scanf("%d",&ch);
return ch;
}
void insert(struct queue *pq, int e)
{
if(pq->rear==MAX-1)
pq->rear=0;
else
pq->rear++;
pq->ele[pq->rear]=e;
}
int removeElement(struct queue *pq)
{
if(pq->front==MAX-1)
pq->front=0;
else

DEPARTMENT OF CSE Data Structure Lab Manual


25
BMIT

pq->front++;
return (pq->ele[pq->front]);
}
int isEmpty(struct queue *pq)
{
if(pq->rear==pq->front)
return 1;
else
return 0;
}
int isFull(struct queue *pq)
{
int i;
if(pq->rear ==(MAX-1))
i=0;
else
i=pq->rear+1;
if(i==pq->front)
return 1;
else
return 0;
}
void displayQueue(struct queue *pq)
{
int i;
if(isEmpty(pq))
printf("\n The queue is empty");
else
{
if(pq->front==MAX-1)
i=0;
else
i=pq->front+1;

printf("\n Elements of the queue from front to rear are : ");

DEPARTMENT OF CSE Data Structure Lab Manual


26
BMIT

while(i!=pq->rear)
{
printf("\t %d",pq->ele[i]);
if(i==MAX-1)
i=0;
else
i++;
}
printf("\t %d",pq->ele[i]);
}
}

Program 9:
Write a C program using dynamic variables and pointers, to construct a singly linked list
containing of the following information in each node: student ID(int), name(string) and
semester(int). The operations to be supported are:
a] The insertion operation:
i] At the front of the list
ii] At the back of the list
iii] At any position of the list

DEPARTMENT OF CSE Data Structure Lab Manual


27
BMIT

b] Deleting a node based on student ID. If the specified node is not present in the
list, an error message to be displayed.
c] Searching a node based on student ID and update the information content. If the
specified node is not present in the list, an error message to be displayed.
d] Displaying all nodes of a file.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
int id;
char name[20];
int sem;
struct node *next;
};

void insertfront(struct node**,struct node**);


void insertback(struct node**,struct node**);
void insertpos(struct node**,struct node**);
void rem(struct node**,struct node**);
void search(struct node*,int);
void display(struct node*);

void main()
{
int choice,id;
struct node *f=NULL;
struct node *r=NULL;
clrscr();
do
{

DEPARTMENT OF CSE Data Structure Lab Manual


28
BMIT

printf("\n1.Insert front\n2.Insert back\n3.Insert at specified position\n4.delete


id\n5.Search id\n6.Display\n7.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: insertfront(&f,&r);
break;
case 2: insertback(&f,&r);
break;
case 3: insertpos(&f,&r);
break;
case 4: rem(&f,&r);
break;
case 5: printf("\nEnter id to search...\n");
scanf("%d",&id);
search(f,id);
break;
case 6: display(f);
break;
case 7: break;
}
}while(choice!=7);
getch();
}

void insertfront(struct node**f,struct node **r)


{
struct node*p;
p=(struct node*)malloc(sizeof(struct node));
printf("\nEnter name...\n");
scanf("%s",p->name);
printf("Enter ID(integer)...\n");
scanf("%d",&p->id);
printf("Enter Semester...\n");
scanf("%d",&p->sem);

DEPARTMENT OF CSE Data Structure Lab Manual


29
BMIT

if(*f==NULL)
{
(*f)=(*r)=p;
p->next = NULL;
}
else
{
p->next=(*f);
(*f)=p;
}
}

void insertback(struct node**f,struct node **r)


{
struct node*p;
p=(struct node*)malloc(sizeof(struct node));
printf("\nEnter name...\n");
scanf("%s",p->name);
printf("Enter ID(integer)...\n");
scanf("%d",&p->id);
printf("Enter Semester...\n");
scanf("%d",&p->sem);
if((*f)==NULL)
{
(*f)=(*r)=p;
p->next=NULL;
}
else
{
p->next=NULL;
(*r)->next=p;
(*r)=p;
}
}

DEPARTMENT OF CSE Data Structure Lab Manual


30
BMIT

void insertpos(struct node**f,struct node**r)


{
int pos,count=0;
struct node*p,*q,*s;
q=(*f);
p=(struct node*)malloc(sizeof(struct node));
printf("\nEnter postion to enter...\n");
scanf("%d",&pos);
printf("\nEnter name...\n");
scanf("%s",p->name);
printf("Enter ID(integer)...\n");
scanf("%d",&p->id);
printf("Enter Semester...\n");
scanf("%d",&p->sem);
while((q->next!=NULL)&&(count!=(pos-1)))
{
count++;
s=q;
q=q->next;
}
if(count<(pos-1))
{printf("\nInvalid postion\n");}
else
{
p->next=s->next;
s->next=p;
if(p->next==NULL)
{(*r)=p;}
}
}

void rem(struct node **f,struct node **r)


{
int i;
struct node *p,*q;

DEPARTMENT OF CSE Data Structure Lab Manual


31
BMIT

printf("\nEnter ID(integer) to delete...\n");


scanf("%d",&i);
p=*f;
while((p->next!=NULL)&&(p->id!=i))
{
q=p;
p=p->next;
}
if(p->id==i)
{
if((*f)==p && (*f)->next==NULL)
{printf("%s",p->name);}
else if ((*r)==p)
{
printf("%s",p->name);
q->next=NULL;
(*r)=q;
}
else {
printf("%s",p->name);
q->next=p->next;
}
free(p);
}
else
{printf("\nNODE NOT FOUND...\n");}
}

void search(struct node *f,int i)


{
printf("\nEnter ID to search...\n");
while((f->next!=NULL)&&(f->id!=i))
{
f=f->next;
}

DEPARTMENT OF CSE Data Structure Lab Manual


32
BMIT

if (f->id==i)
{
printf("\nRecord FOUND...\n");
printf("\nNAME-%s\nSEMESTER-%d",f->name,f->sem);
}
else
{
printf("\nSorry SEARCH FAILED...\n");
}
}

void display(struct node *f)


{
do
{
printf("\n%s\t%d\t%d",f->name,f->id,f->sem);
f=f->next;
}while(f!=NULL);
}

Program 10.

DEPARTMENT OF CSE Data Structure Lab Manual


33
BMIT

Write a C program using dynamic variables and pointers to construct a stack of integers
using singly linked list to perform the following operations:
a.Push
b.Pop
c.Display
It must print appropriate messages for Stack overflow and empty.

#include<stdio.h>
#include<conio.h>
struct node{
int info;
struct node* next;
};
int displayMenu();
void push(struct node**,int);
int pop(struct node**);
int isEmpty(struct node**);
void display(struct node**);
void main()
{
struct node *top;
int element,choice;
top=NULL;
do
{
choice=displayMenu();
switch (choice)
{
case 1:
printf("\n Enter the element to be pushed : ");
scanf("%d",&element);
push(&top,element);
break;
case 2:
if(isEmpty(&top))

DEPARTMENT OF CSE Data Structure Lab Manual


34
BMIT

printf("\n Stack is empty");


else
printf("\n Element removed = %d",pop(&top));
break;
case 3:
if(isEmpty(&top))
printf("\n Stack is empty");
else
printf("\n Stack is not empty");
break;
case 4:
display(&top);
break;
case 5:
printf("\n Exiting the program, thank you for using it");
break;
default:
printf("\n Please enter your choice correctly");
break;
}
}while(choice!=5);
getch();
}
int displayMenu()
{
int ch;
printf("\n Press any key to continue...........");
getch();
clrscr();
printf("\n Stack implementation using Singly Linked List");
printf("\n 1-Push an element on the top of the stack");
printf("\n 2-Pop out an element from the top of the stack");
printf("\n 3-Check whether the stack is empty");
printf("\n 4-Display the elements of the stack");
printf("\n 5-Exit");

DEPARTMENT OF CSE Data Structure Lab Manual


35
BMIT

printf("\n Enter your choice(1-5) : ");


scanf("%d",&ch);
return ch;
}
void push(struct node **t,int e)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
if(p==NULL)
printf("\n Stack Overflow");
else
{
p->info=e;
p->next=*t;
*t=p;
}
}
int pop(struct node **t)
{
int poppedElement;
struct node *p;
p=*t;
*t=p->next;
poppedElement=p->info;
free(p);
return poppedElement;
}
int isEmpty(struct node **t)
{
if(*t==NULL)
return 1;
else
return 0;
}
void display(struct node **t)

DEPARTMENT OF CSE Data Structure Lab Manual


36
BMIT

{
int i;
struct node *p;
if(isEmpty(t))
printf("\n The stack is empty");
else
{
p=*t;
printf("\n");
while(p!=NULL)
{
printf("\t %d",p->info);
p=p->next;
}
}
}

DEPARTMENT OF CSE Data Structure Lab Manual


37
BMIT

Program 11.

Write a C program using dynamic variables and pointers to construct a queue of integers
using a singly linked list to perform the following operations:
a. Insert
b. Delete
c. Display
It must print appropriate messages for queue full and empty.

#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *next;
};
void add(struct node **f,struct node **r,int a);
int rem(struct node **f,struct node **r);
int empty(struct node **f);
void display(struct node **f);
void main()
{
struct node *f=NULL,*r=NULL;
int choice,a,i,yes=1;
clrscr();
do
{
printf("\n1:Add\n2:Remove\n3:Display");
printf("\n Enter choice");
scanf("%d",&choice);
switch(choice)
{
case 1:

DEPARTMENT OF CSE Data Structure Lab Manual


38
BMIT

printf("Enter the element to be added ");


scanf("%d",&a);
add(&f,&r,a);
break;
case 2:
if(empty(&f))
printf("Queue Empty");
else
{
i=rem(&f,&r);
printf("%d",i);
}
break;
case 3:
display(&f);
break;
default:
printf("Wrong Choice ");
exit(0);
}
}while(yes);
getch();
}

void add(struct node **f,struct node **r,int a)


{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->info=a;
p->next=NULL;
if(*f==NULL)
*f=p;
else
(*r)->next=p;
*r=p;

DEPARTMENT OF CSE Data Structure Lab Manual


39
BMIT

int rem(struct node **f,struct node **r)


{
int i;
struct node *p;
p=*f;
i=p->info;
(*f)=(*f)->next;
if(*f==NULL)
*r=NULL;
free(p);
return i;
}

int empty(struct node **f)


{
if(*f==NULL)
return 1;
else
return 0;
}

void display(struct node **f)


{
struct node *p;
if(*f==NULL)
printf("\nQueue Empty");
else
{
p=*f;
while(p!=NULL)
{
printf(" %d ",p->info);
p=p->next;

DEPARTMENT OF CSE Data Structure Lab Manual


40
BMIT

}
}
}

Program 12:

Write a C program to support the following operations on a doubly linked list where each
node consists of integers.
a] Create a doubly linked list by adding each node at the front.
b] Insert new nodes to the left of the node whose key value is read as an input
c] Delete the node of the given data, if it is found, otherwise display appropriate
message.
d] Display the contents of the file.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
int id;
char name[20];
int sem;
struct node *succ;
struct node *pred;
};

void insertfront(struct node**,struct node**);


void insertback(struct node**,struct node**);
void insertpos(struct node**,struct node**);
void rem(struct node**,struct node**);
void search(struct node*,int);
void display(struct node*);

DEPARTMENT OF CSE Data Structure Lab Manual


41
BMIT

void main()
{
int choice,id;
struct node *f=NULL;
struct node *r=NULL;
clrscr();
do
{
printf("\n1.Insert front\n2.Insert back\n3.Insert at specified position\n4.delete
id\n5.Search id\n6.Display\n7.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: insertfront(&f,&r);
break;
case 2: insertback(&f,&r);
break;
case 3: insertpos(&f,&r);
break;
case 4: rem(&f,&r);
break;
case 5: printf("\nEnter id to search...\n");
scanf("%d",&id);
search(f,id);
break;
case 6: display(f);
break;
case 7: break;
}
}while(choice!=7);
getch();
}

void insertfront(struct node**f,struct node **r)

DEPARTMENT OF CSE Data Structure Lab Manual


42
BMIT

{
struct node*p;
p=(struct node*)malloc(sizeof(struct node));
printf("\nEnter name...\n");
scanf("%s",p->name);
printf("Enter ID(integer)...\n");
scanf("%d",&p->id);
printf("Enter Semester...\n");
scanf("%d",&p->sem);
if(*f==NULL)
{
(*f)=(*r)=p;
p->succ = NULL;
p->pred = NULL;
}
else
{
p->succ=(*f);
p->pred=NULL;
(*f)=p;
}
}

void insertback(struct node**f,struct node **r)


{
struct node*p;
p=(struct node*)malloc(sizeof(struct node));
printf("\nEnter name...\n");
scanf("%s",p->name);
printf("Enter ID(integer)...\n");
scanf("%d",&p->id);
printf("Enter Semester...\n");
scanf("%d",&p->sem);
if((*f)==NULL)
{

DEPARTMENT OF CSE Data Structure Lab Manual


43
BMIT

(*f)=(*r)=p;
p->succ=NULL;
p->pred=NULL;
}
else
{
p->succ=NULL;
p->pred=(*r);
(*r)->succ=p;
(*r)=p;
}
}

void insertpos(struct node**f,struct node**r)


{
int pos,count=0;
struct node*p,*q,*s;
q=(*f);
p=(struct node*)malloc(sizeof(struct node));
printf("\nEnter postion to enter...\n");
scanf("%d",&pos);
printf("\nEnter name...\n");
scanf("%s",p->name);
printf("Enter ID(integer)...\n");
scanf("%d",&p->id);
printf("Enter Semester...\n");
scanf("%d",&p->sem);
while((q->succ!=NULL)&&(count!=(pos-1)))
{
count++;
s=q;
q=q->succ;
}
if(count<(pos-1))
{printf("\nInvalid postion\n");}

DEPARTMENT OF CSE Data Structure Lab Manual


44
BMIT

else
{
p->succ=s->succ;
p->pred=s;
s->succ->pred=p;
s->succ=p;
if(p->succ==NULL)
{(*r)=p;}
}
}

void rem(struct node **f,struct node **r)


{
int i;
struct node *p,*q;
printf("\nEnter ID(integer) to delete...\n");
scanf("%d",&i);
p=*f;
while((p->succ!=NULL)&&(p->id!=i))
{
q=p;
p=p->succ;
}
if(p->id==i)
{
if((*f)==p && (*f)->succ==NULL)
{printf("%s",p->name);}
else if ((*r)==p)
{
printf("%s",p->name);
q->succ=NULL;
(*r)=q;
}
else {
printf("%s",p->name);

DEPARTMENT OF CSE Data Structure Lab Manual


45
BMIT

q->succ=p->succ;
p->succ->pred=q;
}
free(p);
}
else
{printf("\nNODE NOT FOUND...\n");}
}

void search(struct node *f,int i)


{
printf("\nEnter ID to search...\n");
while((f->succ!=NULL)&&(f->id!=i))
{
f=f->succ;
}
if (f->id==i)
{
printf("\nRecord FOUND...\n");
printf("\nNAME-%s\nSEMESTER-%d",f->name,f->sem);
}
else
{
printf("\nSorry SEARCH FAILED...\n");
}
}

void display(struct node *f)


{
do
{
printf("\n%s\t%d\t%d",f->name,f->id,f->sem);
f=f->succ;
}while(f!=NULL);
}

DEPARTMENT OF CSE Data Structure Lab Manual


46
BMIT

Program 13:
Write a C program:
a] To construct a binary search tree of integers.
b] To traverse the tree using all methods i.e., inorder , preorder and postorder.
c] To display the elements in the tree.

#include<stdio.h>
#include<conio.h>

struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;

NODE getnode();
NODE insert(int,NODE);
void display(NODE,int);
void freenode(NODE);
void inorder(NODE);
void preorder(NODE);
void postorder(NODE);

DEPARTMENT OF CSE Data Structure Lab Manual


47
BMIT

NODE insert(int item,NODE root)


{
NODE temp,cur,prev;
temp=getnode();
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;

if(root==NULL)return temp;

prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(item==cur->info)
{
printf("Duplicate item\n");
freenode(temp);
return root;
}
cur=(item<cur->info)?cur->llink:cur->rlink;
}
if(item<prev->info)
prev->llink=temp;
else
prev->rlink=temp;

return root;
}

NODE getnode()
{

DEPARTMENT OF CSE Data Structure Lab Manual


48
BMIT

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

void freenode(NODE x)
{
free(x);
}

void display(NODE root,int i)


{
int j;
if(root!=NULL)
{
display(root->rlink,i+1);
for(j=1;j<=i;j++)
printf(" ");
printf(" %d \n",root->info);
display(root->llink,i+1);
}
}

void inorder(NODE root)


{
if(root!=NULL)
{
inorder(root->llink);
printf("%d ",root->info);
inorder(root->rlink);

DEPARTMENT OF CSE Data Structure Lab Manual


49
BMIT

}
void preorder(NODE root)
{
if(root!=NULL)
{
printf("%d ",root->info);
preorder(root->llink);
preorder(root->rlink);
}

}
void postorder(NODE root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d ",root->info);

}
}

void main()
{
NODE root,temp;
int item,choice;
root=NULL;
clrscr();
for(;;)
{
printf("1-insert 2-display 3-traversels 4-exit\n");
printf("enter choice\n");

DEPARTMENT OF CSE Data Structure Lab Manual


50
BMIT

scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the element\n");
scanf("%d",&item);
root=insert(item,root);
break;
case 2: if(root==NULL)
printf("empty tree\n");
else
{
printf("the tree in tree form\n");
display(root,1);
printf("\n");
}
break;
case 3: if(root==NULL)
printf("empty tree\n");
else
{
printf("inorder traversal:");
inorder(root);
printf("\npreorder traversal:");
preorder(root);
printf("\npostorder traversal:");
postorder(root);
printf("\n");
}
break;
case 4: exit(0);
default: exit(0);
}
}
}

DEPARTMENT OF CSE Data Structure Lab Manual


51
BMIT

Program 14:
Write a recursive C program for
a] Searching an element on a given list of integers using binary search method.

#include<stdio.h>
#include<conio.h>
#define MAX 50
int binarySearch(int [],int,int,int);
void main()
{
int a[MAX],i,n,found,element;
clrscr();
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n Now enter the elements one by one in ascending order : ");
for(i=0;i<n;i++)
{
printf("\n a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("\n Enter the element to be searched : ");

DEPARTMENT OF CSE Data Structure Lab Manual


52
BMIT

scanf("%d",&element);
found=binarySearch(a,0,n-1,element);
if(found==-1)
printf("\n The element is not found");
else
printf("\n The element is found at position %d",found);
getch();
}
int binarySearch(int arr[],int low,int high,int key)
{
int mid;
if(low>high)
return -1;
mid=(low+high)/2;
if(arr[mid]==key)
return mid;
else
{
if(key>arr[mid])
binarySearch(arr,mid+1,high,key);
else
binarySearch(arr,low,mid-1,key);
}
}

b] Solving the tower of Hanoi problem.

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void towersOfHanoi(int n,char source,char destination,char temp);
void main()
{
int numOfDiscs;
clrscr();

DEPARTMENT OF CSE Data Structure Lab Manual


53
BMIT

printf("\n Enter the number of discs : ");


scanf("%d",&numOfDiscs);
towersOfHanoi(numOfDiscs,'s','d','t');
getch();
}
void towersOfHanoi(int n,char source,char destination,char temp)

{static int i;
if(n>0)
{
towersOfHanoi(n-1,source,temp,destination);
delay(1000);
printf("\n Move disc %d from %c to %c %d",n,source,destination,i++);
towersOfHanoi(n-1,temp,destination,source);
}
}

DEPARTMENT OF CSE Data Structure Lab Manual


54

You might also like