0% found this document useful (0 votes)
43 views

Dsa File

The document contains code implementations and outputs for various data structures and algorithms topics like stacks, queues, linked lists, trees, and sorting. Specifically, it includes: 1. Array and linked list implementations of stacks with push, pop, peek functions and examples. 2. Array and linked list implementations of queues with enqueue, dequeue functions and examples. 3. A program to convert infix expressions to postfix using a stack. 4. Code for single linked list insertion at beginning and after a node. The document appears to be notes or assignments submitted by a student on various fundamental DS and algorithms concepts.

Uploaded by

Pragyaditya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Dsa File

The document contains code implementations and outputs for various data structures and algorithms topics like stacks, queues, linked lists, trees, and sorting. Specifically, it includes: 1. Array and linked list implementations of stacks with push, pop, peek functions and examples. 2. Array and linked list implementations of queues with enqueue, dequeue functions and examples. 3. A program to convert infix expressions to postfix using a stack. 4. Code for single linked list insertion at beginning and after a node. The document appears to be notes or assignments submitted by a student on various fundamental DS and algorithms concepts.

Uploaded by

Pragyaditya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

DATA STRUCTURES AND

ALGORITHMS
(EACPC03)

ECAM-02

SUBMITTED TO: MR PRAKASH RAO

SUBMITTED BY: PRAGYADITYA JHA

ROLL NUMBER:2020UEA6578
INDEX:
S.NO
1 STACK USING ARRAY
2 QUEUE USING ARRAY
3 INFIX TO POSTFIX
CONVERSION
4 STACK USING LINKED
LIST
5 QUEUE USING LINKED
LIST
6 SINGLE LINKED LIST
IMPLIMENTATION
7 DOUBLE LINKED LIST
IMPLEMENTATION
8 SEARCHING AN
ELEMENT IN ARRAY
9 TREE TRAVERSALS
10 BINARY SEARCH TREE
IMPLEMENTATION
11 DFS IN GRAPH
12 QUICKSORT

1-ARRAY IMPLEMENTATION OF
STACK
CODE:
#include<stdio.h>

int stack[100],top=-1,size;

void push(int element)


{
if(top==size)
{
printf("Overflow conditiion:");
return;
}
top++;
stack[top]=element;
}
void pop()
{
if(top==-1)
{
printf("Underflow condition:\n");
return;
}
top--;
}
int getTop()
{
if(top==-1)
{
printf("Underflow condition:\n");
return -1;
}
int ans=stack[top];
return ans;
}

int getSize()
{
return (top+1);
}
void printStack()
{
for(int i=top;i>=0;i--)
{
printf("%d,",stack[i]);
}
printf("\n");
}

int main()
{
int choice;
printf("Enter the size of stack:");
scanf("%d",&size);
printf("MENU:\n1-PUSH\n2-POP\n3-TOP\n4-getSize\n5-
PRINT\n6-EXIT\n");
printf("Enter your choice:");
scanf("%d",&choice);
int element;
while(choice!=6)
{
switch(choice)
{
case 1:
printf("Enter the element to pushed:");
scanf("%d",&element);
push(element);
break;
case 2:
pop();
break;
case 3:
printf("topmost element present in the
stack=%d\n",getTop());
break;
case 4:
printf("current size of the stack=%d\n",getSize());
break;
case 5:
printf("printing stack:");
printStack();
break;
default:
printf("Enter a valid choice:\n");
}
printf("Enter your choice:");
scanf("%d",&choice);
}
return 0;
}
OUTPUT:
Enter the size of stack:4
MENU:
1-PUSH
2-POP
3-TOP
4-getSize
5-PRINT
6-EXIT
Enter your choice:1
Enter the element to pushed:5
Enter your choice:1
Enter the element to pushed:4
Enter your choice:1
Enter the element to pushed:3
Enter your choice:1
Enter the element to pushed:2
Enter your choice:2
Enter your choice:3
topmost element present in the stack=3
Enter your choice:4
current size of the stack=3
Enter your choice:
5
printing stack:3,4,5,
Enter your choice:6
2-ARRAY IMPLEMENTATION OF QUEUE
CODE:
#include<stdio.h>
int queue[100],rear=0,front=-1,size;

void enqueue(int element)


{
if(rear==size)
{
printf("Queue is full:\n");
return;
}
if(front=-1)
{
front++;
queue[rear]=element;
rear++;
return;
}
queue[rear]=element;
rear++;
}
int dequeue()
{
if(front==-1)
{
printf("Queue is empty\n");
return -1;
}
int ans=queue[front];
front++;
if(front==rear)
{
front=-1;
rear=-1;
}
return ans;
}
void display()
{
for(int i=front;i<rear;i++)
{
printf("%d ,",queue[i]);
}
printf("\n");
}

int main()
{
printf("Enter the size of queue:");
scanf("%d",&size);
int choice;
printf("MENU\n1-Enqueue\n2-Dequeue\n3-Display\n4-
Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
while(choice!=4)
{
if(choice==1)
{
int element;
printf("Enter the element to be inserted:");
scanf("%d",&element);
enqueue(element);
}
else if(choice==2)
{
if(dequeue()==-1)
{

}
else
{
printf("element dequed is:%d\n",dequeue());
}
}
else if(choice==3)
{
display();
}
else
{
printf("enter a valid choice:\n");
}
printf("Enter your choice:");
scanf("%d",&choice);
}
return 0;
}
OUTPUT:
Enter the size of queue:4
MENU
1-Enqueue
2-Dequeue
3-Display
4-Exit
Enter your choice:1
Enter the element to be inserted:1
Enter your choice:1
Enter the element to be inserted:2
Enter your choice:1
Enter the element to be inserted:3
Enter your choice:3
1 ,2 ,3 ,
Enter your choice:2
element dequed is:2
Enter your choice:4

3-PROGRAM TO CONVERT INFIX


EXPRESSION TO POSTFIX USING STACK
CODE:
#include<stdio.h>
#include<ctype.h>
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:
Enter the expression : (a+b)*c+(d-a)

ab+c*da-+
4-LINKED LIST IMPLEMENTATION OF
STACK
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node next;
};
Node top = NULL;
void push(int value) {
struct Node *newNode;
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
if (top == NULL) {
newNode->next = NULL;
} else {
newNode->next = top; // Make the node as top
}
top = newNode; // top always points to the newly created
node
printf("Node is Inserted\n\n");
}
int pop() {
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
struct Node *temp = top;
int temp_data = top->data;
top = top->next;
free(temp);
return temp_data;
}
}
void display() {
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
printf("The stack is \n");
struct Node *temp = top;
while (temp->next != NULL) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL\n\n", temp->data);
}
}
int main() {
int choice, value;
printf("\nImplementaion of Stack using Linked List\n");
while (1) {
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped element is :%d\n", pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}
OUTPUT:
Implementaion of Stack using Linked List

Push
Pop
Display
Exit
Enter your choice : 1
Enter the value to insert: 12
Node is Inserted

Push
Pop
Display
Exit
Enter your choice : 1
Enter the value to insert: 45
Node is Inserted

Push
Pop
Display
Exit
Enter your choice : 1
Enter the value to insert: 56
Node is Inserted

Push
Pop
Display
Exit
Enter your choice : 3
The stack is
56--->45--->12--->NULL

Pop Operation:
The stack is
56--->45--->12--->NULL
Push
Pop
Display
Exit
Enter your choice : 2
Popped element is :56

Push
Pop
Display
Exit
Enter your choice : 2
Popped element is :45

Push
Pop
Display
Exit
Enter your choice : 3
The stack is
12--->NULL
Push
Pop
Display
Exit
Enter your choice : 2
Popped element is :12

Push
Pop
Display
Exit

5-LINKED LIST IMPLEMENTATION OF


QUEUE
CODE:
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node* next;
};
struct node* front=NULL;
struct node* rear=NULL;

struct node* getNode(int element)


{
struct node* newNode=(struct node*)malloc(sizeof(struct
node));
newNode->data=element;
newNode->next=NULL;
return newNode;
}
void enqueue(int element)
{
struct node*newNode=getNode(element);
if(front==NULL) //queue is empty
{
front=newNode;
rear=newNode;
}
else
{
rear->next=newNode;
rear=rear->next;
}
}
int dequeue()
{
if(front==NULL)
{
printf("queue is empty hence dequeue not possible:\n");
return -1;
}
int ans=front->data;
struct node* temp=front;
front=front->next;
free(temp);

return ans;
}
void display()
{
struct node* temp=front;
while(temp!=rear)
{
printf("%d,",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
printf("MENU\n1-Enqueue\n2-Dequeue\n3-Display\n4-
Exit\n");
int choice;
printf("enter your choice:");
scanf("%d",&choice);
while(choice!=4)
{
if(choice==1)
{
int element;
printf("enter the element to be inserted:");
scanf("%d",&element);
enqueue(element);
}
else if(choice==2)
{
int ans=dequeue();
if(ans==-1)
{

}
else
{
printf("element dequed=%d\n",ans);
}
}
else if(choice==3)
{
display();
}
else
{
printf("Please enter a valid choice\n");
}
printf("enter your choice:");
scanf("%d",&choice);
}
}
OUTPUT:
MENU
1-Enqueue
2-Dequeue
3-Display
4-Exit
enter your choice:1
enter the element to be inserted:1
enter your choice:1
enter the element to be inserted:2
enter your choice:1
enter the element to be inserted:3
enter your choice:1
enter the element to be inserted:4
enter your choice:2
element dequed=1
enter your choice:3
2,3,
enter your choice:4
6-SINGLE LINKEDLIST
IMPLEMENTATION
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head_ref, int
new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct
Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void insertAfter(struct Node* prev_node, int new_data) {
if (prev_node == NULL) {
printf("the given previous node cannot be NULL");
return;
}
struct Node* new_node = (struct Node*)malloc(sizeof(struct
Node));
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void insertAtEnd(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct
Node));
struct Node* last = head_ref; / used in step 5*/
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
while (last->next != NULL) last = last->next;
last->next = new_node;
return;
}
void deleteNode(struct Node** head_ref, int key) {
struct Node *temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
int searchNode(struct Node** head_ref, int key) {
struct Node* current = *head_ref;
while (current != NULL) {
if (current->data == key) return 1;
current = current->next;
}
return 0;
}
void sortLinkedList(struct Node** head_ref) {
struct Node *current = *head_ref, *index = NULL;
int temp;
if (head_ref == NULL) {
return;
} else {
while (current != NULL) {
index = current->next;
while (index != NULL) {
if (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;

}
}
}
void printList(struct Node* node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
insertAfter(head->next, 5);
printf("Linked list: ");
printList(head);
printf("\nAfter deleting an element: ");
deleteNode(&head, 3);
printList(head);
int item_to_find = 3;
if (searchNode(&head, item_to_find)) {
printf("\n%d is found", item_to_find);
} else {
printf("\n%d is not found", item_to_find);
}
sortLinkedList(&head);
printf("\nSorted List: ");
printList(head);
}
OUTPUT:
Linked list: 3 2 5 1 4
After deleting an element: 2 5 1 4
3 is not found
Sorted List: 1 2 4 5
7-DOUBLY LINKED LIST IMPLEMENTATION
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node* prev;
struct node* next;
int data;
};
struct node* getNode(int element)
{
struct node* newNode=(struct node*)malloc(sizeof(struct
node));
newNode->data=element;
newNode->prev=NULL;
newNode->next=NULL;
return newNode;
}
struct node* head=NULL;
struct node* tail=NULL;

void insertion_end(int element)


{
struct node* newNode=getNode(element);
newNode->data=element;
if(head==NULL)
{
head=newNode;
tail=newNode;
}
else
{
tail->next=newNode;
newNode->prev=tail;
tail=tail->next;
}
}
int deletion_end()
{
if(head==NULL)
{
printf("\nUnderflow condition:");
return -1;
}
struct node* temp=tail;
tail=tail->prev;
tail->next=NULL;
int ans=temp->data;
free(temp);
return ans;
}
void insertion_beginning(int element)
{
struct node* newNode=getNode(element);
newNode->data=element;
if(head==NULL)
{
head=newNode;
tail=newNode;
return;
}
newNode->next=head;
head->prev=newNode;
head=newNode;
}
int deletion_beginning()
{
if(head==NULL)
{
printf("\nUnderflow condition:");
return -1;
}
struct node* temp=head;
head=head->next;
head->prev=NULL;
int ans=temp->data;
return ans;
}
int insertion_specific(int index,int element)
{
int insert=0;
struct node* newNode=getNode(element);
newNode->data=element;
int i=0;
struct node* temp=head;
while(temp!=NULL)
{
if(i==index-1)
{
newNode->next=temp->next;
temp->next->prev=newNode;
temp->next=newNode;
newNode->prev=temp;
insert=1;
break;
}
else
{
temp=temp->next;
}
i++;
}
return insert;
}
void display()
{
struct node* temp=head;
while(temp!=NULL)
{
printf("%d,",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
printf("MENU\n1-insertion_end\n2-deletion_end\n3-
insertion_beginning\n4-insertion_specific\n5-display\n6-
Exit\n");
int choice;
printf("enter your choice:");
scanf("%d",&choice);
while(choice!=6)
{
if(choice==1)
{
int element;
printf("enter the element to be inserted:");
scanf("%d",&element);
insertion_end(element);
}
else if(choice==2)
{
deletion_end();
}
else if(choice==3)
{
int element;
printf("enter the element to be inserted:");
scanf("%d",&element);
insertion_beginning(element);
}
else if(choice==4)
{
int element;
printf("enter the element to be inserted:");
scanf("%d",&element);
int index;
printf("enter the index:");
scanf("%d",&index);
int ans=insertion_specific(index,element);
if(ans==0)
{
printf("insertion not possible:\n");
}
else
{
printf("insertion done\n:");
}
}
else if(choice==5)
{
display();
}
else
{
printf("Please enter a valid choice:\n");
}
printf("Enter your choice:");
scanf("%d",&choice);
}
}
OUTPUT:
MENU
1-insertion_end
2-deletion_end
3-insertion_beginning
4-insertion_specific
5-display
6-Exit
enter your choice:3
enter the element to be inserted:1
Enter your choice:3
enter the element to be inserted:2
Enter your choice:3
enter the element to be inserted:3
Enter your choice:4
enter the element to be inserted:5
enter the index:2
insertion done
:Enter your choice:5
3,2,5,1,
Enter your choice:6
8-PROGRAM FOR SEARCHING AN ELEMENT
IN AN ARRAY
CODE:
#include<stdio.h>
#include<stdlib.h>

int linearSearch(int arr[],int size,int element)


{
for(int i=0;i<size;i++)
{
if(arr[i]==element)
{
return i;
}
}
return -1;
}
int main()
{
int size;
printf("enter the size of array:");
scanf("%d",&size);
int* arr=(int*)malloc(sizeof(int)*size);
printf("enter the array elements:");
for(int i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
int element;
printf("enter the element to be searched in array:");
scanf("%d",&element);
int ans=linearSearch(arr,size,element);
if(ans==-1)
{
printf("element not found\n");
}
else
{
printf("element found at index=%d",ans);
}
}
OUTPUT:
enter the size of array:5
enter the array elements:1 2 3 4 5
enter the element to be searched in array:3
element found at index=2

You might also like