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

Laboratory Report

Course Code: CSE1102L


Advanced Data Structures
SCHOOL OF ENGINEERING AND SCIENCES
DEPARTMENT OF COMPUTER SCIENCES AND ENGINEERING

SUBMITTED BY
Student Name Ankush raj
Enrollment Number 230160223013
Section/Group B.tech-CSE(AIML) sec-A
Department Computer Science & Engineering
Session/Semester 2023-27/EVEN SEMESTER
SUBMITTED TO
Faculty Name Mr. Sunny Saxena
Stack
#include <stdio.h>

int arr_stack[5];
int top = -1;

int push() {
int val;
if (top == 5 - 1) {
printf("\nOverflow\n");
return -1;
}
else {
printf("Enter element for stack: ");
scanf_s("%d", &val);
top = top + 1;
arr_stack[top] = val;
return 0;
}
}

int pop() {
if (top == -1) {
printf("\nUnderflow\n");
return -1;
}
else {
printf("\n%d is popped element\n\n", arr_stack[top]);
top = top - 1;
return 0;
}
}

int display() {
if (top == -1) {
printf("\nStack is empty\n");
return -1;
}
else {
printf("\nStack elements:\n");
for (int i = 0; i <= top; i++) {
printf("%d\n", arr_stack[i]);
}
return 0;
}
}

int main() {
push();
display();
push();
display();
pop();
display();
pop();
display();
return 0;
}
Output:
Queue
#include<stdio.h>

int queue[5];
int rear = -1;
int front = -1;
int value;

void enque()
{
if (rear == 5 - 1)
{
printf("Queue bhra hai");
return;
}
if (front == -1)
{
front = 0;
}
printf("queue me element insert kro:");
scanf_s("%d", &value);
rear = rear++;
queue[rear] = value;
}

void deque()
{
if (front > rear || front == -1)
{
printf("queue khali hai");
}
else if (front > rear)
{
front = -1;
rear = -1;
}
else
{
printf("\n%d(front) ko dequeue kro\n", queue[front]);
front++;
}
}

void display()
{
for (int i = front; i <= rear; i++)
{
printf("%d\n", queue[i]);
}
}

void main()
{
enque();
display();
enque();
display();
enque();
display();
enque();
display();
enque();
display();
deque();
display();
}

Output:

Linked list
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

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

struct Node* head = NULL;


void insert_begin()
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
printf("Enter the data:");
scanf_s("%d", &temp->data);
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
temp->next = head;
head = temp;
}
}

void insert_end()
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
printf("Enter the data:");
scanf_s("%d", &temp->data);
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
struct Node* temp1 = head;
while (temp1->next != NULL)
{
temp1 = temp1->next;
}
temp1->next = temp;
}
}

void delete_begin()
{
if (head == NULL)
{
printf("List is empty | nothing to delete");
}
else
{
struct Node* temp1 = head;
head = head->next;
free(temp1);
}
}
void delete_end()
{
if (head == NULL)
{
printf("List is empty | nothing to delete");
}
else if (head->next == NULL)
{
struct Node* temp1 = head;
head = NULL;
free(temp1);
}
else
{
struct Node* temp1 = head;
struct Node* temp2 = head->next;
while (temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = NULL;
free(temp1);
}
}
void insertAtPosition()
{
int pos;
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
printf("Enter the data:");
scanf_s("%d", &temp->data);
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
struct Node* temp1 = head;
printf("Enter the position:");
scanf_s("%d", &pos);
for (int i = 1; i < pos - 1; i++)
{
temp1 = temp1->next;
}
temp->next = temp1->next;
temp1->next = temp;
}
}

void deleteAtPosition()
{
int pos;
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
printf("Enter the position:");
scanf_s("%d", &pos);
if (head == NULL)
{
printf("List is empty | nothing to delete");
}
else if (pos == 1)
{
delete_begin();
}
else
{
struct Node* temp1 = head;
for (int i = 1; i < pos - 1; i++)
{
temp1 = temp1->next;
}
temp = temp1->next;
temp1->next = temp->next;
free(temp);
}
}

void traverse()
{
struct Node* temp = head;
while (temp != NULL)
{
printf("%d\n", temp->data);
temp = temp->next;
}
}

void main()
{
insert_begin();
traverse();
insert_begin();
traverse();
insert_begin();
traverse();
insert_end();
traverse();
insert_end();
traverse();
insertAtPosition();
traverse();
deleteAtPosition();
traverse();

You might also like