Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 77

Name: Kumar Gaurav

Roll- 20051617
CSE

Lab -8

1).WAP to create a doubly linked list and perform the following


operation:
■ Insert at beginning and end,
■ Delete from beginning and end, ■
Swap first and last node Solution:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h> void
insertAtBeginning(int);
void insertAtEnd(int); void
display(); void
removeBeginning(); void
removeEnd();
struct Node
{ int data; struct
Node *next; }*head
= NULL;
int main()
{ int choice,value,choice1,loc1,loc2; while(1){ mainMenu:
printf("\n\n*** MENU ***\n1. Insert\n2. Display\n3. Delete\n4.
Exit\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert:
"); scanf("%d",&value); while(1){
printf("Where you want to insert: \n1. At Beginning\n2. At
End\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: insertAtBeginning(value); break; case 2:
insertAtEnd(value); break; default: printf("\
nWrong Input!! Try again!!!\n\n"); goto mainMenu;
}
goto subMenuEnd;
}
subMenuEnd:
break;
case 2: display();
break;
case 3: printf("How do you want to Delete: \n1. From Beginning\n2.
From End\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: removeBeginning();
break;
case 2: removeEnd(); break; default: printf("\
nWrong Input!! Try again!!!\n\n"); goto mainMenu;
}
break;
case 4: exit(0); default: printf("\nWrong
input!!! Try again!!\n\n");
} return
0;
}
}
void insertAtBeginning(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node)); newNode-
>data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
printf("\nOne node inserted!!!\n");
}
void insertAtEnd(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = value; newNode->next =
NULL; if(head == NULL) head = newNode;
else
{
struct Node *temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n");
}
void removeBeginning()
{
if(head == NULL) printf("\n\nList
is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
}
}
}
void removeEnd()
{
if(head == NULL)
{ printf("\nList is Empty!!!\n");
}
else
{
struct Node *temp1 = head,*temp2; if(head-
>next == NULL)
head = NULL;
else
{
while(temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = NULL;
}
free(temp1);
printf("\nOne node deleted!!!\n\n");
}
}

void display()
{
if(head == NULL)
{ printf("\nList is Empty\n");
}
else
{
struct Node *temp = head; printf("\n\
nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}

OUTPUT:

2).WAP to represent a polynomial with single unknown using


Single Linked list Solution:
#include<stdio.h>
#include<stdlib.h>
struct list
{ int coef,deg;
struct list* next;
};

void createnode(struct list** head,int coef,int deg)


{
struct list* temp, *temp1; temp1
= malloc(sizeof(struct list));
temp1->coef=coef; temp1-
>deg=deg; temp1->next=NULL;

if(*head==NULL)
*head=temp1; else
{
temp=*head;

while(temp->next!=NULL) temp=temp-
>next;

temp->next=temp1;
}
}

void printing(struct list** head)


{ printf("the polynomial is :\n");
struct list* temp=*head;
while(temp)
{
printf("%dX^%d + ",temp->coef,temp->deg);
temp=temp->next;
}
}

int main()
{
struct list* head=NULL,*head2 = NULL;
int i,n,coef,deg;

printf("Enter the no of terms in the polynomial : ");


scanf("%d",&n); for(i=1;i<=n;i+
+)
{ printf("Enter coef and deg of term %d : ",i);
scanf("%d%d",&coef,&deg);
createnode(&head,coef,deg);
}
printing(&head);

return 0;
}
OUTPUT:
3) WAP to add two polynomials using single linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
typedef struct link
{ int coeff; int pow;
struct link * next;
}
my_poly; void
my_create_poly(my_poly **);
void my_show_poly(my_poly *);
void my_add_poly(my_poly **, my_poly *, my_poly *);
int main(void)
{ int ch;
do {
my_poly * poly1, * poly2, * poly3;

printf("\nCreate 1st expression\n");


my_create_poly(&poly1); printf("\
nStored the 1st expression");
my_show_poly(poly1);
printf("\nCreate 2nd expression\n");
my_create_poly(&poly2); printf("\
nStored the 2nd expression");
my_show_poly(poly2);
my_add_poly(&poly3, poly1, poly2);
my_show_poly(poly3);
printf("\nAdd two more expressions? (Y = 1/N = 0):
"); scanf("%d", &ch); } while (ch);
return 0;
}
void my_create_poly(my_poly ** node)
{ int flag;
int coeff, pow;
my_poly * tmp_node;
tmp_node = (my_poly *) malloc(sizeof(my_poly));
*node = tmp_node;
do
{ printf("\nEnter Coeff:");
scanf("%d", &coeff);
tmp_node->coeff =
coeff; printf("\nEnter
Pow:"); scanf("%d",
&pow); tmp_node->pow
= pow; tmp_node->next
= NULL;
printf("\nContinue adding more terms to the polynomial list?(Y =
1/N = 0): ");
scanf("%d", &flag);
if(flag)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly)); //Grow
the list
tmp_node = tmp_node->next;
tmp_node->next = NULL;
}
} while (flag);
}
void my_show_poly(my_poly * node) {
printf("\nThe polynomial expression is:\n");
while(node != NULL) { printf("%dx^%d",
node->coeff, node->pow); node = node-
>next; if(node != NULL) printf(" + ");
}
}
void my_add_poly(my_poly ** result, my_poly * poly1, my_poly * poly2)
{
my_poly * tmp_node;
tmp_node = (my_poly *) malloc(sizeof(my_poly));
tmp_node->next = NULL;
*result = tmp_node;
while(poly1 && poly2)
{
if (poly1->pow > poly2->pow)
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1 = poly1->next;
}
else if (poly1->pow < poly2->pow)
{
tmp_node->pow = poly2->pow;
tmp_node->coeff = poly2->coeff;
poly2 = poly2->next;
}
else
{
tmp_node->pow = poly1->pow; tmp_node-
>coeff = poly1->coeff + poly2->coeff; poly1 =
poly1->next; poly2 = poly2->next;
}
if(poly1 && poly2)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;
tmp_node->next = NULL;
}
}
while(poly1 || poly2)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next; tmp_node-
>next = NULL;

if(poly1)
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2)
{
tmp_node->pow = poly2->pow;
tmp_node->coeff = poly2->coeff;
poly2 = poly2->next;
}
}
printf("\nAddition Complete");
}

OUTPUT:

4).WAP to multiply two polynomial using single linked list.


Solution:
#include<stdio.h>
#include<stdlib.h>

struct node
{ int
coef; int
expo;
struct
node
*link;
};
struct node *insert_sort(struct node *,int ,int);
void display(struct node *poly)
{
while(poly!=NULL)
{

if(poly->expo==0) printf("%d",poly->coef);
else if(poly->expo==1 && poly->coef==1)
printf("x"); else if(poly-
>expo==1)
printf("%dx",poly->coef);
else if(poly->coef==1)
printf("x^%d",poly->expo);

else printf("%dx^%d",poly->coef,poly->expo); poly=poly-


>link;
if(poly!=NULL && poly->coef>=0)
{
printf("+");
}

struct node *polynomial_multiplication(struct node *poly1,struct node


*poly2)
{
struct node *poly3,*p2;
poly3=(struct node *)malloc(sizeof(struct node));

while(poly1!=NULL)
{
for(p2=poly2;p2!=NULL;p2=p2->link)
{

poly3=insert_sort(poly3,(poly1->coef)*(p2->coef),poly1->expo+p2->expo
);
}
poly1=poly1->link;
}
return poly3;
}
struct node *insert_sort(struct node *start,int coef,int expo)
{
struct node *temp,*p;
temp=(struct node *)malloc(sizeof(struct node ));
temp->coef=coef; temp-
>expo=expo;

if(start==NULL || expo>=start->expo)
{
if(start!=NULL && expo==start->expo)
{

start->coef=start->coef+coef;
return start;
}

temp->link=start;
start=temp;
return start;
}
else
{ p=start;
while(p->link!=NULL && p->link->expo>=expo) p=p-
>link;

if(p->expo==expo)
{
p->coef=coef+p->coef;
return start;
}

temp->link=p->link; p->link=temp;
return start;
}
}
struct node * create()
{
struct node *poly=NULL;
int ino; int expo; int coef;
printf("\nHow many terms Do you have to insert in polynomial \n");
scanf("%d",&ino);

for(int i=1;i<=ino;i++)
{
printf("\nEnter Coefficient And Exponent for term\n\n");
scanf("%d%d",&coef,&expo);
poly=insert_sort(poly,coef,expo);
display(poly);
}
return poly;
}

int main()
{
struct node *poly1,*poly2,*poly3;

poly1=create();
printf("\n NOW WE WILL ACCEPT 2ND POLYNOMIAL \n");
poly2=create();
poly3=polynomial_multiplication(poly1,poly2); printf("\n\n
Multiplication of two polynomial is \n\n"); display(poly3);

return 0;
}

OUTPUT:

LAB-9
1).WAP to create a stack and do following operations.
■ Push()
■ Pop()
■ Traverse()
■ Find-largest-ele()
■ Find-Min-ele() Solution:
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 10

int top=-1,stack[MAX];
void push(); void
pop(); void traverse();

void main()
{ int ch;
while(1)
{
printf("\n** Stack Menu **");
printf("\n\n1.Push\n2.Pop\n3.Traverse\n4.Find Largest
Element\n5.Find Smallest Element\n6.Exit"); printf("\n\
nEnter your choice(1-5):");
scanf("%d",&ch);
switch(ch)
{ case 1: push(); break;
case 2: pop(); break;
case 3: traverse();
break; case 4:
findmaxele(); break;
case 5: findminele();
break; case 6:
exit(0);
default: printf("\nWrong Choice!!");
}
}
}

void push()
{ int val;
if(top==MAX-1)
{ printf("\nStack is full!!");
}
else
{
printf("\nEnter element to
push:"); scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}

void pop()
{ if(top==-1)
{ printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}

void traverse()
{ int i;
if(top==-1)
{ printf("\nStack is empty!!");
}
else
{ printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}

void findmaxele()
{ int i,j=0;
for(i=top;i>=0;--i)
{
if(stack[i]>stack[j])
{ j=i;
}
}
printf("\nLargest element in stack :");
printf("%d",stack[j]);
return 0;
}

void findminele()
{ int i,j=0;
for(i=top;i>=0;--i)
{ if(stack[i]<stack[j])
{ j=i;
}
}
printf("\nSmallest element in stack :");
printf("%d",stack[j]);
return 0;
}
OUTPUT:
2)WAP to reverse an array using stack.
Solution:
#include<limits.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Stack
{ int top; unsigned
capacity;
int* array;
};

struct Stack* createStack(unsigned capacity)


{ struct Stack* stack= (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity; stack->top = -1;
stack->array= (int*)malloc(stack->capacity* sizeof(int));
return stack;
}
int isFull(struct Stack* stack)
{
return stack->top== stack->capacity - 1;
}

int isEmpty(struct Stack* stack)


{ return stack->top == -1;
}

void push(struct Stack* stack, int item)


{ if (isFull(stack))
return;
stack->array[++stack->top] = item;
}

int pop(struct Stack* stack)


{ if (isEmpty(stack))
return -1;
return stack->array[stack->top--];
}
void reverseArray(int arr[], int n)
{ struct Stack* stack = createStack(n);
for (int i = 0; i < n; i++) push(stack,
arr[i]);
for (int i = 0; i < n; i++)
arr[i] = pop(stack);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}

int main()
{ int size=10;
int arr[size];
printf("Enter the Elements of Array :");
for(int i=0;i<size;i++) scanf("%d",&arr[i]);
int N = sizeof(arr) /
sizeof(arr[0]); reverseArray(arr,
N); return 0;
}
OUTPUT:

3)WAP to create a Linear queue and do following operations:


■ Insert()
■ Delete()
■ Traverse() Solution:
#include <stdio.h>
#include <stdlib.h>

void push1(int); void


push2(int); int pop1(); int
pop2(); void insert(); void
delete(); void traverse();
void create(); int
stack1[100], stack2[100];
int top1 = -1, top2 = -1; int
count = 0;

int main()
{ int choice;
printf("\nQUEUE USING STACKS IMPLEMENTATION\n\
n"); printf("\n1.Insert"); printf("\n2.Delete"); printf("\
n3.Traverse"); printf("\n4.Exit"); printf("\n"); create();
while (1)
{ printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1: insert(); break; case
2: delete(); break; case 3:
traverse(); break; case 4:
exit(0); default: printf("\
nInvalid Choice\n");
}
}
}

void create()
{
top1 = top2 = -1;
}
void push1(int element)
{
stack1[++top1] = element;
}
int pop1()
{ return(stack1[top1--]);
}
void push2(int element)
{
stack2[++top2] = element;
}
int pop2()
{ return(stack2[top2--]);
}
void insert()
{ int data, i; printf("Enter
the data : "); scanf("%d",
&data); push1(data);
count++;
}

void delete()
{ int i; for (i = 0;i <=
count;i++)
{
push2(pop1());
}
pop2(); count--; for (i =
0;i <= count;i++)
{
push1(pop2());
}
}

void traverse()
{ int i; if(top1
== -1)
{
printf("\nEMPTY QUEUE\n");
}
else
{
printf("\nQUEUE ELEMENTS : ");
for (i = 0;i <= top1;i++)
{ printf(" %d ", stack1[i]);
} printf("\
n");
}
}
OUTPUT:
LAB-10
1)WAP to implement Linear Queue using Linked list. Perform
Insert() ,delete() and traverse() operation.

Solution:

#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert(); void
delete1 (); void
traverse();
int main ()
{ int choice;
while(choice != 4)
{
printf("\n*Main Menu*\n"); printf("\
n===================\n"); printf("\n1.Insert\
n2.Delete\n3.Traverse\n4.Exit\n"); printf("\nEnter
your choice ?"); scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;

case 2:
delete1();
break;

case 3:
traverse();
break;

case 4: exit(0); break; default:


printf("\nEnter valid choice?\
n");
}
}
} void
insert() {
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\
n"); scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{ front = ptr; rear =
ptr; front -> next =
NULL;
rear -> next = NULL;
}
else
{ rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
} void delete1
()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{ ptr = front; front =
front -> next;
free(ptr);
}
} void
traverse()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nPrinting values .....\n");
while(ptr != NULL)
{ printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

OUTPUT:
2) .WAP to implement Circular queue using Array. Perform Insert(),
delete() and Traverse() operation.
Solution:
#include <stdio.h>
# define max 6 int queue[max]; //
array declaration int front=-1; int
rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{ rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at
the
rear position.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
} else
if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]); front=-
1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to traverse the elements of a queue
void traverse()
{ int i=front;
if(front==-1 && rear==-1)
{ printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
} int
main()
{ int choice=1,x; // variables declaration

while(choice<4 && choice!=0) // while loop


{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Traverse the
element"); printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Enter the element which is to be


inserted"); scanf("%d", &x); enqueue(x); break;
case 2: dequeue(); break; case 3:
traverse();
}} return
0;
}

OUTPUT:
3) WAP to implement Priority Queue using array/linked list. Perform
Insert(), delete() and Traverse() operation.

Solution:
# include<stdio.h> #
include<malloc.h>
typedef struct node
{ int
priority;
int info;
struct node *link;
}NODE;
NODE *front =
NULL; // insert
method
void insert(int data,int priority)
{
NODE *temp,*q;
temp = (NODE
*)malloc(sizeof(NODE)); temp->info =
data; temp->priority = priority;
// condition to check whether the first element is empty or the
element to be inserted has more priority than the first element
if( front == NULL || priority < front->priority )
{
temp->link = front;
front = temp;
}
else
{ q = front;
while( q->link != NULL && q->link->priority <= priority )
q=q->link;
temp->link = q->link;
q->link = temp;
}
}
// delete method
void del()
{
NODE *temp;
// condition to check whether the Queue is empty or not
if(front == NULL)
printf("Queue Underflow\n");
else
{
temp = front;
printf("Deleted item is %d\n", temp->info);
front = front->link;
free(temp);
}
}
// traverse method
void traverse()
{
NODE *ptr; ptr
= front; if(front
== NULL)
printf("Queue is empty\n");
else
{ printf("Queue is :\n");
printf("Priority Item\n");
while(ptr != NULL)
{ printf("%5d %5d\n",ptr->priority,ptr->info);
ptr = ptr->link;
}
}
}
/*End of
traverse*/ // main
method
int main()
{ int choice, data, priority;
do
{ printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Traverse\n");
printf("4.Quit\n");
printf("Enter your choice :
"); scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter the data which is to be added in the
queue : "); scanf("%d",&data);
printf("Enter its priority : ");
scanf("%d",&priority);
insert(data,priority);
break;
case 2:
del();
break;
case 3:
traverse();
break;
case 4:
break;
default :
printf("Wrong choice\n");
}
}while(choice!=4);
return 0;
}
OUTPUT:

4)WAP to implement Input-restricted De-Queue using array. Perform


Insert(), delete() and Traverse() operation.
Solution:
# include<stdio.h>
# define MAX 5

int deque_arr[MAX];
int left = -1;
int right = -
1; /*Begin of
insert_right*/
void insert_right()
{
int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{ printf("Queue Overflow\n");
return;}
if (left == -1) /* if queue is initially empty */
{ left = 0;
right = 0;}
else
if(right == MAX-1) /*right is at last position of queue */
right = 0;
else
right = right+1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[right] = added_item ;
}
/*End of insert_right*/

/*Begin of insert_left*/
void insert_left()
{ int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{ printf("Queue Overflow \n");
return; }
if (left == -1)/*If queue is initially empty*/
{ left = 0; right
= 0; }
else
if(left== 0)
left=MAX-1;
else
left=left-1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[left] = added_item ; }
/*End of insert_left*/

/*Begin of delete_left*/
void delete_left()
{ if (left == -1)
{ printf("Queue Underflow\n");
return ; }
printf("Element deleted from queue is : %d\n",deque_arr[left]);
if(left == right) /*Queue has only one element */
{ left = -1;
right=-1; }
else if(left == MAX-
1) left = 0;
else left = left+1;
}
/*End of delete_left*/

/*Begin of delete_right*/
void delete_right()
{if (left == -1)
{printf("Queue Underflow\n");
return ; }
printf("Element deleted from queue is : %d\n",deque_arr[right]);
if(left == right) /*queue has only one element*/
{ left = -1;
right=-1; }
else if(right == 0)
right=MAX-1;
else
right=right-1; }
/*End of delete_right*/
/*Begin of display_queue*/
void display_queue()
{ int front_pos = left,rear_pos = right;
if(left == -1)
{ printf("Queue is empty\n");
return; }
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{ while(front_pos <= rear_pos)
{ printf("%d ",deque_arr[front_pos]); front_pos++; } }
else
{ while(front_pos <= MAX-1)
{ printf("%d ",deque_arr[front_pos]); front_pos++; }
front_pos = 0;
while(front_pos <= rear_pos)
{ printf("%d ",deque_arr[front_pos]); front_pos+
+;
}
} printf("\
n");
}
/*End of
display_queue*/ /*Begin of
input_que*/ void
input_que()
{ int choice;
do
{ printf("1.Insert at right\n");
printf("2.Delete from left\n");
printf("3.Delete from right\
n"); printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{ case 1:
insert_right();
break;
case 2:
delete_left();
break;
case 3:
delete_right();
break;
case 4:
display_queue();
break;
case 5:
break;
default: printf("Wrong choice\
n");
}
}while(choice!=5);
}
/*End of input_que*/

/*Begin of output_que*/
void output_que()
{ int choice;
do
{ printf("1.Insert at right\n");
printf("2.Insert at left\n");
printf("3.Delete from left\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_left();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=5);
}
/*End of output_que*/

/*Begin of main*/
int main() {
int choice;
printf("1.Input
restricted
dequeue\n");
printf("2.Output
restricted
dequeue\n");
printf("Enter your
choice : ");
scanf("%d",&choi
ce);
switch(choice)
{ case 1 :
input_que();
break;
case 2:
output_que();
break;
default: printf("Wrong
choice\n");
}
}
OUTPUT:

LAB-11
1) WAP to implement output restricted queue using array. Solution:

# include<stdio.h> #
define MAX 5 int
deque_arr[MAX];
int left = -1;
int right = -1;

/*Begin of insert_right*/
void insert_right()
{
int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{ printf("Queue Overflow\n");
return;}
if (left == -1) /* if queue is initially empty */
{ left = 0;
right = 0;}
else
if(right == MAX-1) /*right is at last position of queue */
right = 0;
else
right = right+1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[right] = added_item ;
}
/*End of insert_right*/

/*Begin of insert_left*/
void insert_left()
{ int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{ printf("Queue Overflow \n");
return; }
if (left == -1)/*If queue is initially empty*/
{ left = 0; right
= 0; }
else
if(left== 0)
left=MAX-1;
else left=left-
1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[left] = added_item ; }
/*End of insert_left*/

/*Begin of delete_left*/
void delete_left()
{ if (left == -1)
{ printf("Queue Underflow\n");
return ; }
printf("Element deleted from queue is : %d\n",deque_arr[left]);
if(left == right) /*Queue has only one element */
{ left = -1;
right=-1; }
else if(left == MAX-
1) left = 0;
else left = left+1;
}
/*End of delete_left*/
/*Begin of delete_right*/
void delete_right()
{if (left == -1)
{printf("Queue Underflow\n");
return ; }
printf("Element deleted from queue is : %d\n",deque_arr[right]);
if(left == right) /*queue has only one element*/
{ left = -1;
right=-1; }
else if(right == 0)
right=MAX-1;
else
right=right-1; }
/*End of delete_right*/
/*Begin of display_queue*/
void display_queue()
{ int front_pos = left,rear_pos = right;
if(left == -1)
{ printf("Queue is empty\n");
return; }
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{ while(front_pos <= rear_pos)
{ printf("%d ",deque_arr[front_pos]); front_pos++; } }
else
{ while(front_pos <= MAX-1)
{ printf("%d ",deque_arr[front_pos]); front_pos++; }
front_pos = 0;
while(front_pos <= rear_pos)
{ printf("%d ",deque_arr[front_pos]); front_pos+
+;
}
} printf("\
n");
}
/*End of
display_queue*/ /*Begin of
input_que*/ void
input_que()
{ int choice;
do
{ printf("1.Insert at right\n");
printf("2.Delete from left\n");
printf("3.Delete from right\
n"); printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{ case 1:
insert_right();
break;
case 2:
delete_left();
break;

case 3:
delete_right();
break;

case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=5);
}
/*End of input_que*/

/*Begin of output_que*/
void output_que()
{ int choice;
do
{ printf("1.Insert at right\n");
printf("2.Insert at left\n");
printf("3.Delete from left\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_left();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=5);
}
/*End of output_que*/

/*Begin of main*/
int main()
{ int choice; printf("1.Input restricted
queue\n"); printf("2.Output restricted
queue\n"); printf("Enter your choice :
"); scanf("%d",&choice);
switch(choice)
{

case 1 :
input_que();
break;

case 2:
output_que();
break;
default:
printf("Wrong choice\n");
}
}

OUTPUT:
2) .WAP to delete all nodes containing even numbers.
Solution:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{ int data; struct
node *next; struct
node *prev; }node;
node *head = NULL;
node *tail = NULL;
void create(int val)
{
node *temp = (node
*)malloc(sizeof(node)); temp->data = val;
temp->next = NULL; temp->prev = NULL;
if (head == NULL)
{
head = temp;
tail = head;
}
else
{
node *q=tail;
temp->prev = q;
tail = temp; q-
>next = tail;
}
} void
traverse()
{
node *q = head;
if (head == NULL)
printf("The linked list is empty");
else while (q !=
NULL)
{
printf("%d", q->data);
q = q->next; if(q!
=NULL)
printf(" <-> ");
}
} void delete(int
t)
{
node *q = head;
for(int i=0; i<t-1; i++)
q = q->next;
if(q==head)
{
q = q->next;
head = q ;
head -> prev = NULL ;
}
else if(q==tail)
{
q=q->prev; q-
>next = NULL;
tail = q ;
}
else
{
node *temp = q->prev ; temp-
>next = q->next ; q->next->prev =
temp ;
q = temp ;
}
}
void prog()
{
node *q = head;
int t=1; if (head
== NULL)
printf("The linked list is empty");
else while (q !=
NULL)
{
if(q->data%2==0)
delete(t--);
q = q->next;
t++;
}
} int
main()
{ int n,val,k;
printf("\nEnter the number of nodes:
"); scanf("%d", &n); printf("\nEnter the
data : "); for (int i = 0; i < n; i++)
{
scanf("%d", &val);
create(val);
}
prog();
printf("\nElements of the linked list are: "); traverse();
return 0;
}
OUTPUT:

3) WAP to convert an infix expression to postfix expression.


Solution:
#include<stdio.h>
#include<ctype.h>

char stack[100];
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;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp)
; printf("\n"); 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());
}return 0;
}

OUTPUT:
4) WAP to check whether an expression is balanced or not (containing
opening and corresponding closing braces).
Solution:
#include<stdio.h>
int main()
{ char expression[50]; // declaration of char type array
int x=0, i=0; // declaration of two integer type
variables printf("\nEnter an expression"); scanf("%s",
expression);
// Scanning the expression until we reach the end of the expression.
while(expression[i]!= '\0')
{
// Condition to check the symbol is '('
if(expression[i]=='(')
{ x++; // incrementing 'x'
variable }
// condition to check the symbol is ')'
else if(expression[i]==')')
{ x--; // decrementing 'x' variable
if(x<0) break;
} i++; // incrementing 'i'
variable.
}
// Condition to check whether x is equal to 0 or not.
if(x==0)
{ printf("Expression is balanced");
}

else
{
printf("Expression is unbalanced");
} return
0;
}

OUTPUT:
5)WAP to reverse a doubly linked list.
Solution:
#include <stdio.h>
#include <stdlib.h>

/*
* Basic structure of Node*/ struct
node {
int data; struct
node * prev;
struct node *
next;
}*head, *last;

/*
* Functions used in this program
*/ void
createList(int n);
void displayList();
void reverseList();

int main()
{ int n, data, choice=1;

head = NULL;
last = NULL;

/*
* Runs forever until user chooses 0
*/ while(choice !
= 0)
{ printf("============================================\n");
printf("DOUBLY LINKED LIST PROGRAM\n");
printf("============================================\
n"); printf("1. Create List\n"); printf("2. Reverse List\n"); printf("3.
Display list\n"); printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Enter your choice : "); scanf("%d",
&choice);

switch(choice)
{
case 1: printf("Enter the total number of nodes in
list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
reverseList();
break;
case 3:
displayList();
break;
case 0:
break;
default: printf("Error! Invalid choice. Please choose
between 0-3");
}

printf("\n\n\n\n\n");
}

return 0;
}

/**
* Creates a doubly linked list of n nodes.
* @n Number of nodes to be created
*/
void createList(int n)
{ int i, data;
struct node *newNode;

if(n >= 1)
{
/*
* Create and link head node
*/ head = (struct node *)malloc(sizeof(struct
node));

printf("Enter data of 1 node: ");


scanf("%d", &data);

head->data = data;
head->prev = NULL;
head->next = NULL;
last = head;
/*
* Create and link rest of the n-1 nodes
*/ for(i=2; i<=n;
i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

printf("Enter data of %d node: ", i);


scanf("%d", &data);

newNode->data = data;
newNode->prev = last; // Link new node with the previous node
newNode->next = NULL;

last->next = newNode; // Link previous node with the new node


last = newNode; // Make new node as last/previous node
}
printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
/**
* Display the content of the list from beginning to end
*/ void
displayList()
{
struct node * temp;
int n = 1;

if(head == NULL)
{ printf("List is empty.\n");
}
else
{
temp = head;
printf("DATA IN THE LIST:\n");

while(temp != NULL)
{
printf("DATA of %d node = %d\n", n, temp->data); n+
+;
/* Move pointer to next node */
temp = temp->next;
}
}
}
/**
* Reverse order of the doubly linked list
*/ void
reverseList()
{
struct node *current, *temp;
current = head;
while(current != NULL)
{
/*
* Swap the previous and next address fields of current node
*/ temp = current->next;
current->next = current-
>prev; current->prev = temp;
/* Move the current pointer to next node which is stored in temp
*/ current = temp;
}
/*
* Swap the head and last pointers
*/ temp =
head; head =
last; last =
temp;
printf("LIST REVERSED SUCCESSFULLY.\n");
}

OUTPUT:

6)WAP to swap 1st and last node of a circular double linked list.
Solution:#include
<stdio.h>
#include <stdlib.h>
// C program for
// Swap first and last nodes in circular Linked
List // Define structure of linked list Node
typedef struct LinkNode
{
// Define useful field of LinkNode
int data;
struct LinkNode * next;
}LinkNode;
LinkNode * getLinkNode(int data, LinkNode * first)
{
// Create dynamic memory of LinkNode
LinkNode * me = (LinkNode * ) malloc(sizeof(LinkNode));
if (me == NULL)
{
// Failed to create memory
return NULL;
}
me->data = data; me-
>next = first;
return me;
}
typedef struct CircularLinkedList
{
// Define useful field of CircularLinkedList
struct LinkNode * head;
}CircularLinkedList;

CircularLinkedList * getCircularLinkedList()
{
// Create dynamic memory of CircularLinkedList
CircularLinkedList * me =
(CircularLinkedList * ) malloc(sizeof(CircularLinkedList));
if (me == NULL)
{
// Failed to create memory
return NULL;
}
me->head = NULL;
return me;
}
// Insert node at end of circular linked list
void insert(CircularLinkedList * me, int value)
{
// Create a new node
LinkNode * node = getLinkNode(value, me->head);
if ((me->head == NULL))
{
// First node of linked list me-
>head = node;
node->next = me->head;
}
else
{
LinkNode * temp = me-
>head; // Find the last node
while (temp->next != me->head)
{
// Visit to next node
temp = temp->next;
}
// Add new node at the last
temp->next = node;
}
}
// Display node element of circular linked list
void display(CircularLinkedList * me)
{
if ((me->head == NULL))
{ printf("Empty Linked List");
}
else
{ printf("Linked List Element :");
LinkNode * temp = me->head;
// iterate linked list
while (temp != NULL)
{
// Display node printf("
%d", temp->data); // Visit
to next node temp =
temp->next;
if ((temp == me->head))
{
// Stop iteration
return;
}
}
}
}
// Swap or exchange first and last nodes
void swapNode(CircularLinkedList * me)
{
if ((me->head == NULL))
{ printf("Empty linked list");
return;
}
else if ((me->head->next == me->head))
{
// Only onle element of linked list printf("\
n Only One element of linked list"); return;
}
// Auxiliary variables
LinkNode * temp = me->head;
LinkNode * first = me->head;
LinkNode * prev = me->head;
while (temp->next != me->head)
{
// Get second last
node prev = temp; //
Find last node
temp = temp->next;
}
if ((prev == me->head))
{
// Only two element in this linked
list // So set second node as head
me->head = prev->next;
}
else
{
// Swap node link //
That is last node temp =
prev->next; // Modified
link temp->next = first-
>next; prev->next = me-
>head; first->next =
temp;
// Make last node to head
me->head = temp;
}
} int
main()
{
CircularLinkedList * cll = getCircularLinkedList();
// Insert element of linked list
insert(cll, 1);
insert(cll, 3);
insert(cll, 5);
insert(cll, 2);
insert(cll, 6);
insert(cll, 7);
printf("\n Before
"); // Before
swap // Display all
nodes display(cll);
swapNode(cll);
printf("\n After
"); // After swap //
Display all nodes
display(cll);
}

OUTPUT:

LAB-12
1)WAP to sort the numbers using Bubble sort.
(i) Ascending
(ii) Descending Solution:#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\


n"); scanf("%d", &n); printf("Enter %d
integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{ for (d = 0 ; d < n - c - 1; d+
+)
{ if (array[d] > array[d+1]) /* For decreasing order use '<' instead of
'>' */
{ swap = array[d];
array[d] =
array[d+1];
array[d+1] = swap;
}
}
} printf("Sorted list in ascending order:\

n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

for (c = 0 ; c < n - 1; c++)


{ for (d = 0 ; d < n - c - 1; d+
+)
{ if (array[d] < array[d+1]) /* For decreasing order use '<' instead of
'>' */
{ swap = array[d];
array[d] =
array[d+1];
array[d+1] = swap;
}
}
}
pr
in
tf(
"S
or
te
d
lis
t
in
de
sc
en
di
ng
or
de
r:\
n"
);
for (c = 0; c < n; c++)
printf("%d\n", array[c]);

return 0;
}
OUTPUT:-

2. WAP to sort the numbers using insertion sort.


(i) Ascending
Solution:#include
<stdio.h>

int main()
{ int n, array[1000], c, d, t; printf("Enter
number of elements\n"); scanf("%d",
&n); printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);
} for (c = 1 ; c <= n - 1; c+

+) { d = c;
while ( d > 0 && array[d] < array[d-1])
{t = array[d]; array[d] =
array[d-1]; array[d-1] = t;

d--;
}
} printf("Sorted list in ascending order:\

n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0;
}
OUTPUT:-

(ii) Descending
Solution:-
#include <stdio.h> void
print_array(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
} printf("\
n");
}
void insertion_sort(int arr[], int size)
{
for (int step = 1; step < size; step++)
{ int key =
arr[step]; int j =
step - 1;
while (key > arr[j] && j >= 0)
{
// For ascending order, change key> arr[j] to key < arr[j].
arr[j + 1] = arr[j];
--
j; }
arr[j + 1] = key;
}
} int
main()
{ int arr[] = {12, 38, 42, 6, 21}; int
size = sizeof(arr) / sizeof(arr[0]);
insertion_sort(arr, size);
printf("Sorted array in descending order:\n");
print_array(arr, size);
}
OUTPUT:

Q3. WAP to create heap from a set of numbers.


(I) Max heap
Solution: #include
<stdio.h> int size = 0;
void swap(int *a, int
*b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void heapify(int array[], int size, int i)
{ if (size ==
1)
{
printf("Single element in the heap");
}
else
{ int largest = i; int l = 2 * i + 1; int r = 2
* i + 2; if (l < size && array[l] >
array[largest]) largest = l;
if (r < size && array[r] > array[largest])
largest = r;
if (largest != i)
{
swap(&array[i], &array[largest]);
heapify(array, size, largest);
}
}
}
void insert(int array[], int newNum)
{ if (size ==
0)
{
array[0] = newNum;
size += 1;
}
else
{
array[size] = newNum;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--)
{ heapify(array, size,
i);
}
}
}
void deleteRoot(int array[], int num)
{ int i; for (i = 0; i <
size; i++)
{ if (num ==
array[i])
break;
}

swap(&array[i], &array[size - 1]);


size -= 1;
for (int i = size / 2 - 1; i >= 0; i--)
{ heapify(array, size,
i);
}
}
void printArray(int array[], int size)
{ for (int i = 0; i < size; +
+i)
printf("%d ", array[i]);
printf("\n");
} int
main()
{ int
array[10];

insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);

printf("Max-Heap array: ");


printArray(array, size);
deleteRoot(array, 4); printf("After
deleting an element: ");

printArray(array, size);
}
OUTPUT:

(ii)Min heap
Solution:-
#include <stdio.h> int
size = 0; void swap(int
*a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void heapify(int array[], int size, int i)
{ if (size ==
1)
{
printf("Single element in the heap");
}
else
{ int smallest = i; int l = 2 * i + 1; int r = 2
* i + 2; if (l < size && array[l] <
array[smallest]) smallest = l;
if (r < size && array[r] < array[smallest])
smallest = r;
if (smallest != i)
{
swap(&array[i], &array[smallest]);
heapify(array, size, smallest);
}
}
}
void insert(int array[], int newNum)
{ if (size ==
0)
{
array[0] = newNum;
size += 1;
}
else
{
array[size] = newNum;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(array, size, i);
}
}
}
void deleteRoot(int array[], int num)
{ int i; for (i = 0; i <
size; i++)
{ if (num ==
array[i])
break;
}

swap(&array[i], &array[size - 1]);


size -= 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(array, size, i);
}
}
void printArray(int array[], int size)
{ for (int i = 0; i < size; ++i)
printf("%d ", array[i]);
printf("\n");
} int
main()
{ int
array[10];
insert(array,3);
insert(array, 6);
insert(array, 4);
insert(array, 2);
insert(array, 7);

printf("Min-Heap array: ");


printArray(array, size);
deleteRoot(array, 4); printf("After
deleting an element: ");

printArray(array, size);
}
OUTPUT:-

Then WAP to sort the numbers in ascending and descending order


using heap sort in above programs using array.
I)ASCENDING ORDER
Solution:#include
<stdio.h>

// Function to swap the the position of two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i) {


// Find largest among root, left child and right
child int largest = i; int left = 2 * i + 1; int right = 2 *
i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;

// Swap and continue heapifying if root is not largest


if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

// Main function to do heap sort


void heapSort(int arr[], int n) {
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);

// Heapify root element to get highest element at root again


heapify(arr, i, 0);
}
}

// Print an array void


printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}

// Driver code
int main() {
int arr[] = {5, 19, 21, 3, 9, 42}; int
n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
printf("Sorted array in ascending order is \n");
printArray(arr, n);
}
OUTPUT:-

ii)DESCENDING ORDER
Solution:#include
<stdio.h>

// Function to swap the the position of two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i) {


// Find largest among root, left child and right
child int largest = i; int left = 2 * i + 1; int right = 2 *
i + 2;

if (left < n && arr[left] < arr[largest])


largest = left;

if (right < n && arr[right] < arr[largest])


largest = right;

// Swap and continue heapifying if root is not largest


if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

// Main function to do heap sort


void heapSort(int arr[], int n) {
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);

// Heapify root element to get highest element at root again


heapify(arr, i, 0);
}
}

// Print an array void


printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}

// Driver code
int main() {
int arr[] = {12, 6, 24, 32, 8, 46};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);

printf("Sorted array in descending order is \n");


printArray(arr, n);
}
OUTPUT:-

4. WAP to create a Binary search tree using linked list.


Solution:#include<stdio.h>
#include<stdlib.h>

struct node
{ int data; struct node
*rlink;
struct node
*llink; }*tmp=NULL;

typedef struct node NODE;


NODE *create(); void
preorder(NODE *); void
inorder(NODE *); void
postorder(NODE *); void
insert(NODE *);

int main()
{ int n,i,ch;
do
{

printf("\n\n1.Create\n\n2.Insert\n\n3.Preorder\n\n4.Postorder\n\
n5.In order\n\n6.Exit\n\n"); printf("\n\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
tmp=create();
break;
case 2:
insert(tmp);
break;
case 3: printf("\n\nDisplay Tree in Preorder
Traversal : ");
preorder(tmp);
break;
case 4: printf("\n\nDisplay Tree in Postorder
Traversal : ");
postorder(tmp);
break;
case 5: printf("\n\nDisplay Tree in Inorder
Traversal : ");
inorder(tmp);
break;
case 6: exit(0); default:
printf("\n Inavild Choice.."); }
}
while(n!=5);
}
void insert(NODE *root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
} root=root-
>llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}
NODE *create()
{
NODE *newnode;
int n;
newnode=(NODE
*)malloc(sizeof(NODE)); printf("\n\
nEnter the Data "); scanf("%d",&n);
newnode->data=n;
newnode->llink=NULL; newnode-
>rlink=NULL;
return(newnode);
}
void postorder(NODE *tmp)
{
if(tmp!=NULL)
{
postorder(tmp->llink); postorder(tmp-
>rlink);
printf("%d->",tmp->data);
}
}
void inorder(NODE *tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink); printf("%d->",tmp-
>data);
inorder(tmp->rlink);
}
}
void preorder(NODE *tmp)
{
if(tmp!=NULL)
{
printf("%d->",tmp->data); preorder(tmp-
>llink);
preorder(tmp->rlink);
}
}

OUTPUT:-

You might also like