You are on page 1of 38

DSA LAB FILE

SEMESTER-2,2022-23

SUBMITTED BY->
GAURAV SAIN
Roll no:-2021UCS1714
Section:-CSE-2

Q1.Write a program to nd the mean and the median of the numbers stored in an
array.

#include <stdio.h>

int main(void){
int n;
int total=0;

printf("Enter the array size\n");


scanf("%d",&n);

int arr[n];

printf("Enter the array elements now!\n");


for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
total+=arr[i];
}

//mean
printf("Mean is:%d\n",total/n);

//median
if(n%2==0){
printf("Median is:%d\n",arr[n/2]);
}else{
printf("Median is:%d\n",arr[n/2] +arr[(n-1)/2]);
}

return 0;
}

fi

Q2.Write a program to implement a stack using an array.

#include "stdio.h"
#include "stdlib.h"
/* Array implementation of stacks in C*/

struct stack{
int top;
int capacity;
int *arr;
};

//function to return pointer to a structure i.e. pointer to a


stack
struct stack* newStack(int capacity){
struct stack *st=(struct stack*)malloc(sizeof(capacity));//
this 'st' is stack pointer.
st->capacity=capacity;
st->top=-1;
st->arr=(int*)malloc(sizeof(int)*capacity);

return st;
}

// is full and is Emptty functions will return true and false as


1 and 0
int isEmpty(struct stack *st){
return st->top == -1;
}

int isFull(struct stack *st){


return st->top== st->capacity -1;
}

void push(struct stack *st,int val){


if(isFull(st)){
printf("Stack is Full Can't Add Now");

}else{
st->top++;
st->arr[st->top]=val;
}
}

int peek(struct stack *st){


if(isEmpty(st)){

printf("Stack is empty Add some elements First");


return 0;
}else{
return st->arr[st->top];
}
}

int pop(struct stack *st){


if(isEmpty(st)){
printf("Stack is empty ");
return 0;
}else{
int popped=st->arr[st->top];
st->top--;
return popped;
}
}

int size(struct stack *st){


return st->top++;
}

void display(struct stack *st){


if(!isEmpty(st)){
printf("Stack Elements are as follows \n");
int length=st->top;
while(length>=0){
printf("%d\t",st->arr[length]);
length--;
}
}else{
printf("Stack is Empty\n");
}
}

int main(void){
struct stack *st;
int size;
printf("Please Enter the size of stack\n");
scanf("%d",&size);

st=newStack(size);
printf("New Stack has been created successfully\n");
push(st,10);
display(st);
push(st,50);
display(st);
push(st,100);
display(st);

push(st,500);
display(st);
push(st,1000);
display(st);
push(st,5000);
display(st);

printf("\n");

printf("%d is deleted element\n",pop(st));


display(st);
printf("%d is top most val\n",peek(st));

===========================OUTPUT=================================

Q3. Write a program to implement a queue using an array.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct queue{
int front;
int rear;
int capacity;
int *arr;

};

//pointer to a queue structure


struct queue* newQueue(int size){
struct queue *q=(struct queue*)malloc(sizeof(struct queue));
q->front=-1;
q->rear=-1;
q->capacity=size;
q->arr=(int*)malloc(q->capacity*sizeof(int));

return q;
}

int isFull(struct queue *q){


return q->rear-q->front+1==q->capacity;
}

int isEmpty(struct queue *q){


return ( q->front==-1);
}

void enqueue(struct queue *q,int data){


if(!isFull(q)){
// if(q->front!=-1){
// q->rear++;
// q->arr[q->rear]=data;
// }else{
// q->rear++;
// q->front++;
// q->arr[q->rear]=data;

// }
q->rear++;
q->arr[q->rear]=data;
if(q->front==-1){
q->front=q->rear;
}

}else{
printf("Queue is Full\n");
}
}

int dequeue(struct queue *q){

if(isEmpty(q)){
printf("Queue is empty,Add first\n");
return -1;
}else{
int data=q->arr[q->front];
if(q->front==q->rear){
q->front=q->rear=-1;
}else{
q->front++;
}

// q->rear--;
return data;

}
return 0;
}

int front(struct queue *q){


if(!isEmpty(q)){
return q->arr[q->front];
}else{
printf("No data found\n");
return 0;
}
}

int rear(struct queue *q){


if(!isEmpty(q)){
return q->arr[q->rear];
}else{
printf("NOt found\n");
return 0;
}
}

int size(struct queue *q){


return q->rear-q->front+1;
}

void display(struct queue *q){


int len=q->front;
printf("Elements are:\n");
while(len<=q->rear){
printf("%d\t",q->arr[len]);
len++;
}
printf("\n");
}

int main(void){

struct queue *Q=newQueue(5);


enqueue(Q,1);
enqueue(Q,10);
enqueue(Q,100);
enqueue(Q,1000);
enqueue(Q,10000);
printf("%dbefore\n",size(Q));
display(Q);
dequeue(Q);
display(Q);
printf("%dafter\n",size(Q));
dequeue(Q);
display(Q);
printf("%dafter\n",size(Q));

dequeue(Q);
display(Q);
printf("%dafter\n",size(Q));

dequeue(Q);
display(Q);
printf("%dafter\n",size(Q));

dequeue(Q);
display(Q);
printf(“%dafter\n”,size(Q));}
===============================OUTPUT=========================

Q4. Write a program to implement a circular queue using an array.

#include "stdio.h"
#include <stdlib.h>

struct circular{
int front;
int rear;
int *array;
int capacity;
};

struct circular* construct(int size){


struct circular *q=(struct circular*)malloc(sizeof(struct
circular));
if(!q){
//if pointer is not formed
return NULL;
}
q->front=q->rear=-1;
q->capacity=size;
q->array=(int*)malloc(sizeof(int)*q->capacity);
if(!q->array){
return NULL;
}
return q;
}

int isFull( struct circular *q){


return (q->rear+1)%q->capacity ==q->front;
}

int isEmpty( struct circular *q){


return q->front==-1;
}

void enqueue(struct circular *q,int data){


if(isFull(q)){
printf("QUEUE OVERFLOW");
}else{
q->rear=(q->rear+1)%q->capacity;
q->array[q->rear]=data;
if(q->front==-1){
q->front=q->rear;
}

}
}

int dequeue(struct circular *q){


int data=0;
if(isEmpty(q)){
printf("Queue Underflow\n");
return -1;
}
else{
data=q->array[q->front];
if(q->front==q->rear){
q->front=q->rear=-1;
}else{
q->front=(q->front+1)%q->capacity;
}
}
return data;
}

int size(struct circular *q){


return (q->capacity - q->front + q->rear +1)%q->capacity==0?q-
>capacity:(q->capacity - q->front + q->rear +1)%q->capacity;
}

void delete(struct circular *q){


if(q){
if(q->array){
free(q->array);
}
free(q);
printf("Queue deleted successfully\n");
}
}

void display(struct circular *q){


int pt=q->front;

while(pt!=q->rear){
printf("%d\t", q->array[pt]);
pt=(pt+1)%q->capacity;
}
//for printing the rear element
printf("%d",q->array[pt]);
printf("\n");
}

int main(void){
struct circular *q=construct(6);
enqueue(q,1);
display(q);

enqueue(q,2);
display(q);
enqueue(q,3);
display(q);
enqueue(q,4);
display(q);
enqueue(q,5);
display(q);
enqueue(q,5);
display(q);
dequeue(q);
display(q);

dequeue(q);
display(q);

printf("size is:%d",size(q)) ;

==============================OUTPUT===========================

Q.5 Write a program to search for a number in an array.

#include "stdio.h"

int main (void){


int arr[]={13,45,67,23,45,38,78,90,45,25,17,18,24};

int num;

int sz=sizeof(arr)/sizeof(arr[0]);

printf("Enter the number you want to search!!\n");


scanf("%d",&num);

for(int i=0;i<sz;i++){
if(arr[i]==num){
printf("Element is found!\n");
return 0;
}
}

printf("Element Not Found!\n");


}

============================OUTPUT=============================

Q.6 Write a program to insert one element in an array and


delete an element from an array

#include<stdio.h>

// Inserts a key in arr[] of given capacity.

// function returns n + 1 if insertion

int insertSorted(int arr[], int n,int key,int capacity)


{
// Cannot insert more elements if n is
// already more than or equal to capacity
if (n >= capacity)
return n;

arr[n] = key;

return (n + 1);
}

int deleteElement(int arr[], int n,int key)


{
// Find position of element to be deleted

// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];

return n - 1;
}

int main()
{
int arr[20] = {12, 16, 20, 40, 50, 70};
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;

printf("\n Before Insertion: ");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

// Inserting key
n = insertSorted(arr, n, key, capacity);

printf("\n After Insertion: ");


for (i = 0; i < n; i++)
printf("%d ",arr[i]);

// deletion operation now

printf("Array before deletion\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

n = deleteElement(arr, n, key);

printf("\nArray after deletion\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;

===============================OUTPUT============================

Q.8 Write a program to store the marks obtained by 10


students in 5 courses in a two-DIMENSIONAL ARRAY

#include "stdio.h"
#define students 10
#define subjects 5

int main(void){
int marks[students][subjects];
// will store marks of 10 students in five subjects

for(int i=0;i<students;i++){
for(int j=0;j<subjects;j++){
printf("Enter the marks of %dth student in %dth
subject!\n",i+1,j+1);
scanf("%d",&marks[i][j]);
}
}

//displaying now
for(int i=0;i<students;i++){
for(int j=0;j<subjects;j++){
printf("Marks of %dth student in %dth subject are:
%d\n",i+1,j+1,marks[i][j]);
}
}

return 0;

==============================OUTPUT=============================

Q.9. Write a program to implement a priority queue using


a linked list.

#include "stdio.h"
#include "stdlib.h"

// priority queue sorts the data on the basis of some priority

typedef struct node{


int data;
int priority;

struct node *next;


}Node;

Node* createNode(int data,int priority){


Node *nn=(Node*)malloc(sizeof(Node));
//nn-> new node
nn->data=data;
nn->priority=priority;
nn->next=NULL;

return nn;
}

int peek(Node **head){


return (*head)->data;
}

void push(Node **head,int data,int priority){


//priority-> current elements priority
Node *temp=*head;

Node *nn=createNode(data,priority);

if((*head)->priority<priority){
nn->next=*head;
// updating head pointer
(*head)=nn;
}
else{
//traversing and finding position to insert
while(temp->next!=NULL && temp->priority<priority ){
temp=temp->next;
}
nn->next=temp->next;
temp->next=nn;
}
}

int pop(Node **head){


Node *temp=(*head);
(*head)=(*head)->next;

free(temp);

return (*head)->data;
}

void display(Node **head){


Node *temp=(*head);

printf("Priority Queue is as follows:");


while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}

int main(void){
//adding values to priority queue
Node *head=createNode(4,1);

push(&head,2,2);
push(&head,7,5);
push(&head,4,3);
push(&head,5,4);

display(&head);

printf("Highest Priority val:%d\n",peek(&head));

int val=pop(&head);
printf("Deleted:%d\n",val);

//after deleting a node


display(&head);

return 0;
}

================================OUTPUT============================

Q.10.Write a program to construct a binary tree and display its preorder,


inorder and postorder traversals.

#include <stdio.h>
#include <stdlib.h>
// structure of a Binary tree node
typedef struct node{
int data;
struct node *left;
struct node *right;
}TreeNode;

// function to create an Node(leaf) pointer


TreeNode* createNode(int data){
TreeNode *nn=(TreeNode*)malloc(sizeof(TreeNode));
nn->data=data;
nn->left=NULL;
nn->right=NULL;

return nn;
}

void preOrder(TreeNode *node){


if(node==NULL){
return;
}

printf("%d ",node->data);
preOrder(node->left);
preOrder(node->right);
}

void inOrder(TreeNode *node){


if(node==NULL){
return;
}

inOrder(node->left);
printf("%d ",node->data);
inOrder(node->right);
}

void postOrder(TreeNode *node){


if(node==NULL){
return;
}

postOrder(node->left);
postOrder(node->right);
printf("%d ",node->data);
}

int main(void){

TreeNode *root=createNode(10);

TreeNode *nn1=createNode(20);
TreeNode *nn2=createNode(21);

root->left=nn1;
root->right=nn2;

TreeNode *nn3=createNode(45);

nn1->left=nn3;
TreeNode *nn4=createNode(63);

nn1->left->right=nn4;

TreeNode *nn5=createNode(34);
nn2->left=nn5;

nn2->right=createNode(27);
printf("%d\n",root->right->left->data);

nn2->right->left=createNode(18);

// Now tree has been successfully constructed

printf("Preorder Traversal Is as Follows:\n");


preOrder(root);
printf("\n");

printf("Inorder Traversal Is as Follows:\n");


inOrder(root);
printf("\n");

printf("Postorder Traversal Is as Follows:\n");


inOrder(root);
printf("\n");

==============================OUTPUT===========================

11. Write a program to implement a doubly-linked list.

#include <stdio.h>
#include <stdlib.h>
// doubly linked list implementation

struct Node{
int data;
struct Node *prev;
struct Node *next;
};

void atFirst(struct Node **head_ref,int data){


struct Node *nn=(struct Node*)malloc(sizeof(struct Node));
nn->data=data;
if(nn==NULL){
printf("Memory not allocated\n");
return;
}
// will work for 1st element also
nn->next=*head_ref;

nn->prev=NULL;

if(*head_ref!=NULL){
(*head_ref)->prev=nn;
}
(*head_ref)=nn;

return;

void display(struct Node *node){

struct Node *temp=node;


while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");

void atLast(struct Node **head_ref,int data){


if(*head_ref==NULL){
// not added
return;
}

struct Node *nn=(struct Node*)malloc(sizeof(struct Node));


nn->data=data;
struct Node *temp=*head_ref;
while(temp->next!=NULL){
temp=temp->next;
}

temp->next=nn;
nn->next=NULL;

int main(void){
struct Node *head=NULL;
atFirst(&head,10);
atFirst(&head,23);
display(head);
atLast(&head,13);
display(head);

}
===============================OUTPUT============================

Q13.Write a program to insert a node in a linked list and delete a node


from a linked list.

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

struct Node{
/* data */
int data;
struct Node *next;
};

int size(struct Node*);

//struct Node **head_ref means the address of the pointer which


has the address of the head pointer
void atFirst(struct Node **head_ref,int data){
// 1. allocate memory to a node
struct Node *nn=(struct Node*)malloc(sizeof(struct Node));

if(nn==NULL){
printf("Memory nOT Allocated\n ");

}else{
// new node mei data dala
nn->data=data;
//new node k next mei address
nn->next=*head_ref;

//update the head pointer now


*head_ref=nn;
}

void atLast(struct Node **head_ref,int data){


struct Node *nn=(struct Node*)malloc(sizeof(struct Node));
nn->data=data;

//checking if memeory is allocated or not


if(nn==NULL){

printf("Memeory not allocated to structure pointer\n");


}else{
// making a temp node

if(*head_ref==NULL){
*head_ref=nn;
return;

struct Node *temp=*head_ref;

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

temp->next=nn;
nn->next=NULL;
}
}

void atAny(struct Node **head_ref,int data,int pos){


struct Node *nn=(struct Node*)malloc(sizeof(struct Node));

if(nn==NULL){
printf("Memory not allocated\n");
return;
}
else if(pos==1){
atFirst(head_ref,data);
}
else if(pos==size(*head_ref)){
atLast(head_ref,data);
}
else{
nn->data=data;

struct Node *temp=*head_ref;

while(pos!=1){
pos--;
temp=temp->next;
}

// gest
nn->next=temp->next;
temp->next=nn;

return;
}

void display(struct Node *node){


struct Node *temp=node;

printf("Linked list elements are:\n");

while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}

int size(struct Node *node){


int size=0;
while(node!=NULL){
size++;
node=node->next;
}

return size;
}

int main(void){
struct Node *head=NULL;
struct Node *two=NULL;
struct Node *three=NULL;
struct Node *four=NULL;

//creation of four nodes in the heap


head=(struct Node*)malloc(sizeof(struct Node));
two=(struct Node*)malloc(sizeof(struct Node));
three=(struct Node*)malloc(sizeof(struct Node));
four=(struct Node*)malloc(sizeof(struct Node));

head->data=1;
head->next=two;

two->data=2;
two->next=three;

three->data=3;
three->next=four;

four->data=4;
four->next=NULL;

atAny(&head,1000,3);
atAny(&head,100,3);
atAny(&head,10,3);
printf("Inserted Successfully! now displaying");
display(head);
return 0;

}
================================OUTPUT===========================

14. Write a program to implement a linked list.


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

struct Node{
/* data */
int data;
struct Node *next;
};

int main(void){
struct Node *head=NULL;
struct Node *two=NULL;
struct Node *three=NULL;
struct Node *four=NULL;

//creation of four nodes in the heap


head=(struct Node*)malloc(sizeof(struct Node));
two=(struct Node*)malloc(sizeof(struct Node));
three=(struct Node*)malloc(sizeof(struct Node));
four=(struct Node*)malloc(sizeof(struct Node));

head->data=1;
head->next=two;

two->data=2;
two->next=three;

three->data=3;
three->next=four;

four->data=4;
four->next=NULL;

printf("%d\n",head->data);

// see that head->next is same as two's address

printf("%d\n",two->data);

printf("%d\n",three->data);

printf("%d\n",four->data);

return 0;

}
==============================OUTPUT=============================

Q15.Write a program to add two polynomials using linked lists.


#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);
printf("\nFLAG: %c\n", flag);
if(flag) {
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
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================================

17. Write a program to sort an array.


#include "stdio.h"

int main(void){

int arr[]={12,43,17,88,34,75,43};
int len=sizeof(arr)/sizeof(arr[0]);

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

// bubble sort
for(int i=0;i<len-1;i++){
for(int j=0;j<len-i-1;j++){
if(arr[j+1]<arr[j]){
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}

}
}
// sorted array
printf("Sorted Array\n");
for(int i=0;i<len;i++){
printf("%d\t",arr[i]);
}
printf("\n");

===============================OUTPUT=============================

Q18. Merge two sorted arrays.

#include "stdio.h"

int main(void){

int nums1={1,2,3,0,0,0};
int nums2={2,5,6};

int
int i=0;
int j=0;
int k=0;

while ( i<m && j<n) {


if(nums1[i]<=nums2[j]){
arr[k]=nums1[i];
k++;
i++;
}else{
arr[k]=nums2[j];
k++;
j++;
}
}

while(i<m){
arr[k]=nums1[i];
k++;
i++;
}

while(j<n){
arr[k]=nums2[j];
k++;
j++;
}

for(int pt=0;pt<nums1.length;pt++){
nums1[pt]=arr[pt];
}

}
================================OUTPUT===========================

Q19. Write a program to implement a queue using a linked list.

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

struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void enq(int data);

void deq();
void empty();
void display();
void create();
void queuesize();

int count = 0;

void main()
{
int no, ch, e;

prin ("\n 1 - Enque");


prin ("\n 2 - Deque");
prin ("\n 3 - Front element");
prin ("\n 4 - Empty");
prin ("\n 5 - Exit");
prin ("\n 6 - Display");
prin ("\n 7 - Queue size");
create();
while (1)
{
prin ("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
prin ("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
prin ("Front element : %d", e);
else
prin ("\n No front element in Queue as queue is empty");

tf
tf
tf
tf
tf
tf
tf
tf

tf

tf
tf

break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
prin ("Wrong choice, Please enter correct choice ");
break;
}
}
}

void create()
{
front = rear = NULL;
}

void queuesize()
{
prin ("\n Queue size : %d", count);
}

void enq(int data)


{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{

tf
tf

temp=(struct node *)malloc(1*sizeof(struct node));


rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;

rear = temp;
}
count++;
}

void display()
{
front1 = front;

if ((front1 == NULL) && (rear == NULL))


{
prin ("Queue is empty");
return;
}
while (front1 != rear)
{
prin ("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
prin ("%d", front1->info);
}

void deq()
{
front1 = front;

if (front1 == NULL)
{
prin ("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{

tf
tf
tf
tf

front1 = front1->ptr;
prin ("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
prin ("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}

int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}

void empty()
{
if ((front == NULL) && (rear == NULL))
prin ("\n Queue empty");
else
prin ("Queue not empty");
}

tf

tf
tf
tf

=========================OUTPUT==========================

Q. Write a Program to reverse a linked list.


struct Node{
/* data */
int data;
struct Node *next;
};

void reverse(struct Node **head){


if(*head==NULL){
return;
}
struct Node *prev=NULL;
struct Node *curr=*head;

while(curr!=NULL){
struct Node *frwd=curr->next;
curr->next=prev;
prev=curr;
curr=frwd;
}

*head=prev;
}

void display(struct Node *node){


struct Node *temp=node;

printf("Linked list elements are:\n");


while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}

int main(void){
struct Node *head=NULL;
struct Node *two=NULL;
struct Node *three=NULL;
struct Node *four=NULL;

//creation of four nodes in the heap


head=(struct Node*)malloc(sizeof(struct Node));
two=(struct Node*)malloc(sizeof(struct Node));
three=(struct Node*)malloc(sizeof(struct Node));
four=(struct Node*)malloc(sizeof(struct Node));

head->data=1;
head->next=two;

two->data=2;
two->next=three;

three->data=3;
three->next=four;

four->data=4;
four->next=NULL;

display(head);
reverse(&head);
display(head);

return 0;

}
===============================OUTPUT=============================

THANK YOU!!

You might also like