Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Week 3

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 19

WEEK 3

AUTOMATED FIX

1).
ROHITH

void push(int value) {


if (top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
} else {
top++;
stack[top] = value;
printf("Element %d is pushed onto the stack\n", value);
}
}
void pop() {
if (top == -1) {
printf("Stack Underflow\n");
} else {
int popped_value = stack[top];
top--;
printf("Element %d is popped from the stack\n", popped_value);
}
}
void displayStack() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Elements in the stack: ");
for (int i = top; i >= 0; i--) {
printf("%d", stack[i]);
if (i != 0) {
printf(" ");
}
}
printf(" \n");

2).
RAJ

struct Node* createNode(char data)


{
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void push(char value)
{
struct Node* newNode = createNode(value);
if (top == NULL)
{
top = newNode;
}
else
{
newNode->next = top;
top = newNode;
}
printf("Pushed element: %c\n", value);
}
void pop()
{
if (top == NULL)
{
printf("Stack is empty. Cannot pop.\n");
return;
}
printf("Popped element: %c\n", top->data);
struct Node* temp = top;
top = top->next;
free(temp);
}
void displayStack()
{
if (top == NULL)
{
printf("Stack is empty\n");
return;
}
printf("Stack elements (top to bottom): ");
struct Node* temp = top;
while (temp != NULL)
{
printf("%c ", temp->data);
temp = temp->next;
}
printf("\n");
}

LAB EX

1).
SHARON

#include <stdio.h>
#include <stdbool.h>

#define MAX_SIZE 100

char items[MAX_SIZE];
int top = -1;

void initialize() {
top = -1;
}

bool isFull() {
return top == MAX_SIZE - 1;
}

bool isEmpty() {
return top == -1;
}
void push(char value) {
if (isFull()) {
printf("Stack is full. Cannot push %c.\n", value);
return;
}
items[++top] = value;
printf("Pushed: %c\n", value);
}

char pop() {
if (isEmpty()) {
printf("Stack is empty. Nothing to pop.\n");
return '\0'; // return a null character to indicate error
}
char popped_value = items[top--];
printf("Popped: %c\n", popped_value);
return popped_value;
}

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

int main() {
initialize();

int choice;
char value;

while (true) {
scanf("%d", &choice);

switch (choice) {
case 1:
scanf(" %c", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}

return 0;
}

2).
ALICE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100


#define MAX_UNDO 3

// Structure to hold student record


typedef struct {
char name[100];
int age;
float gpa;
} Student;

// Global variables to manage student records and undo history


Student students[MAX_STUDENTS];
int studentCount = 0;

// Stack for undo operations


Student undoStack[MAX_UNDO];
int undoCount = 0;

// Function to add a student record


void addStudent(const char *name, int age, float gpa) {
if (studentCount < MAX_STUDENTS) {
strcpy(students[studentCount].name, name);
students[studentCount].age = age;
students[studentCount].gpa = gpa;

// Push to undo stack


if (undoCount < MAX_UNDO) {
undoStack[undoCount++] = students[studentCount];
} else {
// Shift the stack if it's full
for (int i = 1; i < MAX_UNDO; i++) {
undoStack[i - 1] = undoStack[i];
}
undoStack[MAX_UNDO - 1] = students[studentCount];
}

studentCount++;
}
}

// Function to print current student records


void printStudents() {
if (studentCount == 0) {
printf("Current Student Records:\n");
return;
}
printf("Current Student Records:\n");
for (int i = 0; i < studentCount; i++) {
printf("Name: %s, Age: %d, GPA: %.2f\n", students[i].name, students[i].age,
students[i].gpa);
}
}

// Function to undo the last action


void undo() {
if (undoCount > 0) {
// Pop from undo stack
Student lastStudent = undoStack[--undoCount];

// Remove last student from records


studentCount--;

printf("Undone: Student records\n");


} else {
printf("No actions to undo.\n");
}
}

// Main function to run the program


int main() {
char command;
char name[100];
int age;
float gpa;

while (1) {
scanf(" %c", &command); // Read command

switch (command) {
case 'a':
scanf(" %[^\n]", name); // Read name (including spaces)
scanf("%d", &age); // Read age
scanf("%f", &gpa); // Read GPA

addStudent(name, age, gpa);


break;

case 'u':
undo();
break;

case 'p':
printStudents();
break;

case 'q':
exit(0); // Quit the program

default:
printf("Invalid Choice\n");
break;
}
}

return 0;
}

3).
MILTON

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

// Define the structure for a node in the linked list


typedef struct Node {
char data;
struct Node* next;
} Node;

// Define the structure for the stack


typedef struct {
Node* top;
} Stack;

// Function to create a new node


Node* createNode(char data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to create a new stack


Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
if (!stack) {
printf("Memory error\n");
return NULL;
}
stack->top = NULL;
return stack;
}

// Function to check if the stack is empty


int isEmpty(Stack* stack) {
return stack->top == NULL;
}

// Function to push a character onto the stack


void push(Stack* stack, char data) {
Node* newNode = createNode(data);
if (stack->top) {
newNode->next = stack->top;
}
stack->top = newNode;
printf("Adding Section: %c\n", data);
}
// Function to pop a character from the stack
void pop(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty. Cannot pop.\n");
return;
}
Node* temp = stack->top;
printf("Removing Section: %c\n", temp->data);
stack->top = temp->next;
free(temp);
}

// Function to display the characters in the stack


void display(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}
printf("Enrolled Sections: ");
Node* temp = stack->top;
while (temp) {
printf("%c ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
Stack* stack = createStack();
int choice;
char data;
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf(" %c", &data);
if (isalpha(data)) {
push(stack, data);
} else {
printf("Invalid input. Only alphabetic characters are allowed.\
n");
}
break;
case 2:
pop(stack);
break;
case 3:
display(stack);
break;
case 4:
printf("Exiting program\n");
return 0;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

4).
SOFTWARE

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

// Define the structure for a node in the stack


typedef struct Node {
char data;
struct Node* next;
} Node;

// Define the structure for the stack


typedef struct {
Node* top;
} Stack;

// Function to create a new node


Node* createNode(char data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to create a new stack


Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
if (!stack) {
printf("Memory error\n");
return NULL;
}
stack->top = NULL;
return stack;
}

// Function to check if the stack is empty


int isEmpty(Stack* stack) {
return stack->top == NULL;
}

// Function to push a character onto the stack


void push(Stack* stack, char data) {
Node* newNode = createNode(data);
if (stack->top) {
newNode->next = stack->top;
}
stack->top = newNode;
}
// Function to pop a character from the stack
char pop(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty. Cannot pop.\n");
return '\0';
}
Node* temp = stack->top;
char data = temp->data;
stack->top = temp->next;
free(temp);
return data;
}

// Function to get the precedence of an operator


int getPrecedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}

// Function to convert infix to postfix


void infixToPostfix(char* infix) {
Stack* stack = createStack();
int length = strlen(infix);
for (int i = 0; i < length; i++) {
char c = infix[i];
if (isalpha(c)) {
printf("%c", c);
} else if (c == '(') {
push(stack, c);
} else if (c == ')') {
while (stack->top->data != '(') {
printf("%c", pop(stack));
}
pop(stack); // Remove the '('
} else {
while (!isEmpty(stack) && getPrecedence(stack->top->data) >=
getPrecedence(c)) {
printf("%c", pop(stack));
}
push(stack, c);
}
}
while (!isEmpty(stack)) {
printf("%c", pop(stack));
}
printf("\n");
}

int main() {
char infix[100];
printf("");
scanf("%s", infix);
printf("");
infixToPostfix(infix);
return 0;
}

CHALLENGE YOURSELF

1).
KRISH

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

#define MAX_SIZE 100

// Stack structure
struct Stack {
int items[MAX_SIZE];
int top;
};

// Function to create a new stack


struct Stack* createStack() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = -1; // stack is empty
return stack;
}

// Function to check if the stack is full


int isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}

// Function to check if the stack is empty


int isEmpty(struct Stack* stack) {
return stack->top == -1;
}

// Function to push an item onto the shelf


void push(struct Stack* stack, int item) {
if (isFull(stack)) {
printf("Shelf is full. Cannot push item %d.\n", item);
return;
}
stack->items[++stack->top] = item;
printf("Item %d is pushed onto the shelf\n", item);
}

// Function to pop an item from the shelf


void pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("No items in the shelf\n");
return;
}
int item = stack->items[stack->top--];
printf("Item %d is popped from the shelf\n", item);
}

// Function to display the current items on the shelf


void display(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Shelf is empty\n");
return;
}
printf("Items in the shelf: ");
for (int i = stack->top; i >= 0; i--) {
printf("%d ", stack->items[i]);
}
printf("\n");
}

// Function to free the stack memory


void freeStack(struct Stack* stack) {
free(stack);
}

// Main function to run the program


int main() {
struct Stack* stack = createStack();
int choice, item;

while (1) {
scanf("%d", &choice);

switch (choice) {
case 1:
scanf("%d", &item);
push(stack, item);
break;
case 2:
pop(stack);
break;
case 3:
display(stack);
break;
case 4:
printf("Exiting the warehouse\n");
freeStack(stack);
return 0;
default:
printf("Invalid choice\n");
break;
}
}

return 0;
}

2).
UNIVERSITY

#include <stdio.h>
#include <stdlib.h>
// Define the node structure for the linked list
struct Node {
int roll_number;
struct Node* next;
};

// Define the stack structure


struct Stack {
struct Node* top;
};

// Function to create a new node


struct Node* createNode(int roll_number) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->roll_number = roll_number;
newNode->next = NULL;
return newNode;
}

// Function to initialize the stack


struct Stack* createStack() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = NULL;
return stack;
}

// Function to check if the stack is empty


int isEmpty(struct Stack* stack) {
return stack->top == NULL;
}

// Function to push a roll number onto the stack


void push(struct Stack* stack, int roll_number) {
struct Node* newNode = createNode(roll_number);
newNode->next = stack->top;
stack->top = newNode;
printf("Adding students to the roll: %d\n", roll_number);
}

// Function to pop a roll number from the stack


void pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty. Cannot pop.\n");
return;
}
struct Node* temp = stack->top;
stack->top = stack->top->next;
printf("Removing students from the roll: %d\n", temp->roll_number);
free(temp);
}

// Function to display the current stack of roll numbers


void display(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}
struct Node* current = stack->top;
printf("Displaying the current list of enrolled students: ");
while (current != NULL) {
printf("%d ", current->roll_number);
current = current->next;
}
printf("\n");
}

// Function to free the stack memory


void freeStack(struct Stack* stack) {
while (!isEmpty(stack)) {
pop(stack);
}
free(stack);
}

// Main function to run the program


int main() {
struct Stack* stack = createStack();
int choice, roll_number;

while (1) {
scanf("%d", &choice);

switch (choice) {
case 1:
scanf("%d", &roll_number);
push(stack, roll_number);
break;
case 2:
pop(stack);
break;
case 3:
display(stack);
break;
case 4:
printf("Exiting program\n");
//freeStack(stack);
return 0;
default:
printf("Invalid choice\n");
break;
}
}

return 0;
}

PRACTICE AT HOME

1).
RAJ

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

// Node structure for the stack


typedef struct Node {
char data;
struct Node* next;
} Node;

// Stack structure
typedef struct Stack {
Node* top;
} Stack;

// Function to create a new node


Node* createNode(char data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to initialize the stack


void initStack(Stack* stack) {
stack->top = NULL;
}

// Function to check if the stack is empty


int isEmpty(Stack* stack) {
return stack->top == NULL;
}

// Function to push an element onto the stack


void push(Stack* stack, char data) {
Node* newNode = createNode(data);
newNode->next = stack->top;
stack->top = newNode;
printf("Pushed element: %c\n", data);
}

// Function to pop an element from the stack


char pop(Stack* stack) {
if (isEmpty(stack)) {
return '\0'; // Indicates that the stack is empty
}
Node* temp = stack->top;
char poppedData = temp->data;
stack->top = temp->next;
free(temp);
return poppedData;
}

// Function to display the elements in the stack


void display(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}

printf("Stack elements (top to bottom): ");

Node* current = stack->top;

while (current != NULL) {


printf("%c ", current->data);
current = current->next;
}

printf("\n");
}

// Main function to run the program


int main() {
Stack stack;
initStack(&stack);

int choice;
char element;

while (1) {
scanf("%d", &choice); // Read user choice

switch (choice) {
case 1:
scanf(" %c", &element); // Read character input for push
push(&stack, element);
break;

case 2:
if (isEmpty(&stack)) {
printf("Stack is empty. Cannot pop.\n");
} else {
char poppedElement = pop(&stack);
printf("Popped element: %c\n", poppedElement);
}
break;

case 3:
display(&stack);
break;

case 4:
printf("Exiting program\n");
exit(0);

default:
printf("Invalid choice\n");
break;
}
}

return 0;
}

2).
raja

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

#define MAX 100


// Stack structure for integers
typedef struct Stack {
int items[MAX];
int top;
} Stack;

// Function to initialize the stack


void initStack(Stack *s) {
s->top = -1;
}

// Function to check if the stack is empty


int isEmpty(Stack *s) {
return s->top == -1;
}

// Function to push an item onto the stack


void push(Stack *s, int item) {
if (s->top < MAX - 1) {
s->items[++(s->top)] = item;
}
}

// Function to pop an item from the stack


int pop(Stack *s) {
if (!isEmpty(s)) {
return s->items[(s->top)--];
}
return 0; // Return 0 if stack is empty (shouldn't happen in this context)
}

// Function to evaluate a postfix expression


int evaluatePostfix(const char *expr) {
Stack stack;
initStack(&stack);

for (int i = 0; expr[i] != '\0'; i++) {


char ch = expr[i];

// If character is a digit, push its integer value onto the stack


if (isdigit(ch)) {
push(&stack, ch - '0'); // Convert char to int
} else { // It must be an operator
int operand2 = pop(&stack);
int operand1 = pop(&stack);
int result;

switch (ch) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
push(&stack, result); // Push the result back onto the stack
}
}

// The final result will be on the top of the stack


return pop(&stack);
}

int main() {
char expr[MAX];

// Input expression
scanf("%s", expr);

// Evaluate
int result = evaluatePostfix(expr);

// Output result
printf("%d\n", result);

return 0;
}

3).
NAREN

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

#define MAX 100

// Stack Structure
typedef struct Stack {
char items[MAX];
int top;
} Stack;

// Function to create a stack


void initStack(Stack* s) {
s->top = -1;
}

// Function to check if the stack is empty


int isEmpty(Stack* s) {
return s->top == -1;
}

// Function to push an item onto the stack


void push(Stack* s, char item) {
if (s->top < MAX - 1) {
s->items[++(s->top)] = item;
}
}
// Function to pop an item from the stack
char pop(Stack* s) {
if (!isEmpty(s)) {
return s->items[(s->top)--];
}
return '\0'; // Return null character if stack is empty
}

// Function to get the top item of the stack


char peek(Stack* s) {
return s->items[s->top];
}

// Function to check if the brackets are balanced


int areBracketsBalanced(const char* exp) {
Stack s;
initStack(&s);
for (int i = 0; i < strlen(exp); i++) {
char ch = exp[i];
if (ch == '(' || ch == '{' || ch == '[') {
push(&s, ch);
} else if ((ch == ')' || ch == '}' || ch == ']')) {
if (isEmpty(&s)) {
return 0; // Stack is empty, thus unbalanced
}
char topChar = pop(&s);
if ((ch == ')' && topChar != '(') ||
(ch == '}' && topChar != '{') ||
(ch == ']' && topChar != '[')) {
return 0; // Mismatched brackets
}
}
}
return isEmpty(&s); // If stack is empty at the end, brackets are balanced
}

int main() {
char exp[MAX];

// Input expression
scanf("%s", exp);

// Print the input expression


printf("Expression: %s\n", exp);

// Check if brackets are balanced


if (areBracketsBalanced(exp)) {
printf("Brackets are balanced\n");
} else {
printf("Brackets are not balanced\n");
}

return 0;
}

4).
RAM
#include <stdio.h>

void removeMiddleElement(int stack[], int size) {


int midIndex;

// Determine the index of the middle element to remove


if (size % 2 == 0) {
midIndex = size / 2 - 1; // Closer to the bottom for even size
} else {
midIndex = size / 2; // For odd size
}

// Shift elements to remove the middle element


for (int i = midIndex; i < size - 1; i++) {
stack[i] = stack[i + 1];
}
}

void printStack(int stack[], int size) {


for (int i = size - 1; i >= 0; i--) {
printf("%d", stack[i]);
if (i > 0) {
printf(" ");
}
}
printf("\n");
}

int main() {
int v1;

// Read the number of elements in the stack


scanf("%d", &v1);

int stack[10]; // Since the constraint specifies that 1 ≤ v1 ≤ 10

// Read the elements into the stack


for (int i = 0; i < v1; i++) {
scanf("%d", &stack[i]);
}

// Remove the middle element from the stack


removeMiddleElement(stack, v1);

// Print the remaining elements in the stack


printStack(stack, v1 - 1); // Printing v1 - 1 since one element has been
removed

return 0;
}

You might also like