DSU 1-10
DSU 1-10
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 (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();
}
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();
}
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;
}
}
}
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
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();
}
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();
}