Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Stack Queue Using Linklist

Uploaded by

stdesai1005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Stack Queue Using Linklist

Uploaded by

stdesai1005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Aim: Stack implementation using Linklist

#include <stdio.h>

#include <stdlib.h>

// Define the stack node structure

struct Node {

int data;

struct Node* next;

};

// Function to push an element onto the stack

void push(struct Node** top, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (!newNode) {

printf("Stack overflow\n");

return;

newNode->data = data;

newNode->next = *top;

*top = newNode;

printf("%d pushed to stack\n", data);

// Function to pop an element from the stack

int pop(struct Node** top) {

if (*top == NULL) {

printf("Stack underflow\n");

return -1;
}

struct Node* temp = *top;

*top = (*top)->next;

int popped = temp->data;

free(temp);

return popped;

// Function to display the stack

void display(struct Node* top) {

struct Node* temp = top;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

struct Node* stack = NULL;

push(&stack, 10);

push(&stack, 20);

push(&stack, 30);

display(stack);

printf("Popped element: %d\n", pop(&stack));

display(stack);

return 0;

}
Aim: Stack implementation using Linklist
#include <stdio.h>

#include <stdlib.h>

// Define the structure for a queue node

typedef struct node {

int data;

struct node* next;

} Node;

// Define the structure for the queue

typedef struct {

Node* front;

Node* rear;

} Queue;

// Function to create a new node

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

if (!newNode) {

printf("Memory allocation error\n");

exit(1);

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to initialize a queue


Queue* createQueue() {

Queue* queue = (Queue*)malloc(sizeof(Queue));

if (!queue) {

printf("Memory allocation error\n");

exit(1);

queue->front = queue->rear = NULL;

return queue;

// Function to enqueue an element

void enqueue(Queue* queue, int data) {

Node* newNode = createNode(data);

if (queue->rear == NULL) {

queue->front = queue->rear = newNode;

return;

queue->rear->next = newNode;

queue->rear = newNode;

// Function to dequeue an element

int dequeue(Queue* queue) {

if (queue->front == NULL) {

printf("Queue is empty\n");

return -1;

Node* temp = queue->front;

int data = temp->data;


queue->front = queue->front->next;

if (queue->front == NULL) {

queue->rear = NULL;

free(temp);

return data;

// Function to display the queue

void displayQueue(Queue* queue) {

Node* temp = queue->front;

while (temp) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

Queue* queue = createQueue();

enqueue(queue, 10);

enqueue(queue, 20);

enqueue(queue, 30);

printf("Queue: ");

displayQueue(queue);

printf("Dequeued: %d\n", dequeue(queue));

printf("Queue after dequeue: ");

displayQueue(queue);

return 0;
}

You might also like