stack using array
#include<stdio.h>
int stack[100],top,i,n,choice,x;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("enter value of STACK [MAX=100]");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("1.Push \n 2.pop \n 3.Display \n 4.Exit");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push(){
if(top>=n-1){
printf("stack is overflow");
}
else{
printf("number to be pushed");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop(){
if(top<=-1){
printf("stack is underflow");
}
else{
printf("the poped element is %d",stack[top]);
top--;
}
}
void display(){
if(top<=0){
for(i=top;i>=0;i--)
printf("the elements in stack are %d",stack[i]);
printf("enter next choice");
}
else{
printf("stack is empty");
}
}
queue using array
#include<stdio.h>
#include<stdlib.h>
struct queue
{
int size;
int f;
int r;
int* arr;
};
int isEmpty(struct queue *q){
if(q->r==q->f){
return 1;
}
return 0;
}
int isFull(struct queue *q){
if(q->r==q->size-1){
return 1;
}
return 0;
}
void enqueue(struct queue *q, int val){
if(isFull(q)){
printf("This Queue is full\n");
}
else{
q->r++;
q->arr[q->r] = val;
printf("Enqued element: %d\n", val);
}
}
int dequeue(struct queue *q){
int a = -1;
if(isEmpty(q)){
printf("This Queue is empty\n");
}
else{
q->f++;
a = q->arr[q->f];
}
return a;
}
int main(){
struct queue q;
q.size = 4;
q.f = q.r = 0;
q.arr = (int*) malloc(q.size*sizeof(int));
// Enqueue few elements
enqueue(&q, 12);
enqueue(&q, 15);
enqueue(&q, 1);
printf("Dequeuing element %d\n", dequeue(&q));
printf("Dequeuing element %d\n", dequeue(&q));
printf("Dequeuing element %d\n", dequeue(&q));
enqueue(&q, 45);
enqueue(&q, 45);
enqueue(&q, 45);
if(isEmpty(&q)){
printf("Queue is empty\n");
}
if(isFull(&q)){
printf("Queue is full\n");
}
return 0;
}
circular Linked list
#include<stdio.h>
#include<stdlib.h>
struct circularQueue
{
int size;
int f;
int r;
int* arr;
};
int isEmpty(struct circularQueue *q){
if(q->r==q->f){
return 1;
}
return 0;
}
int isFull(struct circularQueue *q){
if((q->r+1)%q->size == q->f){
return 1;
}
return 0;
}
void enqueue(struct circularQueue *q, int val){
if(isFull(q)){
printf("This Queue is full");
}
else{
q->r = (q->r +1)%q->size;
q->arr[q->r] = val;
printf("Enqued element: %d\n", val);
}
}
int dequeue(struct circularQueue *q){
int a = -1;
if(isEmpty(q)){
printf("This Queue is empty");
}
else{
q->f = (q->f +1)%q->size;
a = q->arr[q->f];
}
return a;
}
int main(){
struct circularQueue q;
q.size = 4;
q.f = q.r = 0;
q.arr = (int*) malloc(q.size*sizeof(int));
// Enqueue few elements
enqueue(&q, 12);
enqueue(&q, 15);
enqueue(&q, 1);
printf("Dequeuing element %d\n", dequeue(&q));
printf("Dequeuing element %d\n", dequeue(&q));
printf("Dequeuing element %d\n", dequeue(&q));
enqueue(&q, 45);
enqueue(&q, 45);
enqueue(&q, 45);
if(isEmpty(&q)){
printf("Queue is empty\n");
}
if(isFull(&q)){
printf("Queue is full\n");
}
return 0;
}
stack using linked list
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node * next;
};
void tranversal(struct node *ptr)
{
while(ptr!=NULL){
printf("THE elements are %d\n",ptr->data);
ptr=ptr->next;
}
}
int isEmpty(struct node*top){
if(top==NULL){
return 1;
}
return 0;
}
int isFull(struct node* top){
struct node * p =(struct node*)malloc(sizeof(struct node));
if(p==NULL){
return 1;
}
return 0;
}
struct node* push(struct node* top,int x){
if(isFull(top)){
printf("It is full");
}
else{
struct node* n=(struct node*)malloc(sizeof(struct node));
n->data=x;
n->next=top;
top=n;
return top;
}
}
int pop(struct node** top){
if(isEmpty){
printf("it is empty");
}
else{
struct node* n = *top;
*top = (*top)->next;
int x=n->data;
free(n);
return x;
}
}
int main()
{
struct Node* top = NULL;
top = push(top, 78);
top = push(top, 7);
top = push(top, 8);
tranversal(top);
return 0;
}
insertion at end and display
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node * next;
};
void linkedlist(struct Node*ptr)
{
while(ptr != NULL)
{
printf("\n elements:%d",ptr->data);
ptr= ptr->next;
}
}
struct node * insertionatend(struct Node*head,int data){
struct Node *ptr =(struct Node*)malloc(sizeof(struct Node));
ptr->data = data;
struct Node * p = head;
while(p->next!=NULL){
p=p->next;
}
p->next=ptr;
ptr->next=NULL;
return head;
int main()
{
struct Node*head;
struct Node*second;
struct Node*third;
head = (struct Node*)malloc(sizeof(struct Node));
second=(struct Node*)malloc(sizeof(struct Node));
third=(struct Node*)malloc(sizeof(struct Node));
head->data = 7;
head->next = second;
second->data = 11;
second->next = third;
third->data = 66;
third->next= NULL;
printf("before insertion");
linkedlist(head);
//insertatfirst(head,45);
insertionatend(head,45);
printf("after insertion");
linkedlist(head);
return 0;
}