Final DS
Final DS
2 Design, develop and implement a menu driver program for various array
operations.
3 Program to demonstrate use of sequential search.
Program to implement singly linked list perform Search, Insert and Delete
6
operation using dynamic memory allocation.
Program to implement doubly linked list perform Search, Insert and Delete
7
operation using dynamic memory allocation.
OUTPUT:
Enter a number5
Factorial of 5 is120
CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[10];
int n,i,pos,elem;
void create();
void display();
void insert();
void Delete();
void main()
{
int ch;
while(1)
{
Printf(“\n Array operation\n”);
printf("\n1.create\n 2.display\n 3.insert\n 4.delete\n 5.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:display();
break;
case 3:insert();
break;
case 4:Delete();
break;
case 5:exit(0);
break;
default:printf("I\nnvalid choice\n");
break;
}
}
}
void create()
{
printf("\nEnter the number of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display()
{
printf("\nArray elements are\n");
for(i=0;i<n;i++)
{
printf("a[%d]=%d\t",i,a[i]);
}
}
void insert()
{
printf("\nEnter the position in which you want to insert\n");
scanf("%d",&pos);
if(pos>=n-1)
{
printf("\n Insertion not possible");
}
else
{
printf("\nEnter the value which you want to insert\n");
scanf("%d",&elem);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
a[pos]=elem;
n++;
}
}
void Delete()
{
printf("\nEnter the position which u want to delete\n");
scanf("%d",&pos);
if(pos==n-1)
{
printf("\n Deletion not possible\n");
}
else
{ elem=a[pos];
for(i=pos;i>=n;i++)
a[i]=a[i+1];
n--;
}
}
OUTPUT:
Array operation:
1.create
2.display
3.insert
4.delete
5.exit
1
Enter the number of elements
4
10
20
30
40
1.create
2.display
3.insert
4.delete
5.exit
2
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[100],n,key,i,first,last,mid;
clrscr();
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element\n");
scanf("%d",&key);
first=0;
last=n-1;
mid=(first+last)/2;
while(first<=last)
{
if(a[mid]<key)
first=mid+1;
else if(a[mid]==key)
{
printf("%d fount at location %d\n",n,key,mid+1);
break;
}
else
last=mid-1;
mid=(first+last)/2;
}
if(first>last)
printf("Elements is not found\n");
getch();
return 0;
}
OUTPUT:
Enter the size of array
5
Enter 5 integers
10
20
30
40
50
Enter the key element
40
5 found at location 40
break;
case 2:
printf("\nEnter sroll:");
scanf("%d",&sroll);
reply=search_data(a,n,sroll);
if (reply == -1)
printf("\nNot found");
else
{
printf("\nRoll\tName\tMarks");
printf("\n%d\t%s\t%d",a[reply].roll,a[reply].name,a[reply].marks);
}
break;
case3:exit(0);
}
}while(1);
}
int read_data(struct stud a[],int n)
{
int i;
printf("Enter %d records\n",n);
for(i=0;i<n;i++)
{
printf("\nRoll:");
scanf("%d",&a[i].roll);
printf("\nName:");
Dept. Of CS, VNC, Hosapete. Page No.
DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester
scanf("%s",a[i].name);
printf("\Marks:");
scanf("%d",&a[i].marks);
}
return;
}
int search_data(struct stud a[],int n,int sroll)
{
int i;
for(i=0;i<n;i++)
{
if(a[i].roll==sroll)
return(i);
}
return(-1);
}
OUTPUT:
1.Create records
2.Search record
3.exit
Select proper option:1
6. Program to implement singly linked list perform Search, Insert and Delete
operation using dynamic memory allocation.
CODE:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node,
next_node;
int data;
int main()
{
int option = 0;
printf("Singly Linked List Example - All Operations\n");
while (option < 5)
{
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
scanf("%d", &option);
switch (option)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: count();
break;
default: break;
}
}
return 0;
}
void insert()
{
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node->value = data;
if (first_node == 0)
{
first_node = temp_node;
}
else
{
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete()
{
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nDisplay Linked List : \n");
printf("\nEnter Position for Delete Element : \n");
scanf("%d", &pos);
if (pos > 0 && pos <= countvalue)
{
if (pos == 1)
{
temp_node = temp_node -> next;
first_node = temp_node;
printf("\nDeleted Successfully \n\n");
}
else
{
while (temp_node != 0)
{
if (i == (pos - 1))
{
prev_node->next = temp_node->next;
if(i == (countvalue - 1))
{
head_node = prev_node;
}
printf("\nDeleted Successfully \n\n");
break;
}
else
{
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
}
else
printf("\nInvalid Position \n\n");
}
void display()
{
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count()
{
int count = 0;
temp_node = first_node;
while (temp_node != 0)
{
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
OUTPUT:
Singly Linked List Example - All Operations
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:3
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:
No Of Items In Linked List : 3
Display Linked List :
Enter Position for Delete Element :
3
Deleted Successfully
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:
7. Program to implement doubly linked list perform Search, Insert and Delete
operation using dynamic memory allocation.
CODE
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev, *next;
};
struct node* start = NULL;
void traverse(){
if (start == NULL) {
printf("\nList is empty\n");
return;
}
struct node* temp;
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->prev = NULL;
temp->next = start;
start = temp;
}
void insertAtEnd()
{
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->next = NULL;
trav = start;
if (start == NULL)
{
start = temp;
}
else
{
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
void insertAtPosition()
{
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
printf("\nEnter position : ");
scanf("%d", &pos);
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
newnode->data = data;
temp = start;
// If start==NULL,
if (start == NULL)
{
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}
else if (pos == 1)
{
newnode->next = start;
newnode->next->prev = newnode;
newnode->prev = NULL;
start = newnode;
}
Else
{
while (i < pos - 1)
{
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
}
}
void deleteEnd()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;
if (start == NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter position : ");
scanf("%d", &pos);
if (pos == 1)
{
position = start;
start = start->next;
if (start != NULL)
{
start->prev = NULL;
}
free(position);
return;
}
while (i < pos - 1) {
temp = temp->next;
i++;
}
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
free(position);
}
}
int main()
{
int choice;
while (1)
{
printf("\n\t1 To see list\n");
printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
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. Try Again \n");
continue;
}
}
return 0;
}
OUTPUT:
1 To see list
2 For insertion at starting
3 For insertion at end
4 For insertion at any position
5 For deletion of first element
6 For deletion of last element
7 For deletion of element at any position
8 To exit
Enter Choice : 2
Enter number to be inserted: 4
1 To see list
2 For insertion at starting
3 For insertion at end
4 For insertion at any position
5 For deletion of first element
6 For deletion of last element
7 For deletion of element at any position
8 To exit
Enter Choice : 3
Enter number to be inserted: 11
1 To see list
2 For insertion at starting
3 For insertion at end
1 To see list
2 For insertion at starting
3 For insertion at end
4 For insertion at any position
5 For deletion of first element
6 For deletion of last element
7 For deletion of element at any position
8 To exit
Enter Choice :
1
Data = 4
Data = 11
Data = 9
CODE:
#include<stdio.h>
#include<conio.h>
#define N 5
int stack[N];
int top=-1;
void push();
void pop();
void peek();
void display();
void main()
{
int ch,item;
clrscr();
do
{
printf("Enter your choice\n1.push\n 2.pop\n 3.peek\n 4.display\n
5.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peek();
break;
case 4:display();
break;
case 5:exit(0);
default:printf("Invalid choice\n");
}
}while(ch!=0);
getch();
}
void push()
{
int x,item;
printf("Enter the data you want to push\n");
scanf("%d",&x);
if(top==N-1)
{
printf("Overflow\n");
}
else
{
top++;
stack[top]=x;
}
}
void pop()
{
int item;
if(top==-1)
{
printf("underflow\n");
}
else
{
item=stack[top];
top--;
printf("popped item is %d\n",item);
}
}
void peek()
{
int item;
if(top==-1)
{
printf("stack is empty\n");
}
else
{
printf("top most element in stack %d\n",stack[top]);
}
}
void display()
{
int i;
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
OUTPUT:
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
1
Enter the data you want to push
10
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
1
Enter the data you want to push
20
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
1
Enter the data you want to push
30
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
4
30
20
10
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
2
popped item is 30
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
3
top most element in stack 20
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
4
20
10
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
int main()
{
int choice, value;
printf("\nIMPLEMENTING STACKS USING LINKED LISTS\n");
while(1)
{
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nInvalid Choice\n");
}
}
}
void pop()
{
if(top == NULL)
printf("\nEMPTY STACK\n");
else
{
struct Node *temp = top;
printf("\nPopped Element : %d", temp->data);
printf("\n");
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nEMPTY STACK\n");
else
{
printf("The stack is \n");
struct Node *temp = top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n\n",temp->data);
}
}
OUTPUT
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
Popped Element : 30
1. Push
2. Pop
3. Display
4. Exit
11. Program to convert infix to postfix expression using stack, using dynamic
memory allocation.
CODE:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
char stack[20];
int top=-1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x=='(')
return 0;
if(x=='+'||x=='-')
return 1;
if(x=='*'||x=='/')
return 2;
}
void main()
{
char exp[20];
char *e,x;
printf("Enter the expression::");
scanf("%s",exp);
e=exp;
while(*e!='\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e=='(')
push(*e);
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top!=-1)
{
printf("%c",pop());
}
getch();
}
OUTPUT:
Enter the expression: A+B*C/D-F+A^E
ABC*D/+F-AE^+
front=front->next;
free(temp);
}
}
void display()
{
struct node*temp;
temp=front;
if(front==0&&rear==0)
{
printf("queue is empty\n");
}
else
{
while(temp!=0)
{
printf("\n elements in a queue is=%d",temp->data);
temp=temp->next;
}
}
}
void peek()
{
if(front==0&&rear==0)
{
printf("queue is empty\n");
}
else
{
printf("\n peek element is=%d",front->data);
}
}
void main()
{
int ch,data;
printf("\n queue operations using linked list\n");
while(1)
{
printf("\n1.enqueue\n2.dequeue\n3.display\n4.peek\n5.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n enter data enqueue\n");
scanf("%d",&data);
enqueue(data);
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:peek();
break;
case 5:exit(0);
default:printf("invalid choice\n");
}
}
}
OUTPUT:
queue operations using linked list
1.enqueue
2.dequeue
3.display
4.peek
5.exit
1
enter data enqueue
10
1.enqueue
2.dequeue
3.display
4.peek
5.exit
1
enter data enqueue
20
1.enqueue
2.dequeue
3.display
4.peek
5.exit
1
enter data enqueue
30
1.enqueue
2.dequeue
3.display
4.peek
5.exit
2
Dequed element is=10
1.enqueue
2.dequeue
3.display
4.peek
5.exit
2
Dequed element is=20
1.enqueue
2.dequeue
3.display
4.peek
5.exit
2
Dequed element is=30
1.enqueue
2.dequeue
3.display
4.peek
5.exit
3
elements in a queue is=10
elements in a queue is=20
elements in a queue is=30
4
peek element is=10
void insert()
{
int add_item;
if (rear == MAX - 1) printf("Queue Overflow \n"); else
{
if (front == - 1) front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n"); return ;
}
else
{
printf("Element deleted from queue is : %d\n",
queue_array[front]); front = front + 1;
}
}
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
OUTPUT:
****MAIN MENU****
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
Enter your choice : 1
Inset the element in queue : 20
Enter your choice : 1
Inset the element in queue : 30
Enter your choice : 3
Queue is :
10 20 30
Enter your choice : 2
Element deleted from queue is : 10
Enter your choice : 3
Queue is :
20 30
Enter your choice : 2
Element deleted from queue is : 20
Enter your choice : 3
Queue is :
30
Enter your choice : 4
Dept. Of CS, VNC, Hosapete. Page No.
DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester
{
printf("\nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
}
break;
case 2: printf("\nEnter element to be inserted:");
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
break;
case 3: printf("\nEnter the element to be inserted:");
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueF(&q,x);
break;
case 4: if(empty(&q))
{
printf("\nQueue is empty!!");
exit(0);
}
x=dequeueR(&q);
printf("\nElement deleted is %d\n",x);
break;
case 5: if(empty(&q))
{
printf("\nQueue is empty!!");
exit(0);
}
x=dequeueF(&q);
printf("\nElement deleted is %d\n",x);
break;
case 6: print(&q);
break;
default: break;
}
}while(op!=7);
getch();
}
void initialize(dequeue *P)
{
P->rear=-1;
P->front=-1;
}
int empty(dequeue *P)
{
if(P->rear==-1)
return(1);
return(0);
}
int full(dequeue *P)
{
if((P->rear+1)%MAX==P->front)
return(1);
return(0);
}
void enqueueR(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->rear=(P->rear+1)%MAX;
P->data[P->rear]=x;
}
}
void enqueueF(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->front=(P->front-1+MAX)%MAX;
P->data[P->front]=x;
}
}
int dequeueF(dequeue *P)
{
int x;
x=P->data[P->front];
if(P->rear==P->front) //delete the last element
initialize(P);
else
P->front=(P->front+1)%MAX;
return(x);
}
int dequeueR(dequeue *P)
{
int x;
x=P->data[P->rear];
if(P->rear==P->front)
initialize(P);
else
P->rear=(P->rear-1+MAX)%MAX;
return(x);
}
void print(dequeue *P)
{
if(empty(P))
{
printf("\nQueue is empty!!");
exit(0);
}
int i;
i=P->front;
while(i!=P->rear)
{
printf("\n%d",P->data[i]);
i=(i+1)%MAX;
}
printf("\n%d\n",P->data[P->rear]);
}
OUTPUT
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:1
Enter number of elements:5
Enter the data:10
20
30
40
50
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:2
Enter element to be inserted:25
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:6
10
20
30
40
50
25
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:4
Element deleted is 25
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:6
10
20
30
40
50
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:5
Element deleted is 10
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:6
20
30
40
50
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:7
void create_queue();
void insert_element(int);
void delete_element(int);
void check_priority(int);
void display_priorityqueue();
int pqueue[MAX];
int front, rear;
void main()
{
int n, choice;
printf("\nEnter 1 to insert element by priority ");
printf("\nEnter 2 to delete element by priority ");
printf("\nEnter 3 to display priority queue ");
printf("\nEnter 4 to exit");
create_queue();
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nEnter element to insert : ");
scanf("%d",&n);
insert_element(n);
break;
case 2:
printf("\nEnter element to delete : ");
scanf("%d",&n);
delete_element(n);
break;
case 3:
display_priorityqueue();
break;
case 4:
exit(0);
default:
printf("\n Please enter valid choice");
}
}
}
void create_queue()
{
front = rear = -1;
}
void insert_element(int data)
{
if (rear >= MAX - 1)
{
printf("\nQUEUE OVERFLOW");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pqueue[rear] = data;
return;
}
else
check_priority(data);
rear++;
}
void check_priority(int data)
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (data >= pqueue[i])
{
for (j = rear + 1; j > i; j--)
{
CODE:
#include<stdio.h>
#include<conio.h>
void toh(int,char,char,char);
void main()
{
int n;
printf("Enter the number of disk:\n");
scanf("%d",&n);
printf("Here is the sequence of elements of tower of hanoi\n");
toh(n,'A','B','C');
getch();
}
void toh(int no,char source,char destination,char spare)
{
if(no==1)
{
printf("move disk 1 from source %c to destination
%c\n",source,destination);
return;
}
else
toh(no-1,source,spare,destination);
printf("move disk %d from source %c to destination
%c\n",no,source,destination);
toh(no-1,spare,destination,source);
}
OUTPUT:
Enter the number of disk:
3
Here is the sequence of elements of tower of hanoi
move disk 1 from source A to destination B
move disk 2 from source A to destination C
move disk 1 from source B to destination C
move disk 3 from source A to destination B
move disk 1 from source C to destination A
move disk 2 from source C to destination B
move disk 1 from source A to destination B
#include<stdio.h>
#include<conio.h>
int fibonacci(int term);
int main()
{
int terms,counter;
printf("enter number of terms in fibonacci series:");
scanf("%d",&terms);
printf("fibonacci series till %d terms/n",terms);
for(counter=0;counter<terms;counter++){
printf("%d\n", fibonacci(counter));
}
getch();
return 0;
}
int fibonacci(int term)
{
if(term<2)
return term;
return fibonacci(term-1)+fibonacci(term-2);
}
OUTPUT:
int main()
{
int data, ch;
while (1)
{
printf("\n 1. Insertion\n 2. In-order\n 3. Pre-order\n
4. Post-order\n 5. Exit\n Enter your choice:");
scanf("%d",&ch);
switch (ch)
{
case 1: printf("Enter the data:");
scanf("%d", &data);
insertion(&root, data);
break;
case 2: inorder(root);
break;
case 3: preorder(root);
break;
case 4: postorder(root);
break;
case 5: exit(0);
OUTPUT
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:9
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:20
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:5
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:1
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:3
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:65
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:15
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:2
1 3 5 9 15 20 65
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:3
9 5 1 3 20 15 65
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:4
3 1 5 15 65 20 9
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10
void main()
{
int a[max],num,key,i;
char ans;
int create(int);
void hash(int[],int ,int);
void display(int[]);
printf("\nHash table\n");
for(i=0;i<max;i++)
a[i]=-1;
do
{
printf("\n enter the number:");
scanf("%d",&num);
key=create(num);
hash(a,key,num);
printf("\n do you want to continue");
ans=getche();
}
while(ans=='y');
display(a);
}
int create(int num)
{
int key;
key=num%10;
return key;
}
void hash(int a[max],int key,int num)
{
int flag,i,count=0;
void display(int a[]);
flag=0;
if(a[key]==-1)
a[key]=num;
else
{
i=0;
while(i<max)
{
if(a[i]!=-1)
count++;
i++;
}
if(count==max)
{
printf("\n hashtable is full");
display(a);
getch();
exit(1);
}
for(i=key+1;i<max;i++)
if(a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
for(i=0;i<key&&flag==0;i++)
if(a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
}
}
OUTPUT:
Hash table
enter the number:10
do you want to continue
the hash table...
010
1-1
2-1
3-1
4-1
5-1
6-1
7-1
8-1
9-1