0% found this document useful (0 votes)
16 views38 pages

DSU 1-10

Data structure codes

Uploaded by

shanksgurav124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views38 pages

DSU 1-10

Data structure codes

Uploaded by

shanksgurav124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

1.

Write a ‘C’ program to perform following Operations on


Array: Create, Insert, Delete, Display.

Algorithm
Step 1: Start
Step 2: Declare Variables a[10], n,max=10,choice
Step 3: Enter the number of terms to be entered.
Step 4: Enter The elements in the array.
Step 5: Choose any one.
· Insert an element.(go to step 6)
· Delete an element(go to step 7)
· Display array(go to step 8)
· Exit(go to step 9)
Step 6: If we choose insert an element then
1. Enter the element to be added
2. Enter the position where the data should be added
3. The value entered will traverse the array and be inserted in the position
given.
4. If the position Is correct then it will be inserted.
5. Else Invalid position
6. Else Array is full
7. Break
Step 7: If we choose Delete an element
1. Enter the element to be deleted
2. If the element is found then it will be deleted.
3. Else the element is not found.
4. Break
Step 8: If we choose Display array.
1. For loop is used to print the entire array.
2. Break
Step 9: Exit The code
Step 10: End

Flowchart:
Code:
#include <stdio.h>
#include<conio.h>
void main() {
int a[10], n, max = 10, choice, i;
printf("Enter number of elements: ");
scanf("%d", &n);
if (n <= max) {
printf("Enter the elements:\n");
for ( i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &a[i]);
}

do {
printf("\nArray operations:\n1. Insert an element\n2. Delete an element\
n3. Display array\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1: {
int x, pos;
if (n < max) {
printf("\nEnter the element to be added: ");
scanf("%d", &x);
printf("Enter the position where the element is to be added: ");
scanf("%d", &pos);

if (pos > 0 && pos <= n + 1) {


for ( i = n; i >= pos; i--) {
a[i] = a[i - 1];
} a[pos - 1] = x;
n++;
} else {
printf("Invalid position!\n");
}
} else {
printf("Array is full. Cannot insert element.\n");
}
break;
}
case 2: {
int x, pos = -1, c = 0;
printf("\nEnter the element to be deleted: ");
scanf("%d", &x);

for ( i = 0; i < n; i++) {


if (a[i] == x) {
pos = i;
c = 1;
break;
}
}

if (c == 1) {
for ( i = pos; i < n - 1; i++) {
a[i] = a[i + 1];
}
n--;
} else {
printf("Element not found.\n");
}
break;
}
case 3: {
printf("Current array:\n");
for ( i = 0; i < n; i++) {
printf("Element no %d is %d\n", i + 1, a[i]);
}
break;
}
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
} else {
printf("Memory not available....\n");
}
getch();
}

2. //program of linear search


Algorithm
Step 1: Start
Step2: declare the variables x,arr,i,j,k,c
Step3: Enter the number of elements in x.
Step 4:Use for loop
Step5 Enter The elements in the array.
Step6:Enter the element to be searched in the
array#
Step 7: Use for loop
Step 8: If arr[j] is equal to k
Step 9: Element is found in the position j and c is
equal to 1.
Step 10: if j is equal to x-1 and c is equal to 0
Step 11:Element is not present in array.
Step 12: End for loop
Step 13: End

Flowchart:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,arr[100],i,j,k,c;
c=0;
clrscr ();
printf("enter number of array element(less than 100)");
scanf("%d",&x);
for (i=0; i<x; i++)
{
printf("enter element no %d", i+1);
scanf("%d",&arr[i]);
}
printf("enter element to be searched:");
scanf ("%d",&k);
for(j=0;j<x;j++)
{
if(arr[j]==k)
{
printf("\nelement %d found at position %d",k,j+1);
c++;
}
if(j==(x-1)&&c==0)
{
printf("\n element not present in array",k);
}
}
getch();
}

3. /*--- Binary Search---*/


1 Start
2 Initialize Variables:
 x: Number of array elements.
 arr[100]: Array to hold up to 100 integers.
 k: Element to be searched.
 i, j: Loop control variables.
 high, low, mid: Variables for binary search.
 found = 0: Flag to check if the element is found.
3 Input the Number of Elements:
 Prompt the user to enter the number of array elements (less than 100).
 Read the value into x.
4 Input Array Elements:
 For each i from 0 to x-1:
o Prompt the user to enter element number i+1.
o Read the element into arr[i].
5 Sort the Array Using Bubble Sort:
 For each i from 0 to x-1:
o For each j from 0 to x-2:
 If arr[j] > arr[j+1]:
 Swap arr[j] and arr[j+1].
6 Display Sorted Array:
 For each i from 0 to x-1:
o Print arr[i].
7 Input the Element to be Searched:
 Prompt the user to enter the element to be searched.
 Read the value into k.
8 Initialize Binary Search Variables:
 Set low = 0.
 Set high = x-1.
9 Perform Binary Search:
 While low is less than or equal to high:
o Calculate mid = (low + high) / 2.
o If arr[mid] == k:
 Set found = 1.
 Print that element k is found at position mid+1.
 Break the loop.
o Else if arr[mid] > k:
 Set high = mid - 1.
o Else:
 Set low = mid + 1.
10 Check If Element Was Not Found:
 If found is still 0, print "Not found".
11 End
#include<stdio.h>
#include<conio.h>
void main()
{
int x,k,arr[100],i,j, high,low,mid,found=0;
clrscr();
printf("\nEnter number of array element (less than 100):");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("Enter element number%d\t",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
if(arr[j]>arr[j+1])
{
int t;
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
for(i=0;i<x;i++)
{
printf("\nElement number%d\n",arr[i]);
}

printf("\n Enter element to be searched");


scanf("%d",&k);
low=0 ;
high=x-1;
while(low<=high)
{
mid=(low+high)/2;
if(arr[mid]==k)
{
printf("\nElement%d found at position %d",k,mid+1);
found=1;
break;
}
else
{
if(arr[mid]>k)
high=mid-1;
else
low=mid+1;
}
}
if(found!=1)
{
printf("Not found");
}
getch();
}

4. Program of Bubble Sort numbers


I Algorithm
Step 1 : Start.
Step 2 : Declare variables a[] , n , i , j .
Step 3 : Declare the number of array elements.
Step 4 : Use for loop for i and declare the values of array elements.
Step 5 : Use for loop for i and show the array elements before sorting.
Step 6 : Use for loop for i and j and declare the if statement to check if a[j] is greater than
a[j+1].
Step 7 : If a[j] is greater than a[j+1] then declare variable t. Where t= a[j] , a[j]=a[j+1] and
a[j+1]=t. Where a[j] value will be assigned to t then a[j+1] value will be assigned to a[j] and t
value will be assigned to a[j+1].
Step 8 : print the sorted array by using for loop .
Step 9 : Display output and stop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j;
clrscr();
printf("Enter number of array element");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter array element %d\t ",i+1);
scanf("%d",&a[i]);
}
printf("\n Before sorting array");
for(i=0;i<n;i++)
{
printf("\nElement No %d is %d",i+1,a[i]);
}
for(i=0;i<n;i++)//passes
{
for(j=0;j<n-1-i;j++)//comparisons
{
if(a[j]>a[j+1])
{
int t;
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}}}
printf("\nAfter sorting array");
for(i=0;i<n;i++)
{
printf("\nElement No %d is %d",i+1,a[i]);
}
getch(); }

5. //Program Of Selection Sort


Algorithm
Step 1: Start.
Step 2: Declare variables a [], n, i , j, temp.
Step 3 : Declare the number of array elements.
Step 4: Use for loop for i and declare the values of array elements.
Step 5: Use for loop for i and show the array elements before sorting.
Step 6: Use for loop for i and j and declare the if statement to check if a[i] is
greater than a[j].
Step 7: If a[i] is greater than a[j] then temp= a[j], a[i]=a[j] and a[j]=temp.
Where a[j] value will be assigned to temp then a[j] value will be assigned to a[i]
and temp value will be assigned to a[j].
Step 8: print the sorted array by using for loop.
Step 9: Display output and stop.

I Flowchart
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("\nEnter number of array element");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter arry element %d",i+1);
scanf("%d",&a[i]);
}
printf("\nBefore sorting array");
for(i=0;i<n;i++)
{
printf("\nElement No %d is %d",i+1,a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

printf("\nAfter sorting array");


for(i=0;i<n;i++)
{
printf("\nElement No %d is %d",i+1,a[i]);
}
getch();
}

6. /*-------Insertion Sort --------*/


Algorithm
1. Start.

2. Input the Number of Elements:


 Print "Enter the number of elements: ".
 Read n.
3. Input the Elements:
 For i = 0 to n-1:
o Print "Element no i+1 is: ".
o Read arr[i].
4. Insertion Sort:
 For i = 1 to n-1:
o Set key = arr[i].
o Set j = i-1.
o While j >= 0 and arr[j] > key:
 Set arr[j + 1] = arr[j].
 Decrement j.
o Set arr[j + 1] = key.
5. Print the Sorted Elements:
 Print "\nAfter sorting:".
 For i = 0 to n-1:
o Print "Element no i+1 is: arr[i]".
6. Input the Number to Search:
 Print "\nEnter the number to search: ".
 Read key.
7. Linear Search:
 Set found = 0.
 For i = 0 to n-1:
o If arr[i] == key:
 Print "\nNumber found at position i+1".
 Set found = 1.
 Break loop.
8. Check if Number was Found:
 If found == 0:
o Print "\nNumber not found".
9. End.

Flow Chart
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,j,k,temp;
clrscr();
printf("\n Enter number of element");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element %d",i+1);
scanf("%d",&a[i]);
}
printf("\n array before sorting");
for(i=0;i<n;i++)
{
printf("\nElement no %d is %d",i+1,a[i]);
}
for(k=1;k<n;k++)
{
temp=a[k];
j=k-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\n array after sorting");
for(i=0;i<n;i++)
{
printf("\nElement no %d is %d",i+1,a[i]);
}
getch();
}

7. // Linked list
Algorithm
Step 1: Start
Step 2: Pick from the options
· Input Number
· Display linked list
· Search a number
· Exit
Step 3: Input number
1. Enter a number
2. The number will be inserted to the linked list and points to
NULL
Step 4: Display the linked list
Step 5: Search an element

1. Check if element is present in the linked list


2. If yes then print Element is found
3. Else
4. Print element is not found
Step 6: Exit the program
Step 7: End

Flowchart

#include <stdio.h>
#include<conio.h>
struct node
{
int info;
struct node * next;
}*start=NULL;
void create(int data)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->next=NULL;
start=temp;
}
void insertb(int data)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->next=start;
start=temp;
}
void inserte(int data)
{
struct node *temp,*q;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->next=NULL;
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
void inserts(int data, int pos)
{
struct node *temp,*q;
int i;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
q=start;
for(i=1;i<pos-1;i++)
{
q=q->next;
}
temp->next=q->next;
q->next=temp;
}
void display( )
{
struct node *temp;
temp=start;
if(temp==NULL)
{
printf("List is empty");
return;
}
else
{
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->next;
}
}
}
void dele(int data)
{
struct node *temp,*q;
temp=start;
if(start==NULL)
{
printf("List is empty");
return;
}
if(temp->info==data)
{
start=temp->next;
free(temp);
return;
}
while(temp->next->next!=NULL)
{
if(temp->next->info==data)
{
q=temp->next;
temp->next=q->next;
free(q);
}
temp=temp->next;
}
if(temp->next->info==data)
{
q=temp->next;
temp->next=NULL;
free(q);
}
return;
}
void search (int data)
{
struct node *temp;
int flag=0;
temp=start;
if(temp==NULL)
{
printf("Number not found");
return;
}
while(temp!=NULL)
{
if(temp->info==data)
{
flag=1;
break;
}
temp=temp->next;
}
if(temp->info==data)
flag=1;
if(flag==1)
{
printf("\n Number found");
}
else
{
printf("Number not found");
}
}
void main()
{
int data,pos,choice;
clrscr();
do
{
printf("\n 1. Create \n2.Insert at beg \n3.Insert at end \n4. Insert at specific pos
\n5.Traverse \n6. Delete \n7.Search");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter element:");
scanf("%d",&data);
create(data);
break;
case 2:
printf("\n Enter element:");
scanf("%d",&data);
insertb(data);
break;
case 3:
printf("\n Enter element:");
scanf("%d",&data);
inserte(data);
break;
case 4:
printf("\n Enter element:");
scanf("%d",&data);
printf("\n Enter position:");
scanf("%d",&pos);
inserts(data,pos);
break;
case 5:
display();
break;
case 6:
printf("\n Enter element:");
scanf("%d",&data);
dele(data);
break;
case 7:
printf("\n Enter element:");
scanf("%d",&data);
search(data);
break;
}
printf("\n Do you want to continue:");
scanf("%d",&choice);
}while(choice==1);
getch();
}

8. Write a 'C' Program to perform PUSH and POP Operations on Stack


using an Array.

push( ) operation:
Step 1 : Start (call to push function with argument value to be stored inside the
stack)
Step 2 : Check for stack overflow state
If stack top pointer is initialize to max-1 position then
Display a message as ‘stack is full’ and return to calling function
Otherwise
Go to step 3
Step 3 : Increment stack pointer by one index position
Step 4 : Store new element value at the index position indicated by stack top
Step 5 : Return to calling function
.
pop( ) operation:
Step 1 : Start (call to pop function)
Step 2 : Check for stack underflow state
If stack top is initialize to -1 value then
Display message as ‘stack is empty’ and return to calling function Otherwise
Go to next step
Step 3 : Store value stored at stack top index position into a temporary variable.
Step 4 : Decrement stack top by one index position
Step 5 : Display value of temporary variable as deleted Step 6 : Return to calling
function
#include <stdio.h>
#include<conio.h>
#define max 5
void main()
{
int a[max],st,i,choice,no,temp;
st=-1;
do
{
printf("Menu: 1. push \t 2. pop \t 3. display");
printf("\n Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter number:");
scanf("%d",&no);
if(st==max-1)
{
printf("\nStack is full");
break;
}
else
{
st=st+1;
a[st]=no;
printf("\n Push operation is done successfully");
}
break;
case 2:
if(st==-1)
{
printf("\nStack is empty");
break;
}
else
{
temp=a[st];
st=st-1;
printf("Element deleted from stack is %d",temp);
}
break;
case 3:
if(st==-1)
{
printf("\nStack is empty");
break;
}
else
{
printf("\n Stack elements are:");
for(i=0;i<=st;i++)
printf("\t%d",a[i]);
}
break;
default:
printf("\nInvalid choice");
}
printf("\n Do you want to continue (1 for yes/2 for
no) : ");
scanf("%d",&choice);
}while(choice==1);
getch();
}

9. Write a 'C' Program to perform INSERT and DELETE Operations on


Linear Queue using an Array.
Step1: START
Step 2: declare variables and structures.
Step 3: Decide from the following
 Insert an element
 Delete an element
 Peek
 Display the queue
 Exit
Step 4: When inserting an element the rear pointer is incremented
Step 5: When deleting an element the front pointer is incremented
Step 6: Peek will show the data pointed by front
Step 7: Display queue will traverse the queue and print all the numbers.
Step 8: EXIT
Step 9: END
#include<stdio.h>
#include<conio.h>
#definemax5
voidmain()
{
inta[max],front,rear,i,no,choice;
front=-1;
rear=-1;
do
{
printf("Menu: 1.Insert 2.Delete 3.Display");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter number");
scanf("%d",&no);
if(rear==max-1)
{
printf("\n Queue is full");
break;
}
rear=rear+1;
a[rear]=no;
if(front==-1)
front=0;
break;
case 2:
if(front==-1)
{
printf("\n queue is empty");
break;
}
no=a[front];
if(front==rear)
front=rear=-1;
else
front=front+1;
printf("\n Element deleted from queue is %d",no);
break;
case 3:
printf("\n Queue elements are:");
for(i=front;i<=rear;i++)
printf("\n%d",a[i]);
break;
default:
printf("\n Invalid Choice");
}
printf("\n Do you want to continue:(1 for yes/2 for
no)");
scanf("%d",&choice);
}while(choice==1);
getch();
}

10. Write a 'C' Program to Traverse BST in Preorder, and Post-Order, In


order.
Algorithm:
Step 1: Start
Step 2: Declare structure node pointer(temp) and variables such as data
Step 3: Accept data for root node.
Step 4: Assign memory to the temp pointer to make it a node.
Step 5: Set the info field of temp node with data and left and right pointers with NULL value
andset it asroot node
Step 6: Repeat step 7 till loop evaluates to true.
Step 7: Perform following steps:
7.1 Input next element to be stored in tree.(Enter -1 to exit the loop)
7.2 If element=-1 then exit from loop
7.3 Compare info of root node with new element If both are same then display message as
duplicate data and discard insert operation. Returntocalling function.
7.4 If new element is less than root node then create new node. Set its info field. Store
addressofnew node in left pointer of root node
7.5 If new element is greater than root node then create new node. Set its info field.
Storeaddressof new node in right pointer of root node.
7.6 Go to step 7.1
Step 8: Call inorder() preorder() and postorder() functions to traversal of tree.
Step 9: Stop
#include<stdio.h>
#include<conio.h>
#defineTRUE1
structnode
{
structnode*left;
intinfo;
structnode*right;
}*root=NULL;
structnode*createnode(intdata)
{
structnode*r;
r=(structnode*)malloc(sizeof(structnode));
r->left=NULL;
r->info=data;
r->right=NULL;
return(r);
}
voidpreorder(structnode*root)
{
if(root!=NULL)
{
printf("%d",root->info);
preorder(root->left);
preorder(root->right);
}
}
void inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d",root->info);
inorder(root->right);
}
}
void postorder(struct node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d",root->info);
}
}
void main()
{
struct node *p,*q;
int no,ch;
printf("Enter root element:");
scanf("%d",&no);
root=createnode(no);
while(TRUE)
{
printf("\nEnter next element:(for exit enter-1):");
scanf("%d",&no);
if(no==-1)
break;
p=root;
q=root;
while(no!=p->info && q!=NULL)
{
p=q;
if(no<p->info)
q=p->left;
else
q=p->right;
}
if(no<p->info)
{
p->left=createnode(no);
printf("\n left child of %d",p->info);
}
else
{
p->right=createnode(no);
printf("\n Right child of %d",p->info);
}
}
printf("\n Preorder Traversal:");
preorder(root);
printf("\n Inorder Traversal:");
inorder(root);
printf("\n Postorder Traversal:");
postorder(root);
getch();
}

You might also like