Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 44

DATA STRUCTURES-[1010206303]

PRACTICAL:-1
AIM:-Introduction to pointers : Call by value and Call by reference.
#include<stdio.h>
void swap(int *x, int *y){
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main(){
int a=34,b=74;
printf("the value is now:%d and %d \n",a,b);
swap(&a,&b);
printf("the value is now:%d and %d \n",a,b);
return 0;
}

OUTPUT:-

[BAIT,SURAT] Page 1
DATA STRUCTURES-[1010206303]

PRACTICAL:-2

AIM:- Introduction to Dynamic Memory Alloction. DMA functions malloc(),


calloc(), free(),etc.
(a)DMA function malloc().
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr=(int *)malloc(3 * sizeof(int));
for (int i = 0; i < 3; i++)
{
printf("Enter the array no %d of this array \n",i);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < 3; i++)
{
printf(" the value at %d of this array %d\n",i,ptr[i]);
}
return 0;
}
Output:-

[BAIT,SURAT] Page 2
DATA STRUCTURES-[1010206303]

(b) DMA function calloc().


#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter Number of Elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}

Output: -

[BAIT,SURAT] Page 3
DATA STRUCTURES-[1010206303]

PRACTICAL:-3
AIM:-“Implement a program for stack that performs following operation
using array. (a) PUSH (b) POP (c) PEEP (d) CHANGE (e) DISPLAY”

#include<stdio.h>
int stack[5],top=-1,size=4,choice;
void main()
{
void push();
void display();
void pop();
void peep();
void update();
printf("\n1:Push Operation");
printf("\n2:Display");
printf("\n3:Pop Operation");
printf("\n4:Peep Operation");
printf("\n5:Update Operation");
printf("\n0:Exit");
do
{
printf("\nEnter The Above Choice:->");
scanf("%d",&choice);
switch(choice)
{
case 0:exit(0);
case 1:push();
break;
case 2:display();
break;
case 3:pop();

[BAIT,SURAT] Page 4
DATA STRUCTURES-[1010206303]

break;
case 4:peep();
break;
case 5:update();
break;
}
}
while(choice != 0);
}
void push()
{
int value;
if(top>=size)
{
printf("\nThe Stack is Overflow...");
return;
}
else
{
printf("Enter The Value:->");
scanf("%d",&value);
top=top+1;
stack[top]=value;
}
}
void display()
{
int i;
if(top == -1)
{
printf("\nThere is value not Avaible...");

[BAIT,SURAT] Page 5
DATA STRUCTURES-[1010206303]

return;
}
else
{
for(i=0;i<=top;i++)
printf("\nThe value is:->%d",stack[i]);
}
}
void pop()
{
if(top==0)
{
printf("\nThe stack is Underflow");
return;
}
else
{
top=top-1;
printf("\nvalue Delete Successfull");
}
}
void peep()
{
int s;
printf("\nEnter Position for search: ");
scanf("%d",&s);
if(top-s<=-1)
{
printf("\nThe Stack is Overflow");
return;
}

[BAIT,SURAT] Page 6
DATA STRUCTURES-[1010206303]

else
{
printf("\nThe value is:%d",stack[top-s]);
}
}

void update()
{
int item1,item2;
printf("\nEnter Position for change: ");
scanf("%d",&item1);
printf("\nEnter Number For Update: ");
scanf("%d",&item2);
if(top-item1<=-1)
{
printf("\nThe Stack Underflow");
return;
}
else
{
Stack [top-item1]=item2;
printf("\update Successful");
}
}

[BAIT,SURAT] Page 7
DATA STRUCTURES-[1010206303]

Output:-

[BAIT,SURAT] Page 8
DATA STRUCTURES-[1010206303]

PRACTICAL: -4

AIM: -Implement a program to convert infix notation to postfix notation


using stack.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
void push(char item)
{
if(top >= SIZE-1)
{
printf("\nStack Overflow.");
}
else
{
top = top+1;
stack[top] = item;
}
}
char pop()
{
char item ;
if(top <0)
{
printf("stack under flow: invalid infix expression");
getchar();
/* underflow may occur for invalid expression */
[BAIT,SURAT] Page 9
DATA STRUCTURES-[1010206303]

/* where ( and ) are not matched */


exit(1);
}
else
{
item = stack[top];
top = top-1;
return(item);
}
}
intis_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(3);
}
elseif(symbol == '*' || symbol == '/')
{
return(2);
}

[BAIT,SURAT] Page 10
DATA STRUCTURES-[1010206303]

elseif(symbol == '+' || symbol == '-')


{
return(1);
}
else
{
return(0);
}
}
voidInfixToPostfix(charinfix_exp[], charpostfix_exp[])
{
int i, j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '\0')
{
if(item == '(')
{
push(item);
}
elseif( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
elseif(is_operator(item) == 1)

[BAIT,SURAT] Page 11
DATA STRUCTURES-[1010206303]

{
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x);
push(item);
}
elseif(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
i++;
item = infix_exp[i];
}
if(top>0)

[BAIT,SURAT] Page 12
DATA STRUCTURES-[1010206303]

{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
postfix_exp[j] = '\0';
}
int main()
{
char infix[SIZE], postfix[SIZE];
printf("ASSUMPTION: The infix expression contains single letter variables and single digit
constants only.\n");
printf("\nEnter Infix expression : ");
gets(infix);
InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);
return 0;
}

Output: -

[BAIT,SURAT] Page 13
DATA STRUCTURES-[1010206303]

PRACTICAL:-5

AIM: - Write a program to implement QUEUE using arrays that perform


following Operation. (a)INSERT (b)DELETE (c)DISPLAY.

#include<stdio.h>
int queue[4],front = -1, rear = -1;
void insertion();
void deletion();
void display();
int main()
{
intch;
printf("1.insert operation\n");
printf("2.delet operation\n");
printf("3.display operation \n");
printf("4.exit");
while (1)
{
printf("\nenter choice: ");
scanf("%d",&ch);
switch (ch)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: display();
break;
default: printf("invalid case");
break;

[BAIT,SURAT] Page 14
DATA STRUCTURES-[1010206303]

}
}
return 0;
}
void insertion()
{
int element;
if (rear == 4 )
{
printf("queue is full");
}
else
{
if (front == -1)
{
front = 0;
}
printf("enter the element: ");
scanf("%d",&element);
rear = rear + 1;
queue[rear] = element;
}
}
void deletion()
{
if (front == -1)
{
printf("queue is empty");
}
else
{

[BAIT,SURAT] Page 15
DATA STRUCTURES-[1010206303]

printf("element is deleted %d",queue[front]);


front = front + 1;
}
}
void display()
{
int x;
if(front == -1)
printf("queue is empty");
else
for ( x = front; x <= rear; x++)
{
printf("%d ",queue[x]);
}
}

Output: -

PRACTICAL:-6
[BAIT,SURAT] Page 16
DATA STRUCTURES-[1010206303]

AIM: - Write a menu driven program to implement following operations on


the singly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Insert a node such that linked list is in asending order.
(d) Delete a First node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.

#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* link;
};
struct node* start = NULL;

void createList()
{
if (start == NULL) {
int n;
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
if (n != 0) {
int data;
struct node* newnode;
struct node* temp;
newnode = malloc(sizeof(struct node));
start = newnode;
temp = start;

[BAIT,SURAT] Page 17
DATA STRUCTURES-[1010206303]

printf("\nEnter number to"


" be inserted : ");
scanf("%d", &data);
start->info = data;

for (int i = 2; i <= n; i++) {


newnode = malloc(sizeof(struct node));
temp->link = newnode;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
}
}
printf("\nThe list is created\n");
}
else
printf("\nThe list is already created\n");
}

void traverse()
{
struct node* temp;

if (start == NULL)
printf("\nList is empty\n");

else {
temp = start;
while (temp != NULL) {

[BAIT,SURAT] Page 18
DATA STRUCTURES-[1010206303]

printf("Data = %d\n", temp->info);


temp = temp->link;
}
}
}

void insertAtFront()
{
int data;
struct node* temp;
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->info = data;
temp->link = start;
start = temp;
}

void insertAtEnd()
{
int data;
struct node *temp, *head;
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL) {

[BAIT,SURAT] Page 19
DATA STRUCTURES-[1010206303]

head = head->link;
}
head->link = temp;
}

void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = malloc(sizeof(struct node));
printf("\nEnter position and data :");
scanf("%d %d", &pos, &data);
temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1) {
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}

void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->link;

[BAIT,SURAT] Page 20
DATA STRUCTURES-[1010206303]

free(temp);
}
}

void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
while (temp->link != 0) {
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}

void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;
if (start == NULL)
printf("\nList is empty\n");
else {
printf("\nEnter index : ");
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;

[BAIT,SURAT] Page 21
DATA STRUCTURES-[1010206303]

while (i < pos - 1) {


temp = temp->link;
i++;
}
position = temp->link;
temp->link = position->link;
free(position);
}
}

int main()
{
int choice;
while (1) {
printf("\n1 To see list\n");
printf("2 For insertion at"
" starting\n");
printf("3 For insertion at"
" end\n");
printf("4 For insertion at "
"any position\n");
printf("5 For deletion of "
"first element\n");
printf("6 For deletion of "
"last element\n");
printf("7 For deletion of "
"element at any position\n");
printf("8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

[BAIT,SURAT] Page 22
DATA STRUCTURES-[1010206303]

switch (choice) {
case 1: traverse();
break;
case 2: insertAtFront();
break;
case 3: insertAtEnd();
break;
case 4: insertAtPosition();
break;
case 5: deleteFirst();
break;
case 6: deleteEnd();
break;
case 7: deletePosition();
break;
case 8: exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}1
return 0;
}

Output: -

[BAIT,SURAT] Page 23
DATA STRUCTURES-[1010206303]

2.

3.

4.

[BAIT,SURAT] Page 24
DATA STRUCTURES-[1010206303]

5.

6.

7.

PRACTICAL:-7

AIM:-Write a program to implement stack using linked list.

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

[BAIT,SURAT] Page 25
DATA STRUCTURES-[1010206303]

void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:

[BAIT,SURAT] Page 26
DATA STRUCTURES-[1010206303]

{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;

[BAIT,SURAT] Page 27
DATA STRUCTURES-[1010206303]

ptr -> next = NULL;


head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

}
printf("Item pushed");

}
}

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

[BAIT,SURAT] Page 28
DATA STRUCTURES-[1010206303]

}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

Output:-

[BAIT,SURAT] Page 29
DATA STRUCTURES-[1010206303]

[BAIT,SURAT] Page 30
DATA STRUCTURES-[1010206303]

PRACTICAL:-8
AIM:-Write a program to implement queue using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:

[BAIT,SURAT] Page 31
DATA STRUCTURES-[1010206303]

display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("Enter value: ");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;

[BAIT,SURAT] Page 32
DATA STRUCTURES-[1010206303]

rear -> next = NULL;


}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
printf("item deleted....\n");
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)

[BAIT,SURAT] Page 33
DATA STRUCTURES-[1010206303]

{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
Output:-

[BAIT,SURAT] Page 34
DATA STRUCTURES-[1010206303]

PRACTICAL: -9
AIM: -Write a program to implement Quick Sort.

#include<stdio.h>
#include <stdlib.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \t2.Deletion \t3.Display \t4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf(" Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}

[BAIT,SURAT] Page 35
DATA STRUCTURES-[1010206303]

else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\t");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}

[BAIT,SURAT] Page 36
DATA STRUCTURES-[1010206303]

Output:-

[BAIT,SURAT] Page 37
DATA STRUCTURES-[1010206303]

PRACTICAL:-10
AIM: -Write a program to implement Merge Sort.
#include <stdio.h>
voidmergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50];
int i, size;
printf("Enter Total Number of Elements:");
scanf("%d", &size);
printf("Enter The Elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
partition(list, 0, size - 1);
printf("After Merge Sort:\n");
for(i = 0;i < size; i++)
{
printf("%d ",list[i]);
}
return 0;
}
void partition(int list[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);

[BAIT,SURAT] Page 38
DATA STRUCTURES-[1010206303]

partition(list, mid + 1, high);


mergeSort(list, low, mid, high);
}
}
voidmergeSort(int list[],int low,intmid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for (k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}

[BAIT,SURAT] Page 39
DATA STRUCTURES-[1010206303]

}
else
{
for (k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
list[k] = temp[k];
}
}

Output: -

[BAIT,SURAT] Page 40
DATA STRUCTURES-[1010206303]

PRACTICAL:-11
AIM:-Write the program to implement bubble sort.
#include <stdio.h>
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
voidbubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] >arr[j + 1])
swap(&arr[j], &arr[j + 1]);
}
voidprintArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
intarr[] = { 23, 45, 56, 66, 78, 90, 67};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);

[BAIT,SURAT] Page 41
DATA STRUCTURES-[1010206303]

return 0;
}
Output:-

[BAIT,SURAT] Page 42
DATA STRUCTURES-[1010206303]

PRACTICAL:-12

AIM:-Write a program to implement binary search.

#include<stdio.h>
int main()
{
int a[5]={10,20,30,40,50},lower=0,upper=4,found=0,mid,item;
printf("enter searching items: ");
scanf("%d",&item);
while(lower<=upper)
{
mid=(lower+upper)/2;
if (a[mid]==item)
{
found=1;
break;
}
if(a[mid]<item)
lower=mid+1;
else
upper=mid-1;

}
if(found==1)
printf("item found with location a[%d]",mid);
else
printf("not found");
return 0;
}

[BAIT,SURAT] Page 43
DATA STRUCTURES-[1010206303]

Output:-

[BAIT,SURAT] Page 44

You might also like