0% found this document useful (0 votes)
3 views8 pages

Practical Exam Program

The document contains implementations of various data structures and algorithms in C, including stacks and queues using both linked lists and arrays, as well as a linear search algorithm. Each implementation includes functions for pushing, popping, enqueuing, dequeuing, and displaying elements, along with a simple user interface for interaction. The document also notes that some implementations can be reused due to their similarity.

Uploaded by

anushricoding
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)
3 views8 pages

Practical Exam Program

The document contains implementations of various data structures and algorithms in C, including stacks and queues using both linked lists and arrays, as well as a linear search algorithm. Each implementation includes functions for pushing, popping, enqueuing, dequeuing, and displaying elements, along with a simple user interface for interaction. The document also notes that some implementations can be reused due to their similarity.

Uploaded by

anushricoding
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

Exam program

1. Stack using Linked List (Push, Pop, Display)


#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node *next;
} Node;

Node *top = NULL;

void push(int value) {


Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed onto stack.\n", value);
}

void pop() {
if (!top) {
printf("Stack Underflow!\n");
return;
}
Node *temp = top;
printf("%d popped from stack.\n", temp->data);
top = top->next;
free(temp);
}

void display() {
if (!top) {
printf("Stack is empty.\n");
return;
}
Node *temp = top;
printf("Stack elements:\n");
while (temp) {
printf("%d\n", temp->data);
temp = temp->next;
}
}

int main() {
int choice, value;
while (1) {
printf("\[Link] [Link] [Link] [Link]\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Enter value: "); scanf("%d",&value); push(value); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

2. Stack using Array


#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX], top = -1;

void push(int value) {


if(top >= MAX-1) { printf("Stack Overflow!\n"); return; }
stack[++top] = value;
printf("%d pushed onto stack.\n", value);
}

void pop() {
if(top < 0) { printf("Stack Underflow!\n"); return; }
printf("%d popped from stack.\n", stack[top--]);
}

void display() {
if(top < 0) { printf("Stack is empty.\n"); return; }
printf("Stack elements:\n");
for(int i=top;i>=0;i--) printf("%d\n", stack[i]);
}

int main() {
int choice,value;
while(1) {
printf("\[Link] [Link] [Link] [Link]\nEnter choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: printf("Enter value: "); scanf("%d",&value); push(value); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

3. Queue using Array


#include <stdio.h>
#include <stdlib.h>
#define MAX 100

int queue[MAX], front=-1, rear=-1;

void enqueue(int value) {


if(rear>=MAX-1) { printf("Queue Overflow!\n"); return; }
if(front==-1) front=0;
queue[++rear]=value;
printf("%d enqueued.\n",value);
}

void dequeue() {
if(front==-1 || front>rear) { printf("Queue Underflow!\n"); return; }
printf("%d dequeued.\n", queue[front++]);
if(front>rear) front=rear=-1;
}

void display() {
if(front==-1) { printf("Queue empty.\n"); return; }
printf("Queue elements:\n");
for(int i=front;i<=rear;i++) printf("%d\n",queue[i]);
}

int main() {
int choice,value;
while(1) {
printf("\[Link] [Link] [Link] [Link]\nEnter choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: printf("Enter value: "); scanf("%d",&value); enqueue(value); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

5. Stack using Array (Push, Pop, Display)


This is essentially same as #2, so you can reuse the program above.

6. Queue using Linked List


#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node *next;
} Node;

Node *front=NULL,*rear=NULL;

void enqueue(int value) {


Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data=value; newNode->next=NULL;
if(!rear) { front=rear=newNode; }
else { rear->next=newNode; rear=newNode; }
printf("%d enqueued.\n",value);
}

void dequeue() {
if(!front) { printf("Queue Underflow!\n"); return; }
Node *temp=front;
printf("%d dequeued.\n",temp->data);
front=front->next;
if(!front) rear=NULL;
free(temp);
}

void display() {
if(!front) { printf("Queue empty.\n"); return; }
Node *temp=front;
printf("Queue elements:\n");
while(temp) { printf("%d\n",temp->data); temp=temp->next; }
}

int main() {
int choice,value;
while(1) {
printf("\[Link] [Link] [Link] [Link]\nEnter choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: printf("Enter value: "); scanf("%d",&value); enqueue(value); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

7. Stack using Array


Same as #2 or #5, you can reuse that code.

9. Linear Search
#include <stdio.h>

int linearSearch(int arr[], int n, int key) {


for(int i = 0; i < n; i++) {
if(arr[i] == key) {
printf("Key found at index %d\n", i);
return i; // return index if found
}
}
printf("Key not found in the array.\n");
return -1; // return -1 if not found
}

int main() {
int n, key;
printf("Enter length of array (n) = ");
scanf("%d", &n);

int arr[100]; // maximum size 100


printf("Enter %d elements: ", n);
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter key element that you want to find in array (key) = ");
scanf("%d", &key);

linearSearch(arr, n, key);

return 0;
}
10. Linear Queue
This is essentially same as #3, so you can reuse the linear queue array code.

You might also like