C With Data Structure 1-6
C With Data Structure 1-6
Given {4,7,3,2,1,7,9,0}, find the location of 7 using Binary search and also display its
first occurrence.
}
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;
Output:
02.Given {5,3,1,6,0,2,4} order the numbers in ascending order using Quick Sort.
}
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.
Output:
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 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
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):
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
{
}
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: