0% found this document useful (0 votes)
56 views26 pages

Lab Assignment - V (Bcac393)

The document contains 3 programming assignments for a Data Structures and Algorithms lab course. The assignments involve implementing various operations on singly linked lists, implementing a stack using linked lists, and implementing a queue using linked lists. Code snippets are provided for each assignment along with sample outputs.

Uploaded by

Gourav Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views26 pages

Lab Assignment - V (Bcac393)

The document contains 3 programming assignments for a Data Structures and Algorithms lab course. The assignments involve implementing various operations on singly linked lists, implementing a stack using linked lists, and implementing a queue using linked lists. Code snippets are provided for each assignment along with sample outputs.

Uploaded by

Gourav Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Calcutta Institute of Engineering and

Management
24/1A, Chandi Ghosh Road, Tollygunge, Kolkata-
700040

Stream: BCA Year: 2nd Semester:3rd

Paper Name: Data Structure & Algorithm Lab


Paper Code: BCAC393

Name GOURAV JAIN


University Roll 29901220025
Number
Data Structure & Algorithm Lab (BCAC393)
2021
ROLL 29901220025
NAME GOURAV JAIN
Learning outcome: Students will be able to perform different operations on Linked
List.

ASSIGNMENT: V

Sl. No Program Listing


1 Write a menu driven program to perform various operations of singly linked list..
2 Write a program to implement stack using linked list.

3 Write a program to implement queue using linked list.


PROGRAM -1
Write a menu driven program to perform various operations of singly linked
list.
Code-
#include <stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* link;
};
struct node* start = NULL;

void traverse()
{
struct node* temp;

if (start == NULL)
printf("\nList is empty\n");

else {
temp = start;
while (temp != NULL)
{
printf("Data = %d\n",temp->info);
temp = temp->link;
}
}
}
void insertAtFront()
{
int data;
struct node *temp;
temp = (node*)malloc(sizeof(struct node));
printf("\nEnter number to be inserted : ");
scanf("%d", &data);
temp->info = data;

temp->link = start;
start = temp;
}
void insertAtEnd()
{
int data;
struct node *temp, *head;
temp =(node*) malloc(sizeof(struct node));

printf("\nEnter number to be inserted : ");


scanf("%d", &data);

temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL)
{
head = head->link;
}
head->link = temp;
}
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = (node*)malloc(sizeof(struct node));
printf("\nEnter position and data :");
scanf("%d %d", &pos, &data);

temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1)
{
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
start = start->link;
free(temp);
}
}
void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
while (temp->link != 0)
{
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;

if (start == NULL)
printf("\nList is empty\n");

else {
printf("\nEnter index : ");

scanf("%d", &pos);
position = (node*)malloc(sizeof(struct node));
temp = start;

while (i < pos - 1)


{
temp = temp->link;
i++;
}

position = temp->link;
temp->link = position->link;
free(position);
}
}
void sort()
{
struct node* current = start;
struct node* index = NULL;
int temp;

if (start == NULL)
{
return;
}

else {

while (current != NULL)


{
index = current->link;

while (index != NULL)


{

if (current->info > index->info)


{
temp = current->info;
current->info = index->info;
index->info = temp;
}
index = index->link;
}

current = current->link;
}
}
}

int main()
{
int choice;
while (1)
{

printf("\n\t1 To see list\n");


printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To sort element\n");
printf("\t9 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

switch (choice)
{
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
sort();
break;
case 9:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
Output-
PROGRAM- 2
Write a program to implement stack using linked list.
Code-
#include <stdio.h>
#include <stdlib.h>

struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

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

printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");

create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
return 0;
}

void create()
{
top = NULL;
}

void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}

void push(int data)


{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}

void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)


{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}

void pop()
{
top1 = top;

if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->info);
}

void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}

void destroy()
{
top1 = top;

while (top1 != NULL)


{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf("\n All stack elements destroyed");


count = 0;
}
Output-
PROGRAM- 3
Write a program to implement queue using linked list.
Code-
#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;

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

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

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

void queuesize()
{
printf("\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
{
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))


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

void deq()
{
front1 = front;

if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\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))
printf("\n Queue empty");
else
printf("Queue not empty");
}
Output-

You might also like