Professional Documents
Culture Documents
(Lab Record)
(Lab Record)
Roll- 20051617
CSE
Lab -8
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:
if(*head==NULL)
*head=temp1; else
{
temp=*head;
while(temp->next!=NULL) temp=temp-
>next;
temp->next=temp1;
}
}
int main()
{
struct list* head=NULL,*head2 = NULL;
int i,n,coef,deg;
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;
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:
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);
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;
};
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:
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;
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.
}
}
switch(choice)
{
case 1:
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:
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:
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));
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));
newNode->data = data;
newNode->prev = last; // Link new node with the previous node
newNode->next = NULL;
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;
n");
return 0;
}
OUTPUT:-
int main()
{ int n, array[1000], c, d, t; printf("Enter
number of elements\n"); scanf("%d",
&n); printf("Enter %d integers\n", n);
+) { 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");
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:
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);
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;
}
printArray(array, size);
}
OUTPUT:-
// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
// 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>
// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
// Driver code
int main() {
int arr[] = {12, 6, 24, 32, 8, 46};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
struct node
{ int data; struct node
*rlink;
struct node
*llink; }*tmp=NULL;
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:-