0% found this document useful (0 votes)
10 views28 pages

C With Data Structure 1-6

Uploaded by

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

C With Data Structure 1-6

Uploaded by

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

01.

Given {4,7,3,2,1,7,9,0}, find the location of 7 using Binary search and also display its
first occurrence.

void Binarysearch(int a[ ],int beg,int end,int val)


Step1: Declare mid
Step2: if(beg<=end) then
a. Set mid=(beg+end)/2
b. if(a[mid]==val) then return mid+1 Go to Step4
c. else if(a[mid]<val) then return Binarysearch(a,mid+1,end,val)
d. else return Binarysearch(a,beg,mid-1,val)
[End if]
Step3: return -1
Step4: Exit

void BubbleSort(int a[ ],int n)


Step1: Declare i,j,temp
Step2: for i<-0 to n-1 repeat Step3 to Step4
Step3: for j<-0 to n-i-1 repeat Step4
Step4: if(a[j]>a[j+1]) then
a. temp=a[j]
b. a[j]=a[j+1]
c. a[j+1]=temp
[End if]
[End for loop]
[End for loop]
Step5: Exit

void First(int a[ ],int x,int n)


Step1: Initialize low=0,high=n-1,res=-1
Step2: while low<=high perform Step3 to Step6
Step3: Set mid=(low+hig)/2
Step4: if(a[mid]>x) then Set high=mid-1
Step5: else if(a[mid]<x) then Set low=mid+1
Step6: else
a. Set res=mid
b. Set high=mid-1
[End if]
[End while]
Step7: return res+1
Step8: Exit
#include<stdio.h>
int binarysearch(int a[],int beg,int end,int val)
{
int mid;
if(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==val)
return mid+1;
else if(a[mid]<val)
return binarysearch(a,mid+1,end,val);
else
return binarysearch(a,beg,mid-1,val) ;
}
return -1;
}
void bubbleSort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

int first(int a[],int x,int n)


{
int low=0,high=n-1,res=-1;
while(low<=high)
{
int mid=(low+high)/2;
if(a[mid]>x)
high=mid-1;
else if(a[mid]<x)
low=mid+1;
else
{
res=mid;
high=mid-1;
}

}
return res+1;
}
void main()
{
int a[]={4,7,3,2,1,7,9,0};
int val=7;
int n=sizeof(a)/sizeof(int);
int res;
int i;

printf("The elements in array before sorting are: \n");


for(i=0;i<n;i++)
printf("%d \t",a[i]);
bubbleSort(a,n);
printf("\nThe elements in array after sorting are: \n");
for(i=0;i<n;i++)
printf("[%d]\t",i+1);
printf("\n") ;
for(i=0;i<n;i++)
printf(" %d \t",a[i]);
printf("\nElement to be searched is %d",val);
res=binarysearch(a,0,n-1,val);
if(res==-1)
printf("\nElement is not present in the array");
else
{
printf("\n Element %d is present at %dth position of
array",val,res) ;
res=first(a,val,n);
printf("\n First occurrence of element %d is at %dth
position of array",val,res) ;
}

Output:

The elements in array before sorting are:


4 7 3 2 1 7 9 0
The elements in array after sorting are:
[1] [2] [3] [4] [5] [6] [7] [8]
0 1 2 3 4 7 7 9
Element to be searched is 7
Element 7 is present at 6th position of array
First occurrence of element 7 is at 6th position of array

02.Given {5,3,1,6,0,2,4} order the numbers in ascending order using Quick Sort.

void Quicksort(int a[ ],int start,int end)


Step1: if(start<end)
a. Set pindex=Partition(a,start,end)
b. Quicksort(a,start,pindex-1)
c. Quicksort(a,pindex+1,end)
[End if]
Step2: Exit

int Partition(int a[ ],int start,int end)


Step1: Set pindex=start-1
Step2: Set pivot=a[end],Declare i
Step3: for i<-start to end-1 repeat Step4
Step4: if(a[i]<pivot) then
a. Increment i
b. Set t=a[pindex]
c. Set a[pindex]=a[i]
d. Set a[i]=t
[End if]
[End for loop]
Step5: Set t=a[pindex+1
Step6: Set a[pindex+1]=a[end]
Step7: Set a[end]=t
Step8: reutrn pindex+1
Step9: Exit
#include<stdio.h>
int partition (int a[], int start, int end)
{
int i;
int pivot = a[end];
int pindex = (start - 1);

for ( i= start; i <= end - 1; i++)


{

if (a[i] < pivot)


{
pindex++;
int t = a[pindex];
a[pindex] = a[i];
a[i] = t;
}
}
int t = a[pindex+1];
a[pindex+1] = a[end];
a[end] = t;
return (pindex + 1);
}

void quicksort(int a[], int start, int end)


{
if (start < end)
{
int pindex = partition(a, start, end);
quicksort(a, start, pindex - 1);
quicksort(a, pindex + 1, end);
}
}

void printArr(int a[], int n)


{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
void main()
{
int a[] = { 5,3,1,6,0,2,4 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quicksort(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);

}
Output
Before sorting array elements are -
5 3 1 6 0 2 4
After sorting array elements are -
0 1 2 3 4 5 6

03.Perform the Merge Sort on the input {75,8,1,16,48,3,7,0} and display the output in
descending order.

void Mergesort(int a[ ],int beg,int end)


Step1: if(beg<end) then
a. Set mid=(beg+end)/2
b. Mergesort(a,beg,mid)
c. Mergesort(a,mid+1,end)
d. Merge(a,beg,mid,end)
[End if]
Step2: Exit

void Merge(int a[ ],int beg,int mid,int end)


Step1: Declare i,j,k
Step2: Set n1=mid-beg+1, n2=end-mid
Step3: Declare larr[n1],rarr[n2]
Step4: for i<-0 to n1 repeat Step5
Step5: Set larr[i]=a[beg+i]
[End for loop]
Step6: for j<-0 to n2 repeat Step7
Step7: Set rarr[j]=a[mid+1+j]
[End for loop]
Step8: Set i=0,j=0,k=beg
Step9: while i<n1 and j<n2 perform Step10 to Step12
Step10: if(larr[i]<rarr[j]) then
a. Set a[k]=larr[i],Increment i
Step11: else
b. Set a[k]=rarr[j],Increment j
[End if]
Step12: Increment k
[End while]
Step13: while i<n1 perform Step14 to Step15
Step14: Set a[k]=larr[i]
Step15: Increment i and k
[End while]
Step16: while j<n2 perform Step17 to Step18
Step17: Set a[k]=rarr[j]
Step18: Increment j and k
[End while]
Step19: Exit
#in

Output:

Array elements before sorting:


75 8 1 16 48 3 7 0
Array elements after sorting:
75 48 16 8 7 3 1 0

04.Write a program to insert the elements 61,16,8,27 into singly linked list and delete
8,61,27 from the list.Display your list after each insertion and deletion.

void Insert()
Step1: Read data
Step2: if(head==NULL) then
a. Allocate memory for p
b. p->data=data
c. p->next=NULL
d. Set head=p
Step3: else
a. Allocate memory for temp
b. temp->data=data
c. temp->next=NULL
d. while p->next!=NULL perform e
e. Set p=p->next
[End while]
f. p->next=temp;
[End if]
Step4: Exit

void Delete()
Step1: if(head==NULL) then Print Empty List
Step2: else if(p->data==data) then
a. Set head=p->next
b. Print Deleted
c. Deallocate memory of p
Step3: else
a. while p!=NULL and p->data!=data perform b
b. Set temp=p
[End while]
c. if(p==NULL) then Print No Match
d. else if p->data==data then
(i). Set temp->next=p->next
(ii). Print Deleted
(iii). Deallocte memory of p
[End if]
[End if]
Step4: Exit
void Display()
Step1: Set p=head
Step2: while p!=NULL perform Step3 to Step4
Step3: Print p->data
Step4: Set p=p->next
[End while]
Step5: Exit
#include<stdio.h>
#include<stdlib.h>

typedef struct nd
{
int data;
struct nd *next;
}node;

node *head,*p,*end,*prev;

void insert(int data)


{
if(head==NULL)
{
p=(node*)malloc(sizeof(node));
p->data=data;
p->next=NULL;
head=p;
end=p;
}
else
{
node *tmp;
tmp=(node*)malloc(sizeof(node));
tmp->data=data;
tmp->next=NULL;
p=head;
while(p->next!=NULL)
p=p->next;
p->next=tmp;
end=tmp;
}
}

void delete()
{
int data;
node *tmp;
p=head;
if(head==NULL)
printf("\n\tEmpty list");
else
{
printf("\n\tEnter item to be deleted: ");
scanf("%d",&data);
if(p->data==data)
{
head=p->next;
printf("\n\t%d is deleted\n",data);
free(p);
}
else
{
while((p!=NULL)&&(p->data!=data))
{
tmp=p;
p=p->next;
}
if(p==NULL)
{
printf("\n\tNo match\n");
}
else if(p->data==data)
{
tmp->next=p->next;
printf("\n\t%d is deleted\n",data);
free(p);
}
}
}

void display()
{
node *p;
p=head;
printf("\n\t------------------------------------------\n");
while(p!=NULL)
{
printf("\t%d ",p->data);
p=p->next;
}
printf("\n\t------------------------------------------");
}

void main()
{
int item,ch,i;

do
{
printf("\n\t------------------------------------------");
printf("\n\t BASIC OPERATIONS OF LINKED LIST");
printf("\n\t\t1.Insert\n\t\t2.Delete\n\t\t3.Exit\n");
printf("\n\t------------------------------------------");
printf("\n\tEnter your choice: ");
scanf("%d",&ch);
printf("\n\t------------------------------------------");
switch(ch)
{
case 1: printf("\n\tEnter the item to be inserted: ");
scanf("%d",&item);
insert(item);
display();
break;
case 2:delete();
display();
break;
case 3:printf("\n\tExit point");
break;
default:printf("\n\tInvalid choice");
}

}while(ch!=3);
}

Output:

------------------------------------------
BASIC OPERATIONS OF LINKED LIST
1.Insert
2.Delete
3.Exit

------------------------------------------
Enter your choice: 1

------------------------------------------
Enter the item to be inserted: 61

------------------------------------------
61
------------------------------------------
------------------------------------------
BASIC OPERATIONS OF LINKED LIST
1.Insert
2.Delete
3.Exit

------------------------------------------
Enter your choice: 1

------------------------------------------
Enter the item to be inserted: 16

------------------------------------------
61 16
------------------------------------------
------------------------------------------
BASIC OPERATIONS OF LINKED LIST
1.Insert
2.Delete
3.Exit
------------------------------------------
Enter your choice: 1

------------------------------------------
Enter the item to be inserted: 8

------------------------------------------
61 16 8
------------------------------------------
------------------------------------------
BASIC OPERATIONS OF LINKED LIST
1.Insert
2.Delete
3.Exit

------------------------------------------
Enter your choice: 1

------------------------------------------
Enter the item to be inserted: 27

------------------------------------------
61 16 8 27
------------------------------------------
------------------------------------------
BASIC OPERATIONS OF LINKED LIST
1.Insert
2.Delete
3.Exit

------------------------------------------
Enter your choice: 2

------------------------------------------
Enter item to be deleted: 8

8 is deleted

------------------------------------------
61 16 27
------------------------------------------
------------------------------------------
BASIC OPERATIONS OF LINKED LIST
1.Insert
2.Delete
3.Exit

------------------------------------------
Enter your choice: 2
------------------------------------------
Enter item to be deleted: 61

61 is deleted

------------------------------------------
16 27
------------------------------------------
------------------------------------------
BASIC OPERATIONS OF LINKED LIST
1.Insert
2.Delete
3.Exit

------------------------------------------
Enter your choice: 2

------------------------------------------
Enter item to be deleted: 27

27 is deleted

------------------------------------------
16
------------------------------------------
------------------------------------------
BASIC OPERATIONS OF LINKED LIST
1.Insert
2.Delete
3.Exit

------------------------------------------
Enter your choice: 3

------------------------------------------
Exit point

05.Write a program to add 6x^3+10x^2+0x+5 and 4x^2+2x+1 using linked list.

void Create(node* node)


Step1: do Step2 to Step6
Step2: Read node->coef and node->power
Step3: Allocate memory for node
Step4: Set node=node->next and node->next=NULL
Step5: Read ch
Step6: while(ch=='y' or ch=='Y')
[End do while]
Step7: Exit

void Show(node* node)


Step1: while node->next!=NULL perform Step2 to Step4
Step2: Print node->coef^node->power
Step3: Set node=node->next
Step4: if(node->next!=NULL) then Print ‘+’
[End if]
[End while]
Step5: Exit

void Polyadd(node* poly1,node* poly2,node* poly)


Step1: while(poly1->next and poly2->next) perform Step2 to Step6
Step2: if(ploy1->power>poly2->power) then
a. Set poly->power=poly1->power and poly->coef=poly1->coef
b. Set poly1=poly1->next
Step3: else if(poly1->power<poly2->power) then
c. Set poly->power=poly2->power and poly->coef=poly2->coef
d. Set poly2=poly2->next
Step4: else
e. Set poly->power=poly1->power and poly->coef=poly1->coef+poly2->coef
f. Set poly1=poly1->next and poly2=poly2->next
[End if]
Step5: Allocate memory for poly->next
Step6: Set poly=poly->next and poly->next=NULL
[End while]
Step7: while(poly1->next||poly2->next) perform Step11
Step8: if(poly1->next) then
a. Set poly->power=poly1->power and poly->coef=poly1->coef
b. Set poly1=poly1->next
[End if]
Step9: if(poly2->next) then
a. Set poly->power=poly2->power and poly->coef=poly2->coef
b. Set poly2=poly2->next
[End if]
[End while]
Step10: Allocate memory for poly->next
Step11: Set poly=poly->next and poly->next=NULL
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\n enter coeff:");
scanf("%d",&node->coeff);
printf("\n enter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\n continue(y/n):");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
main()
{
char ch;
do{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\nenter 1st number:");
create(poly1);
printf("\nenter 2nd number:");
create(poly2);
printf("\n1st Number:");
show(poly1);
printf("\n2nd Number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\nAdded polynomial:");
show(poly);
}
while(ch=='y' || ch=='Y');
}

Output:
enter 1st number:
enter coeff:6
enter power:3

continue(y/n):
enter coeff:10
enter power:2

continue(y/n):
enter coeff:0
enter power:1

continue(y/n):
enter coeff:5
enter power:0

continue(y/n):

enter 2nd number:


enter coeff:4
enter power:2

continue(y/n):
enter coeff:2
enter power:1

continue(y/n):
enter coeff:1
enter power:0

continue(y/n):

1st Number:6x^3+10x^2+0x^1+5x^0
2nd Number:4x^2+2x^1+1x^0
Added polynomial:6x^3+14x^2+2x^1+6x^0

06.Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack, also
display the popped elements.

PUSH_STACK(STACK,TOP,MAX,ITEM)
1) IF TOP = MAX then
Print “Stack is full”;
Exit;
2) Otherwise
TOP: = TOP + 1; /*increment TOP*/
STACK (TOP):= ITEM;
3) End of IF
4) Exit

POP_STACK(STACK,TOP,ITEM)
1) IF TOP = 0 then
Print “Stack is empty”;
Exit;
2) Otherwise
ITEM: =STACK (TOP);
TOP:=TOP – 1;
3) End of IF
4) Exit
#include<stdio.h>
void push();
void pop();
void display();
int top=-1,stack[10],choice,n,x,i;
void main()
{
printf("BASIC STACK OPERATIONS");
printf("\n 1.PUSH \n 2.POP\n 3.DISPLAY\n 4.EXIT\n");
printf("\n Enter the size of stack: ");
scanf("%d",&n);
do
{

printf("\n Enter the choice: ");


scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:
break;
default:printf("\nInvalid choice");
}
}
while(choice!=4);
}
void push()
{
if(top>=n-1)
{
printf("\n Stack Overflow");
}
else
{
printf("Enter a value to be pushed: ");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
int i;
if(top<=-1)
{
printf("\nStack Underflow");
}
else
{
printf("\nHow many times to be popped? : ");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("\nThe popped element is %d",stack[top]);
top--;
}
}

}
void display()
{
if(top>=0)
{
printf("\nThe element in the stack: ");
for(i=top;i>=0;i--)
printf("%d \t",stack[i]);
}
else
printf("\nStack is empty");
}

Output:

BASIC STACK OPERATIONS


1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter the size of stack: 5

Enter the choice: 1


Enter a value to be pushed: 5

Enter the choice: 1


Enter a value to be pushed: 9

Enter the choice: 1


Enter a value to be pushed: 34

Enter the choice: 1


Enter a value to be pushed: 17

Enter the choice: 1


Enter a value to be pushed: 32

Enter the choice: 1


Stack Overflow

Enter the choice: 3

The element in the stack: 32 17 34 9 5


Enter the choice: 2

How many times to be popped? : 3

The popped element is 32


The popped element is 17
The popped element is 34
Enter the choice: 3

The element in the stack: 9 5


Enter the choice: 4

You might also like