Skip to content
This repository was archived by the owner on Oct 7, 2019. It is now read-only.

queue using [ll and array] and stack using ll #162

Merged
merged 1 commit into from
Oct 10, 2018
Merged
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
76 changes: 76 additions & 0 deletions data_structures/queues/C/queue_using_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

int A[MAX];
int front = -1;
int rear = -1;

void enqueue()
{
if ((rear+1)%MAX == front)
{
printf("Can't Insert! FULL\n");
return;
}

if (front == -1)
{
front++;
}
printf("Enter data: ");
scanf("%d", &A[(rear+1)%MAX]);
}

void dequeue()
{
if (front == -1)
{
printf("Empty!\n");
return;
}
if (front == rear)
{
printf("data: %d\n", A[front]);
front = -1;
rear = -1;
return;
}
printf("data: %d\n", A[(rear+1)%MAX]);
}

void atFront()
{
if (front == -1)
{
printf("Empty!\n");
return;
}
printf("At the top: %d\n", A[front]);
}

int main()
{
int choice;

while(1)
{
printf("1. Enqueue\n2. Dequeue\n3. At front\n4. Exit\n\nEnter Choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
atFront();
break;
default:
exit(0);
}
}

}
78 changes: 78 additions & 0 deletions data_structures/queues/C/queue_using_linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h>

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

struct node* front = NULL, *rear = NULL;

void enqueue()
{
struct node* temp = (struct node* )malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d", &temp->data);
temp->next = NULL;

if (front == NULL)
{
front = rear = temp;
return;
}

rear->next = temp;
rear = temp;
}

void dequeue()
{
if (front == NULL)
{
printf("Empty!\n");
return;
}
struct node* temp = front;
printf("data: %d\n", front->data);
if (front == rear)
front = rear = NULL;
else
front = front->next;
free(temp);
}

void atFront()
{
if (front == NULL)
{
printf("Empty!\n");
return;
}
printf("At the top: %d\n", front->data);
}

int main()
{
int choice;

while(1)
{
printf("1. Enqueue\n2. Dequeue\n3. At front\n4. Exit\n\nEnter Choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
atFront();
break;
default:
exit(0);
}
}

}
78 changes: 78 additions & 0 deletions data_structures/stacks/C/stack_using_linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h>

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

struct node* front = NULL, *rear = NULL;

void enqueue()
{
struct node* temp = (struct node* )malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d", &temp->data);
temp->next = NULL;

if (front == NULL)
{
front = rear = temp;
return;
}

rear->next = temp;
rear = temp;
}

void dequeue()
{
if (front == NULL)
{
printf("Empty!\n");
return;
}
struct node* temp = front;
printf("data: %d\n", front->data);
if (front == rear)
front = rear = NULL;
else
front = front->next;
free(temp);
}

void atFront()
{
if (front == NULL)
{
printf("Empty!\n");
return;
}
printf("At the top: %d\n", front->data);
}

int main()
{
int choice;

while(1)
{
printf("1. Enqueue\n2. Dequeue\n3. At front\n4. Exit\n\nEnter Choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
atFront();
break;
default:
exit(0);
}
}

}