0% found this document useful (0 votes)
21 views10 pages

Turbo C Code Solutions Fixed

Codes

Uploaded by

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

Turbo C Code Solutions Fixed

Codes

Uploaded by

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

STACK (Programming Questions):

4 Marks:

1. Write a program to implement a stack with push, pop, and display operations.

Code:

#include <stdio.h>

#include <conio.h>

#define MAX 100

int stack[MAX];

int top = -1;

void push(int value) {

if (top == MAX - 1) {

printf("Stack overflow\n");

} else {

stack[++top] = value;

printf("%d pushed onto stack\n", 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 {

printf("Stack elements are:\n");

int i;

for (i = top; i >= 0; 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>

#define MAX 100

int stack[MAX];

int top = -1;


void push() {

int value;

if (top == MAX - 1) {

printf("Stack overflow\n");

} else {

printf("Enter the value to push: ");

scanf("%d", &value);

stack[++top] = value;

printf("%d pushed onto stack\n", value);

void pop() {

if (top == -1) {

printf("Stack underflow\n");

} else {

printf("%d popped from stack\n", stack[top--]);

void display() {

if (top == -1) {

printf("Stack is empty\n");

} else {

printf("Stack elements are:\n");

int i;

for (i = top; i >= 0; i--) {

printf("%d\n", stack[i]);

}
void main() {

int choice;

clrscr();

while (1) {

printf("\nStack Operations Menu:\n");

printf("1) Push\n");

printf("2) Pop\n");

printf("3) Display\n");

printf("4) Exit\n");

printf("Enter your choice: ");

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:

printf("Invalid choice. Please try again.\n");

getch();

QUEUE (Programming Questions):


4 Marks:

1. Write a program in 'C' to insert an element in a linear queue.

Code:

#include <stdio.h>

#include <conio.h>

#define MAX 100

int queue[MAX];

int front = -1, rear = -1;

void insert(int value) {

if (rear == MAX - 1) {

printf("Queue overflow\n");

} else {

if (front == -1) front = 0;

queue[++rear] = value;

printf("%d inserted into queue\n", 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>

#define MAX 100

int queue[MAX];

int front = -1, rear = -1;

void insert(int value) {

if (rear == MAX - 1) {

printf("Queue overflow\n");

} else {

if (front == -1) front = 0;

queue[++rear] = value;

printf("%d inserted into queue\n", value);

void delete() {

if (front == -1 || front > rear) {

printf("Queue underflow\n");

} else {

printf("%d deleted from queue\n", queue[front++]);

if (front > rear) front = rear = -1;

void main() {

clrscr();

insert(10);

insert(20);

delete();

getch();
}

LINKED LIST (Programming Questions):

4 Marks:

1. Write a program to traverse a linked list.

Code:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void traverse(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

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

current = current->next;

printf("NULL\n");

void main() {

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

head->data = 10;

head->next = (struct Node*)malloc(sizeof(struct Node));

head->next->data = 20;

head->next->next = NULL;
clrscr();

traverse(head);

getch();

6 Marks:

1. Write a 'C' program to implement singly linked list with operations:

- Insert a node at the beginning

- Insert a node at the end

- Delete a node from the beginning

- Delete a node from the end

Code:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

void insertBeginning(int value) {

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

newNode->data = value;

newNode->next = head;

head = newNode;

}
void insertEnd(int value) {

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

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL) temp = temp->next;

temp->next = newNode;

void deleteBeginning() {

if (head == NULL) {

printf("List is empty\n");

} else {

struct Node* temp = head;

head = head->next;

free(temp);

void deleteEnd() {

if (head == NULL) {

printf("List is empty\n");

} else if (head->next == NULL) {

free(head);

head = NULL;

} else {

struct Node* temp = head;

while (temp->next->next != NULL) temp = temp->next;

free(temp->next);
temp->next = NULL;

void display() {

struct Node* temp = head;

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

void main() {

clrscr();

insertBeginning(10);

insertEnd(20);

deleteBeginning();

deleteEnd();

display();

getch();

You might also like