~~~STACK~~~
1] Static implementation of stack:-
#include <stdio.h>
#include <stdlib.h>
//static stack~#define size 4
int stack[size];
int top = -1;
void push()
{
int x;
printf("\npls enter data item:");
scanf("%d", &x);
if (top == size - 1)
{
printf("/n ohhh..stack is overflow");
}
else
{
top++;
stack[top] = x;
printf("\nindex of top is:%d",top);
printf("\n u enter item %d", x);
}
}
void pop()
{
int item;
if (top == -1)
{
printf("\n ohh..stack is underflow");
}
else
{
printf("\nindex of top is:%d",top);
item = stack[top];
top--;
printf("\nnow u deleted item is :%d", item);
}
}
void peek()
{
if (top == -1)
{
printf("\n ohh...stack is empty");
}
else
{
printf("\nThe top element is:%d", stack[top]);
}
}
void display()
{
int i;
for (i = top; i >= 0; i--)
{
printf("\n%d", stack[i]);
}
}
void main()
{
int ch;
printf("\nur menucard~:");
printf("\n1:push~");
printf("\n2:pop~");
printf("\n3:peek~");
printf("\n4:Display~");
do
{
printf("\nheyy u pls enter ur journey:");
scanf("%d", &ch);
switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
printf("\n u enter invalid Choice!!!");
}
} while (ch != 0);
}
Or
#include <stdio.h>
#define MAXSIZE 5
typedef struct
{
int data[MAXSIZE];
int top;
} STACK;
void initstack(STACK *ps)
{
ps->top = -1;
}
void push(STACK *ps, int num)
{
ps->data[++ps->top] = num;
}
int pop(STACK *ps)
{
return ps->data[ps->top--];
}
int isempty(STACK *ps)
{
return (ps->top == -1);
}
int isfull(STACK *ps)
{
return (ps->top == MAXSIZE - 1);
}
void main()
{
int n, choice;
STACK s1;
initstack(&s1);
do
{
printf("\n1:PUSH");
printf("\n2:POP");
printf("\n3:EXIT");
printf("\nEnter your choice:");
scanf("%d", &choice);
switch (choice)
{
case 1: /*PUSH*/
if (isfull(&s1))
printf("\n Stack Overflow");
else
{
printf("\n Enter the element to be pushed");
scanf("%d", &n);
push(&s1, n);
}
break;
case 2: /*POP*/
if (isempty(&s1))
printf("\n Stack Underflow");
else
printf("The popped element is:%d", pop(&s1));
break;
}
} while (choice != 3);
}
2] Dynamic implementation of stack:-
#include<stdio.h>
#include<stdlib.h>
//Dynamic stack~
typedef struct node
{
int data;
struct node *next;
}STACK;
STACK * top=NULL;
int push(int num)
{
STACK *newnode;
newnode=(STACK *)malloc(sizeof(STACK));
newnode->data=num;
newnode->next=top;
top=newnode;
}
int display()
{
STACK *temp;
temp=top;
if(top==NULL)
printf("\nstack is empty!!!");
else
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
int peek()
{
if(top==NULL)
printf("\nstack is empty!!!");
else
printf("\n The topmost element is :%d",top->data);
}
int pop()
{
STACK *temp=top;
if(top==NULL)
printf("\nstack is empty!!");
else
{
printf("\npop element:%d",top->data);
top=top->next;
free(temp);
}
}
int main ()
{
int ch,num;
do
{
printf("\n1:push\n2:display\n3:peek\n4:pop:");
printf("\nenter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nenter the data:");
scanf("%d",&num);
push(num);
break;
case 2:printf("\nDISPLAY\n");
display();
break;
case 3:peek();
break;
case 4:pop();
break;
default:
printf("\nInvalid choice");
}
}while(ch!=0);
}
Or
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node *next;
} NODE;
NODE *top;
void initstack()
{
top = NULL;
}
int isempty()
{
return (top == NULL);
}
void push(int num)
{
NODE *newnode;
newnode = (NODE *)malloc(sizeof(NODE));
newnode->info = num;
newnode->next = top;
top = newnode;
}
int pop()
{
int num;
NODE *temp = top;
num = top->info;
top = top->next;
free(temp);
return num;
}
int main()
{
int n, choice;
initstack();
do
{
printf("\n1:Push\n2:Pop\n3:Exit");
printf("\nEnter your choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\n Enter the element:");
scanf("%d", &n);
push(n);
break;
case 2:
if (isempty())
printf("\n Stack is empty");
else
printf("\n pop element:%d", pop());
break;
default:
printf("\nInvalid choice");
}
} while (choice != 3);
}
~~~QUEUE~~~
1] Static implementation of queue:-
#include <stdio.h>
#include <stdlib.h>
//~static queue~
#define size 5
int queue[size];
int front = -1;
int end = -1;
int enqueue(int num) //enqueue
{
if (end == size - 1)
printf("\nQueue is overflow");
else if ((front == -1) && (end == -1))
{
front = end = 0;
queue[end] = num;
}
else
{
end++;
queue[end] = num;
}
}
int dequeue() //dequeue
{
if (front == -1 && end == -1)
printf("\n queue is empty");
else if (front == end)
{
printf("\n pop element:%d", queue[front]);
front = end = -1;
}
else
{
printf("\n pop element:%d", queue[front]);
front++;
}
}
int peek()
{
if (front == -1 && end == -1)
printf("\n queue is empty");
else
printf("\nfront element is:%d", queue[front]);
}
int display()
{
int top = front;
if (front == -1 && end == -1)
printf("\n queue is empty");
else
while (top < end + 1)
{
printf("\n %d", queue[top]);
top++;
}
}
int main()
{
int num, choice;
do
{
printf("\n1:ADD\n2:DELETE\n3:PEEK\n4:DISPLAY");
printf("\n Enter ur choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\n Enter the element to be added:");
scanf("%d", &num);
enqueue(num);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
printf("\n invalid choice");
}
} while (choice != 5);
}
or
#include <stdio.h>
#define MAXSIZE 5
typedef struct
{
int data[MAXSIZE];
int front, rear;
} QUEUE;
void initq(QUEUE *pq)
{
pq->front = pq->rear = -1;
}
void addq(QUEUE *pq, int num)
{
pq->rear++;
pq->data[pq->rear] = num;
}
int removeq(QUEUE *pq)
{
int num;
pq->front++;
num = pq->data[pq->front];
return (num);
}
int peek(QUEUE *pq)
{
return pq->data[pq->front + 1];
}
int isempty(QUEUE *pq)
{
return (pq->front == pq->rear);
}
int isfull(QUEUE *pq)
{
return (pq->rear == MAXSIZE - 1);
}
void main()
{
int n, choice;
QUEUE q;
initq(&q);
do
{
printf("\n1:ADD\n2:DELETE\n3:PEEK\n4:EXIT");
printf("\n Enter ur choice:");
scanf("%d", &choice);
switch (choice)
{
case 1: /*ADD*/
printf("\n Enter the element to be added:");
scanf("%d", &n);
if (isfull(&q))
printf("Queue Overflow");
else
addq(&q, n);
break;
case 2: /*REMOVE*/
if (isempty(&q))
printf("\nQueue is empty\n");
else
printf("The removed element is %d", removeq(&q));
break;
case 3: /*PEEK*/
if (isempty(&q))
printf("\nQueue is empty\n");
else
printf("The element at front is %d", peek(&q));
break;
}
} while (choice != 4);
}
2] Dynamic implementation of queue:-
#include <stdio.h>
#include <stdlib.h>
//Dynamic implementation of queue
typedef struct node
{
int data;
struct node *next;
} QUEUE;
QUEUE *front = NULL;
QUEUE *end = NULL;
int enqueue(int num) // enqueue
{
QUEUE *newnode;
newnode = (QUEUE *)malloc(sizeof(QUEUE));
newnode->data = num;
newnode->next = NULL;
if (front == 0 && end == 0)
{
front = end = newnode;
}
else
{
end->next = newnode;
end = newnode;
}
}
void display()
{
QUEUE *temp;
temp = front;
if (front == NULL && end == NULL)
{
printf("ohh..ur queue is empty");
}
else
{
while (temp != NULL)
{
printf("%d\n", temp->data);
temp = temp->next;
}
}
}
int dequeue()
{
QUEUE *temp;
temp = front;
if ((front == NULL) && (end == NULL))
{
printf("ohh..ur queue is empty");
}
else
{
printf("\n pop element is:%d", front->data);
front = front->next;
free(temp);
}
}
int peek()
{
if ((front == NULL) && (end == NULL))
{
printf("ohh..ur queue is empty");
}
else
printf("\n topmost element is :%d", front->data);
}
int main()
{
int choice, num;
do
{
printf("\n1:Add\n2:Display\n3:Delete\n4:peek");
printf("\nHey pls enter ur journey:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the element:");
scanf("%d", &num);
enqueue(num);
break;
case 2:
printf("\nDISPLAY:\n");
display();
break;
case 3:
dequeue();
break;
case 4:
peek();
break;
default:
printf("\ninvalid Choice");
}
} while (choice != 5);
}
or
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node *next;
} NODE;
NODE *front, *rear;
void initq()
{
front = rear = NULL;
}
int isempty()
{
return (front == NULL);
}
void addq(int num)
{
NODE *newnode;
newnode = (NODE *)malloc(sizeof(NODE));
newnode->info = num;
newnode->next = NULL;
if (front == NULL)
rear = front = newnode;
else
{
rear->next = newnode;
rear = newnode;
}
}
int removeq()
{
int num;
NODE *temp = front;
num = front->info;
front = front->next;
free(temp);
if (front == NULL)
rear = NULL;
return (num);
}
int peek()
{
return (front->info);
}
void main()
{
int choice, num;
initq();
do
{
printf("\n\n1:Add\n2:Delete\n3:Peek\n4:Exit");
printf("\nEnter ur choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the element:");
scanf("%d", &num);
addq(num);
break;
case 2:
if (isempty())
printf("\nQueue underflow");
else
printf("\nThe removed element is %d", removeq());
break;
case 3:
if (isempty())
printf("\nQueue underflow");
else
printf("\nThe front element is %d", peek());
break;
}
} while (choice != 4);
}
Comments
Post a Comment
hey