Turbo C Code Solutions Fixed
Turbo C Code Solutions Fixed
4 Marks:
1. Write a program to implement a stack with push, pop, and display operations.
Code:
#include <stdio.h>
#include <conio.h>
int stack[MAX];
if (top == MAX - 1) {
printf("Stack overflow\n");
} else {
stack[++top] = value;
int pop() {
if (top == -1) {
printf("Stack underflow\n");
return -1;
} else {
return stack[top--];
void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
int i;
printf("%d\n", stack[i]);
void main() {
clrscr();
push(10);
push(20);
display();
pop();
display();
getch();
6 Marks:
1. Write a menu-driven 'C' program to implement stack using array with the following menu: push, pop, display, exit.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int stack[MAX];
int value;
if (top == MAX - 1) {
printf("Stack overflow\n");
} else {
scanf("%d", &value);
stack[++top] = value;
void pop() {
if (top == -1) {
printf("Stack underflow\n");
} else {
void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
int i;
printf("%d\n", stack[i]);
}
void main() {
int choice;
clrscr();
while (1) {
printf("1) Push\n");
printf("2) Pop\n");
printf("3) Display\n");
printf("4) Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting program...\n");
exit(0);
default:
getch();
Code:
#include <stdio.h>
#include <conio.h>
int queue[MAX];
if (rear == MAX - 1) {
printf("Queue overflow\n");
} else {
queue[++rear] = value;
void main() {
clrscr();
insert(10);
insert(20);
getch();
6 Marks:
1. Implement a 'C' program to insert an element into the queue and delete an element from the queue.
Code:
#include <stdio.h>
#include <conio.h>
int queue[MAX];
if (rear == MAX - 1) {
printf("Queue overflow\n");
} else {
queue[++rear] = value;
void delete() {
printf("Queue underflow\n");
} else {
void main() {
clrscr();
insert(10);
insert(20);
delete();
getch();
}
4 Marks:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct Node {
int data;
};
current = current->next;
printf("NULL\n");
void main() {
head->data = 10;
head->next->data = 20;
head->next->next = NULL;
clrscr();
traverse(head);
getch();
6 Marks:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->next = head;
head = newNode;
}
void insertEnd(int value) {
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp->next = newNode;
void deleteBeginning() {
if (head == NULL) {
printf("List is empty\n");
} else {
head = head->next;
free(temp);
void deleteEnd() {
if (head == NULL) {
printf("List is empty\n");
free(head);
head = NULL;
} else {
free(temp->next);
temp->next = NULL;
void display() {
temp = temp->next;
printf("NULL\n");
void main() {
clrscr();
insertBeginning(10);
insertEnd(20);
deleteBeginning();
deleteEnd();
display();
getch();