Professional Documents
Culture Documents
Data Structures With C: Third Semester Cs
Data Structures With C: Third Semester Cs
Data Structures With C: Third Semester Cs
LABORATORY MANUAL
THIRD SEMESTER CS
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;
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");
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:
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 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
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);
}
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;
{
ptr->top++;
ptr->item[ptr->top]=ele;
}
}
main()
{
clrscr();
s.top=-1;
while(1)
{
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();
}
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;
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 '/':
main()
{
clrscr();
top=-1;
printf("Enter a valid infix expression: ");
scanf("%s",str);
convert();
getch();
}
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;
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
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();
}
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 *);
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))
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]);
}
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;
};
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;
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");
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
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;
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
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 main()
{
int choice,id;
struct node *f=NULL;
struct node *r=NULL;
clrscr();
do
{
if(*f==NULL)
{
(*f)=(*r)=p;
p->next = NULL;
}
else
{
p->next=(*f);
(*f)=p;
}
}
if (f->id==i)
{
printf("\nRecord FOUND...\n");
printf("\nNAME-%s\nSEMESTER-%d",f->name,f->sem);
}
else
{
printf("\nSorry SEARCH FAILED...\n");
}
}
Program 10.
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))
{
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;
}
}
}
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:
}
}
}
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 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();
}
{
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;
}
}
(*f)=(*r)=p;
p->succ=NULL;
p->pred=NULL;
}
else
{
p->succ=NULL;
p->pred=(*r);
(*r)->succ=p;
(*r)=p;
}
}
else
{
p->succ=s->succ;
p->pred=s;
s->succ->pred=p;
s->succ=p;
if(p->succ==NULL)
{(*r)=p;}
}
}
q->succ=p->succ;
p->succ->pred=q;
}
free(p);
}
else
{printf("\nNODE NOT FOUND...\n");}
}
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);
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()
{
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 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");
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);
}
}
}
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 : ");
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);
}
}
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void towersOfHanoi(int n,char source,char destination,char temp);
void main()
{
int numOfDiscs;
clrscr();
{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);
}
}