You are on page 1of 12

Pseudo - Code:

Pseudo - Code: 1
Pseudo - Code: 2
DSA LAB FAT

Name – Braham Aggarwal Date – 24-06-2021

Reg. No. - 20BCT0052

DSA LAB L49 + L50

Question 1:

1. A) Write a program in C to implement a Singly Linked List with the following


operations: Insert Node at beginning insert Node at End Delete Node at beginning delete
Node at End Display the Nodes Data Count the Nodes.

Code:

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

struct node
{
int data;
struct node *next;
};

void linked_list_traversal(struct node *ptr)


{
printf("Linked list traversal\n");
while (ptr != NULL)
{
printf("%d --> ", (*ptr).data);
ptr = (*ptr).next;
}
printf("NULL\n");
}

struct node *insert_at_beginning(struct node *ptr, struct node *head)


{
printf("\nInserting at beginning...\n");
// here head changes and it is returned too;
(*ptr).next = head;
head = ptr;
return head;
}
void insert_at_end(struct node *head, struct node *ptr)
{
printf("\nInserting at the end...\n");
while ((*head).next != NULL)
{
head = (*head).next;
}
(*head).next = ptr;
(*ptr).next = NULL;
}

struct node *delete_at_beginning(struct node *head)


{
printf("\nDeleting node from beginning...\n");
struct node *temp = head;
head = (*head).next;
free(temp); // to free the previous head
return head;
}

void delete_last_node(struct node *head)


{
printf("\nDeleting last node\n");
while ((*(*head).next).next != NULL)
{
head = (*head).next;
}
struct node *temp = (*head).next;
(*head).next = NULL;
free(temp);
}

void data_count(struct node *ptr)


{
int count = 0;
while (ptr != NULL)
{
ptr = (*ptr).next;
count++;
}
printf("Data count = %d\n", count);
}

int main()
{
struct node *head, *second, *third, *fourth;
//Allocate memory for the nodes in heap memory
head = (struct node *)malloc(1 * sizeof(struct node));
second = (struct node *)malloc(1 * sizeof(struct node));
third = (struct node *)malloc(1 * sizeof(struct node));
fourth = (struct node *)malloc(1 * sizeof(struct node));
//size of struct node is 8

//link first and second nodes


(*head).data = 7;
(*head).next = second;

//Link second and third nodes


(*second).data = 5;
(*second).next = third;

//Link third and fourth nodes


(*third).data = 8;
(*third).next = fourth;

//Terminating fourth node


(*fourth).data = 11;
(*fourth).next = NULL;

linked_list_traversal(head);

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


(*beginning).data = 6;
head = insert_at_beginning(beginning, head); //New head returned
linked_list_traversal(head);

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


(*end).data = 3;
insert_at_end(head, end);
linked_list_traversal(head);

head = delete_at_beginning(head);
linked_list_traversal(head);

delete_last_node(head);
linked_list_traversal(head);

data_count(head);

return 0;
}

Result
CPU Time: 0.00 sec(s), Memory: 1372 kilobyte(s)
Output:

Linked list traversal

7 --> 5 --> 8 --> 11 --> NULL

Inserting at beginning...

Linked list traversal

6 --> 7 --> 5 --> 8 --> 11 --> NULL

Inserting at the end...

Linked list traversal

6 --> 7 --> 5 --> 8 --> 11 --> 3 --> NULL

Deleting node from beginning...

Linked list traversal

7 --> 5 --> 8 --> 11 --> 3 --> NULL

Deleting last node

Linked list traversal

7 --> 5 --> 8 --> 11 --> NULL

Data count = 4
1 B) Write a program in C to implement circular queue using array.

Code:

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

struct queue
{
int size;
int f;
int r;
int *arr;
};

void enqueue(struct queue *q, int val)


{
if (((*q).r + 1) % (*q).size == (*q).f)
{
printf("Queue Overflow\n");
}
else
{
(*q).r = ((*q).r + 1) % (*q).size;
printf("Enqueueing %d\n", val);
(*q).arr[(*q).r] = val;
}
}

void dequeue(struct queue *q)


{
if ((*q).f == (*q).r)
{
printf("Queue Empty\n");
}
else
{
printf("Dequeueing %d\n", (*q).arr[((*q).f + 1) % (*q).size]);
(*q).f = ((*q).f + 1) % (*q).size;
}
}

void circ_queue_trv(struct queue *q)


{
printf("Traversal:\n");
for (int i = ((*q).f + 1) % (*q).size; i != (*q).r; i = (i + 1) % (*q).size
)
{
printf("%d ", (*q).arr[i]);
}
printf("%d\n", (*q).arr[(*q).r]);
}
int main()
{
struct queue *q = (struct queue *)malloc(sizeof(struct queue));
(*q).size = 10;
(*q).f = (*q).r = (*q).size - 1;
(*q).arr = (int *)malloc((*q).size * sizeof(int));
enqueue(q, 62);
enqueue(q, 43);
enqueue(q, 92);
enqueue(q, 57);
enqueue(q, 25);
enqueue(q, 7);
circ_queue_trv(q);

dequeue(q);
dequeue(q);
circ_queue_trv(q);

enqueue(q, 15);
enqueue(q, 87);
circ_queue_trv(q);

return 0;
}

Result

CPU Time: 0.00 sec(s), Memory: 1444 kilobyte(s)

Output:

Enqueueing 62
Enqueueing 43
Enqueueing 92
Enqueueing 57
Enqueueing 25
Enqueueing 7
Traversal:
62 43 92 57 25 7
Dequeueing 62
Dequeueing 43
Traversal:
92 57 25 7
Enqueueing 15
Enqueueing 87
Traversal:
92 57 25 7 15 87

You might also like