/*
* C Program to Implement a Stack using Linked List
*/
#include <stdio.h>
#include <stdlib.h>
struct node
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
1
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
2
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
e = topelement();
printf("\n Top element : %d", e);
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
3
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
/* Create empty stack */
void create()
top = NULL;
/* Count stack elements */
void stack_count()
printf("\n No. of elements in stack : %d", count);
/* Push data into stack */
void push(int data)
if (top == NULL)
4
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
else
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
count++;
/* Display stack elements */
void display()
top1 = top;
if (top1 == NULL)
printf("Stack is empty");
return;
5
while (top1 != NULL)
printf("%d ", top1->info);
top1 = top1->ptr;
/* Pop Operation on stack */
void pop()
top1 = top;
if (top1 == NULL)
printf("\n Error : Trying to pop from empty stack");
return;
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
6
/* Return top element */
int topelement()
return(top->info);
/* Check if stack is empty or not */
void empty()
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
/* Destroy entire stack */
void destroy()
top1 = top;
while (top1 != NULL)
top1 = top->ptr;
free(top);
top = top1;
7
top1 = top1->ptr;
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count = 0;
8
/*
* C Program to Implement Queue Data Structure using Linked List
*/
#include <stdio.h>
#include <stdlib.h>
struct node
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
int no, ch, e;
9
printf("\n 1 - Enque");
printf("\n 2 - Dequeue");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
10
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
/* Create an empty queue */
void create()
11
{
front = rear = NULL;
/* Returns queue size */
void queuesize()
printf("\n Queue size : %d", count);
/* Enqueing the queue */
void enq(int data)
if (rear == NULL)
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
else
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
12
temp->ptr = NULL;
rear = temp;
count++;
/* Displaying the queue elements */
void display()
front1 = front;
if ((front1 == NULL) && (rear == NULL))
printf("Queue is empty");
return;
while (front1 != rear)
printf("%d ", front1->info);
front1 = front1->ptr;
if (front1 == rear)
printf("%d", front1->info);
13
/* Dequeing the queue */
void deq()
front1 = front;
if (front1 == NULL)
printf("\n Error: Trying to display elements from empty queue");
return;
else
if (front1->ptr != NULL)
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
else
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
14
}
count--;
/* Returns the front element of queue */
int frontelement()
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
/* Display if queue is empty or not */
void empty()
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
15
/*
* C program to create a linked list and display the elements in the list
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
struct node
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
int count = 0;
int choice = 1;
first = 0;
while (choice)
head = (NODE *) malloc (sizeof(NODE));
printf("Enter the data item\n");
16
scanf("%d", &head-> num);
if (first != 0)
temp->ptr = head;
temp = head;
else
first = temp = head;
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?\n");
scanf("%d", &choice);
temp->ptr = 0;
/* reset temp to the beginning */
temp = first;
printf("\n status of the linked list is\n");
while (temp != 0)
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
17
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
18