Data_Structure_lab_report
Data_Structure_lab_report
no
Design, Develop and Implement a menu driven Program in C for
the following array operations.
A) Creating an array of N Integer Elements
B) Display of array Elements with Suitable Headings
1 C) Inserting an Element (ELEM) at a given valid Position 1-2
(POS) D) Deleting an Element at a given valid Position (POS)
E) Exit.
Support the program with functions for each of the above
operations
Design, develop and Implement a Program in C for the following
operations on Strings.
A) Read a main String (STR), a Pattern String (PAT) and a
Replace String (REP)
2 B) Perform Pattern Matching Operation: Find and Replace all 3-4
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. Don't use Built-in functions.
#include<stdio.h>
#include<stdlib.h>
int a[20],n,val,i,pos;
void create();
void display();
void insert();
void delete();
int main()
{
int choice=1;
while(choice)
{
printf("\n\n---------------MENU ---------------\n");
printf("1.CREATE\N");
printf("2.DISPLAY\N");
printf("3.INSERT\N");
printf("4.DELETE\N");
printf("5.EXIT\N");
printf("-----------------------------------------");
printf("\nENTER YOUR CHOICE:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: delete();
break;
case 5: exit(0);
default: printf("\n Invalid choice:\n");
break;
}
}
return 0;
}
void create()
{
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display()
{
int i;
printf("\nThe array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void insert()
{
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted:\t");
scanf("%d",&val);
for(i=n-1;i>=pos;i--)
{
a[i+1]-a[i];
}
a[pos]=val;
n=n+1;
}
void delete()
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n+1;
printf("\nThe deleted element is=%d",val);
}
2. Design, develop and Implement a Program in C for the following
operations on Strings.
A) Read a main String (STR), a Pattern String (PAT) and a 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. Don't use Built-in functions.
#include<stdio.h>
char str[50], pat[20], rep[20], ans[50];
int c=0, m=0, i=0, j=0, k, flag=0;
void stringmatch()
{
while(str[c] !='\0')
{
if(str[m] == pat[i])
{
i++;
m++;
if(pat[i] == '\0')
{
flag = 1;
for(k=0; rep[k]!='\0'; k++, j++)
{
ans[j] = rep[k];
}
i = 0;
c = m;
}
}
else
{
ans[j]= str[c];
j++;
c++;
m=c;
i=0;
}
}
}
void main()
{
printf("\nEnter the main string:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
stringmatch();
if(flag == 1)
printf("\nResultant string is %s", ans);
else
printf("\nPattern string is not found");
}
OUTPUT:
3. Design, Develop and Implement 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 5
int s[MAX];
int top = -1;
void main()
{
int choice, item;
while(1)
{
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");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\nEnter an element to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2: item = pop();
if(item != -1)
printf("\nElement popped is: %d", item);
break;
case 3: palindrome();
break;
case 4: display();
break;
case 5: exit(1);
default: printf("\nPlease enter valid choice ") ;
break;
}
}
}
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("\nStack elements are:\n ");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
}
void palindrome()
{
int flag=1,i;
printf("\nStack content are:\n");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
#include<stdio.h>
#include<stdlib.h>
void evaluate();
void push(char);
char pop();
int prec(char);
char infix[30], postfix[30], stack[30];
int top = -1;
void main()
{
printf("\nEnter the valid infix expression:\t");
scanf("%s", infix);
evaluate();
printf("\nThe entered infix expression is :\n %s \n", infix);
printf("\nThe corresponding 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;
char pop()
{
char item;
item = stack[top];
top = top-1;
return item;
}
case '(' :
case ')' : p = 0;
break;
case '+' :
case '-' : p = 1;
break;
case '*' :
case '/' :
case '%' : p = 2;
break;
case '^' :
case '$' : p = 3;
break;
}
return p;
}
OUTPUT:
5. Design, Develop and Implement a Program in C for the following Stack
Applications. Evaluation of suffix expression with single digit operand and
operators: +, -, *, /, %,^
#include<stdio.h>
#include<math.h>
#include<string.h>
switch (symbol)
case '$':
default : return 0;
void main()
top=-1;
symbol = postfix[i];
if(isdigit(symbol))
s[++top]=symbol - '0';
else
op2 = s[top--];
op1 = s[top--];
s[++top] = res;
}
OUTPUT:
6. Solving Tower of Hanoi problem with n disks
#include<stdio.h>
#include<conio.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');
getch();
}
OUTPUT:
7. Program to implement factorial of a number and to generate the
Ackerman function using recursive
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int fact(int n)
if(n==0)
return 1;
return n*fact(n-1);
if(p==0)
return q+1;
else if(q==0)
return A(p-1,1);
else
return A(p-1,A(p,q-1));
void main()
{
int n,p,q,ch;
while(1)
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
break;
scanf("%d%d",&p,&q);
break;
case 3:exit(0);
default:printf("Invalid choice");
return;
}
OUTPUT:
8. Design, Develop and Implement 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<conio.h>
#define MAX 10
int ch, front = 0, rear = -1, count=0;
char q[MAX], item;
void insert()
{
if(count == MAX)
{
printf("\nQueue is Full");
}
else
{
rear = (rear + 1)% MAX; q[rear]=item;
count++;
}
}
void del()
{
if(count == 0)
printf("\nQueue is Empty");
else if(front > rear && rear==MAX-1)
{
front=0; rear=-1; count=0;
}
else
{
item=q[front];
printf("\nDeleted item is: %c",item);
front = (front + 1)% MAX;
count--;
}
}
void display()
{
int i, f=front, r=rear;
if(count == 0)
printf("\nQueue is Empty");
else
{
printf("\nContents of Queue is:\n");
for(i=f; i!=r; i=(i+1)% MAX)
{
printf("%c\t", q[i]);
}
printf("%c\t", q[i]);
}
}
void main()
{
do
{
printf("\n\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter the character / item to be inserted: ");
scanf("%s",&item);
insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
break;
}
}while(ch!=4);
getch();
}
OUTPUT:
9. Program to implement singly Linked list using Queue
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node* getnode()
{
struct node* x = (struct node*)malloc(sizeof(struct node));
if(x == NULL)
{
printf("Out of memory\n");
exit(0);
}
return x;
}
void freenode(struct node *x)
{
free(x);
}
struct node* insert_rear(int item, struct node *first)
{
struct node *temp, *cur; temp = getnode();
temp->info = item; temp->link = NULL;
if(first == NULL)
{
return temp;
}
cur = first;
while(cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
return first;
}
struct node* delete_front(struct node *first)
{
struct node *temp;
if(first == NULL)
{
printf("List is empty, cannot delete\n");
return first;
}
temp = first;
first = first->link;
printf("Item deleted = %d\n", temp->info);
freenode(temp);
return first;
}
void display(struct node *first)
{
struct node *temp;
if(first == NULL)
{
printf("List is empty\n");
return;
}
printf("The contents of singly linked list:\n");
temp = first;
while(temp != NULL)
{
printf("%d ", temp->info);
temp = temp->link;
}
printf("\n");
}
int main()
{
struct node *first = NULL;
int ch, item;
while(1)
{
printf("\n1. Insert Rear\n2. Delete Front\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:printf("Enter the element to be inserted: ");
scanf("%d", &item);
first = insert_rear(item, first);
break;
case 2: first = delete_front(first);
break;
case 3: display(first);
break;
case 4: exit(0);
default: printf("Invalid choice, please try again.\n");
}
}
return 0;
}
OUTPUT:
9. Program to implement singly Linked list using Queue
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
struct node
{
int info;
struct node*link;
};
typedef struct node*NODE;
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);
}
NODE insert_rear(int item,NODE first)
{
NODE temp,cur;
temp=getnode();
temp->info=item;
temp->link=NULL;
if(first==NULL)
return temp;
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
return first;
}
NODE delete_front(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("List is empty can not delete\n");
return first;
}
temp=first;
temp=temp->link;
printf("Item deleted =%d\n",first->info);
freenode(first);
return temp;
}
void display(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("List is empty\n");
return;
}
printf("The content of Singly Linked List\n");
temp=first;
while(temp!=NULL)
{
printf("%d",temp->info);
temp=temp->link;
}
printf("\n");
}
void main()
{
NODE first=NULL;
int ch,item;
for(;;)
{
printf("1.INSERT REAR\n 2.DELETE FRONT\n 3.DISPLAY \n
4.EXIT\n");
printf("ENTER THE CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the element to be inserted:");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
case 4:exit(0);
default:printf("Invalid Choice");
return;
}
}
}
OUTPUT:
10. Program to implement Binary tree traversal
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int val)
{
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->left = newNode->right = NULL;
return newNode;
}
void inorder(struct Node* root)
{
if (root)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder(struct Node* root)
{
if (root)
{
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct Node* root)
{
if (root)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
struct Node* insertNode(struct Node* node, int val)
{
if (!node)
return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}
int main()
{
struct Node* root = NULL;
int choice, item;
while (1)
{
printf("1. Insert 2. Inorder 3. Preorder 4. Postorder 5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: printf("Enter the element to be inserted: ");
scanf("%d", &item);
root = insertNode(root, item);
break;
case 2: printf("Inorder traversal: ");
inorder(root);
printf("\n");
break;
case 3: printf("Preorder traversal: ");
preorder(root);
printf("\n");
break;
case 4: printf("Postorder traversal: ");
postorder(root);
printf("\n");
break;
case 5: exit(0);
default: printf("Invalid choice\n");
}
}
return 0;
}
OUTPUT:
11. Program to implement Binary Search
#include<stdio.h>
#include<conio.h>
int search( int item, int a[ ], int n)
{
int low, high,key,mid;
low = 0; //Initialization
high = n-1; // Initialization
key=item;
while( low <= high )
{
mid = ( low + high ) / 2; // Find the mid-point
if ( key == a[mid] )
{
// If item not found, return position
return mid;
}
if( key < a[mid] )
high = mid - 1; // Search left side
else
low = mid + 1; // Search right side
}
return -1; // Item not found
}
void main( )
{
int i,item,a[10],n,pos;
printf("Enter the size of an Array\n");
scanf("%d",&n);
printf("Enter the Array Elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("The Array Elements are\n");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
printf("Enter the Element to be searched\n");
scanf("%d",&item);
pos=search(item,a,n);
if(pos==-1)
printf("Item not found\n");
else
printf("Item found\n");
getch( );
}