Skip to content
Open

DSA #61

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Data Structures/Graph/DFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ void Graph::DFS(int s){
}

int main(){
Graph g(5);
Graph g(6);
g.addEdge(1, 0);
g.addEdge(0, 2);
g.addEdge(2, 1);
g.addEdge(0, 3);
g.addEdge(1, 4);
g.addEdge(1, 5);

g.DFS(0);
cout<<endl;
return 0;
}
}
110 changes: 110 additions & 0 deletions Data Structures/Queue/Double_Ended_Queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include<stdio.h>

#define max 10
int front = -1, rear = -1;
int que[max];
int is_full() {
if (front == max - 1 && rear == 0 || front == rear - 1) {
printf("Queue is full\n");
return 1;
} else
return 0;

}
void right_enque() {
if (is_full());
else {
if (front == -1) {
front = 0;
rear = 0;
}
if (front == max - 1)
front = 0;
printf("Enter a number\n");
int temp;
scanf("%d", & temp);
que[front++] = temp;
}
}
void left_enque() {
if (is_full());
else {
if (front == -1) {
front = 0;
rear = 0;
}
if (rear == 0)
rear = max - 1;
printf("Enter a number\n");
int temp;
scanf("%d", & temp);
que[rear--] = temp;
}
}
int is_empty() {
if (front == -1 || rear == -1) {
printf("Empty Queue\n");
return 1;
} else
return 0;
}

void right_deque() {
if (is_empty());
else {
printf("Deleted number is %d %d\n", front, que[--front]);

if (front == -1)
front = max - 1;
}
}
void left_deque() {
if (is_empty());
else {
printf("Deleted number is %d %d\n", rear, que[++rear]);
if (rear == max - 1)
rear = 0;
}
}
void display() {
int i;
if (is_empty());
else if (rear <= front) {
printf("Array is\n");
for (i = rear + 1; i <= front; i++)
printf("%d %d\n", i, que[i]);
} else {
printf("Array is\n");
for (i = rear + 1; i < max; i++)
printf("%d %d\n", i, que[i]);
for (i = 0; i < front; i++)
printf("%d %d\n", i, que[i]);

}
}

int main() {
int op;
do {
printf("1)right enqueue\n2)left enqueue\n3)right dequeue\n4)left dequeue\n5)display\n");
scanf("%d", & op);
switch (op) {
case 1:
right_enque();
break;
case 2:
left_enque();
break;
case 3:
right_deque();
break;
case 4:
left_deque();
break;
case 5:
display();
break;
}
} while (op > 0 & op < 6);
return 0;
}
66 changes: 66 additions & 0 deletions Data Structures/Queue/Queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include<stdio.h>
#define max 100
int front=-1,rear=-1;
int que[max];
void enque(){
if(rear==max-1)
printf("Queue overflow\n");
else {
if(front==-1)
front=0;
printf("Enter a number\n");
int temp;
scanf("%d",&temp);
que[++rear]=temp;
}
}
void deque(){
if(front==-1 or front>rear)
printf("Queue underflow");
else{
printf("Deleted number is %d\n",que[front++]);
}
}
void display()
{
if(front==-1&&rear==-1)
printf("Empty array");
else if(front>-1)
for(int i=front ;i<=rear;i++)
printf("%d\n",que[i]);
else
for(int i=0 ;i<=rear;i++)
printf("%d\n",que[i]);
}
void peek(){
printf("%d\n",que[front]);
}
void size(){
printf("%d\n",rear-front+1);
}
int main(){
int op;
do{
printf("1)enqueue\n2)dequeue\n3)display\n4)peek\n5)size\n");
scanf("%d",&op);
switch(op)
{
case 1:
enque();
break;
case 2:
deque();
break;
case 3:
printf("the stack is\n");
display();
break;
case 4:
peek();
break;
case 5:
size();
}
}while(op>0&op<6);
return 0;
}
66 changes: 66 additions & 0 deletions Data Structures/Stack/Stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#define max 1000
int pt[max];
int top=-1;
void push(int x)
{
if(top>max)
printf("stackoverflow");
else
{
pt[++top]=x;
}
}
int pop()
{
if(top==-1)
printf("stackunderflow");
else{
printf("the number popped is");
return pt[top--];
}
}
void display()
{
int i;
for( i=top;i>=0;i--)
printf("%d\n",pt[i]);
}
void peek()
{
printf("%d\n",pt[top]);
}
void size()
{
printf("the size of stack is %d\n",top+1);
}
int main() {
int temp;
int op;
do{
printf("1)push\n2)pop\n3)display\n4)peek\n5)size\n");
scanf("%d",&op);
switch(op)
{
case 1:
printf("enter a number\n");
scanf("%d",&temp);
push(temp);
break;
case 2:
printf(" %d\n",pop());
break;
case 3:
printf("the stack is\n");
display();
break;
case 4:
printf("the top number is\n");
peek();
break;
case 5:
size();
}
}while(op>0&op<6);
return 0;
}
83 changes: 83 additions & 0 deletions Data Structures/Stack/Stack_LinkedList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * next;
};
struct node *head,*ptr;

int top=-1;
void push(int x)
{
ptr=head;
struct node * newnode =(struct node *)malloc(sizeof(struct node *));
newnode->data=x;
newnode->next=ptr;
head=newnode;
top++;

}
int pop()
{
if(top<0)
printf("stackunderflow");
else
{
printf("the number popped is: ");
ptr=head;
head=head->next;
int v= ptr->data;
free(ptr);
top--;
return v;
}

}
void display()
{
ptr=head;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void peek()
{
printf("%d\n",head->data);
}
void size()
{
printf("the size of stack is %d\n",top+1);
}
int main() {

int temp;
int op;
do{
printf("1)push\n2)pop\n3)display\n4)peek\n5)size\n");
scanf("%d",&op);
switch(op)
{
case 1:
printf("enter a number\n");
scanf("%d",&temp);
push(temp);
break;
case 2:
printf(" %d\n",pop());
break;
case 3:
printf("the stack is\n");
display();
break;
case 4:
printf("the top number is\n");
peek();
break;
case 5:
size();
}
}while(op>0&op<6);
return 0;
}
4 changes: 2 additions & 2 deletions Data Structures/Tree/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Node{

int main(){
Node *head = new Node(10);
head->next = new Node(20);
head->next = new Node(30);
cout<<head->data<<"->"<<head->next->data<<endl;
return 0;
}
}