0% found this document useful (0 votes)
7 views21 pages

CS162 - 2

The document outlines a lab assessment for a Data Structures course, focusing on implementing stack operations using arrays and linked lists, along with infix to postfix and prefix expression conversions. It includes detailed programming tasks, code implementations, and expected outputs for each task. The student, Ajudiya Krish Prafulkumar, is required to submit the assessment by February 6, 2025.

Uploaded by

krishajudiya21
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)
7 views21 pages

CS162 - 2

The document outlines a lab assessment for a Data Structures course, focusing on implementing stack operations using arrays and linked lists, along with infix to postfix and prefix expression conversions. It includes detailed programming tasks, code implementations, and expected outputs for each task. The student, Ajudiya Krish Prafulkumar, is required to submit the assessment by February 6, 2025.

Uploaded by

krishajudiya21
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/ 21

Semester Course Code Course Title

Data Structures Lab


Winter 2024-25 CS162

Student Details: Student Name: Ajudiya Krish Prafulkumar


Roll/Reg No: 202411005
Email: krishajudiya21@gmail.com
Mobile: 8799400820
Faculty Details: Faculty Name: Dr. VENKATA PHANIKRISHNA B
Department: Computer Science and Engineering
Email: venkata_phanikrishna@diu.iiitvadodara.ac.in
Assessment No: 2
Assessment Title. Implement Stack Using Array and Linked List and its application

Date of Submission 6-Feb-2025

Implement Stack using Array and Linked List


A2: P1@Write a program to develop a stack using an array.
A2: P2@Write a program to develop a stack using a singly linked list.
A2: P3@ Write a program to implement stack operations for Conversion of an infix expression
to postfix and evaluation of the converted postfix expression.
A2: P4@ Write a program to implement stack operations for Conversion of an infix expression
to prefix and evaluation of the converted prefix expression.
Question 1 Write a program to develop a stack using an array.

Program #include <stdio.h>


#include <stdlib.h>

struct stack{
int size;
int top;
int *arr;
};

void push_back(struct stack *s){


int val;
scanf("%d",&val);

s->top++;
s->arr[s->top]=val;
}

void pop_back(struct stack *s){


if(s->top == -1){
printf("Stack underflow! Cannot pop.");
}else{
int a=s->arr[s->top];
s->top--;

printf("Popped element : %d",a);


}
}

void empty(struct stack *s){


if(s->top == -1){
printf("Stack is empty");
}
}

void full(struct stack *s){


if(s->top == s->size-1){
printf("Stack is full");
}
}

void display(struct stack *s){


if(s->top == -1){
printf("Stack is empty! Nothing to display.");
}else{
int n=s->top;
while(n != -1){
printf("%d\n",s->arr[n]);
n--;
}
}
}

int main()
{

struct stack *s=(struct stack *)malloc(sizeof(struct stack));


s->size=100;
s->top=-1;
s->arr=(int *)malloc(s->size * sizeof(int));

int ch;
scanf("%d",&ch);

while(ch!=9)
{
switch(ch)
{
case 2:
push_back(s);
break;
case 3:
pop_back(s);
break;
case 4:
printf("%d",s->arr[s->top]);
break;
case 5:
printf("Stack size:%d\n",s->top+1);
break;
case 6:
empty(s);
break;
case 7:
full(s);
break;
case 8:
display(s);
break;
}

if(ch!=2)
{
break;
}

scanf("%d",&ch);
}
return 0;
}

Output:

Your This program creates a stack where users can add numbers, remove numbers, check if the
Observation stack is full or empty, see the top number, view all stored numbers, and check the stack size
using a array.

Question 2 Write a program to develop a stack using a singly linked list.

Program #include <stdio.h>


#include <stdlib.h>
int c=0,elm=0;

struct node{
int data;
struct node *next;
};

struct node *head=NULL;

void push_back(){
int val;
scanf("%d",&val);

struct node *newnode=(struct node *)malloc(sizeof(struct node));


newnode->data=val;
newnode->next=NULL;
if(head==NULL){
head=newnode;
elm++;
}
else{
struct node *p=head;
while(p->next != NULL){
p=p->next;
}
p->next=newnode;
elm++;
}
}

void pop_back(){

if(head==NULL){
printf("Stack underflow! Cannot pop.");
c=1;
return;
}
else{
if(head->next == NULL){
struct node *p=head;
head==NULL;
free(p);
elm--;
}else{
struct node *p=head;
struct node *q=p->next;
while(q->next != NULL){
q=q->next;
p=p->next;
}
printf("%d",q->data);
p->next=NULL;
free(q);
elm--;
}
}
}

void peek(){
struct node *p=head;
while(p->next != NULL){
p=p->next;
}
printf("Top element: %d",p->data);
}
void empty(){
if(head==NULL){
printf("Stack is empty");
}
}

void display(){
if(head==NULL){
printf("Stack is empty! Nothing to display.");
}else{
int a[elm],i=0;
struct node *p=head;

while(p != NULL){
a[i]=p->data;
p=p->next;
i++;
}
printf("Stack elements (top to bottom):\n");
for(int i=elm-1;i>=0;i--)
{
printf("%d\n",a[i]);
}

}
}

int main()
{

int ch;
scanf("%d",&ch);

while(ch!=8)
{
switch(ch)
{
case 2:
push_back();
break;
case 3:
pop_back();
break;
case 4:
peek();
break;
case 5:
printf("Stack size: %d",elm);
break;
case 6:
empty();
break;
case 7:
display();
break;
}

if(c==1 || ch==4 || ch == 5 || ch == 6 || ch==7)


{
break;
}

scanf("%d",&ch);
}
return 0;
}

Output:

Your This program creates a stack where users can add numbers, remove numbers, check if the
Observation stack is full or empty, see the top number, view all stored numbers, and check the stack size
using a linked list.

Question 3 Write a program to implement stack operations for Conversion of an infix expression to
postfix and evaluation of the converted postfix expression.

Program #include <stdio.h>


#include <stdlib.h>
#include <ctype.h>
struct node {
char data;
int v;
struct node *next;
};
struct node *head=NULL;
char ans[100];
int i_dex=0;

void pop_back(){
if (head == NULL) return;
char val='0';
do{
struct node *p=head;
struct node *q=NULL;
while(p->next!=NULL)
{
q = p;
p = p->next;
}
if (p->data != '(') {
ans[i_dex++] = p->data;
}

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

free(p);

}while(val!='(' && head!=NULL);


}

void pop_down(){
if(head->next==NULL){
head=NULL;
}else{
struct node *p=head;
struct node *q=p->next;
while(q->next!=NULL){
p=p->next;
q=q->next;
}
p->next=NULL;
free(q);
}
}

void push_back(char c){


struct node *n=(struct node *)malloc(sizeof(struct node));
n->data=c;
n->v=0;
n->next=NULL;

if(head==NULL){
head=n;
}else{
struct node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;
}
}

void push_down(int m){


struct node *n=(struct node *)malloc(sizeof(struct node));
n->data='0';
n->v=m;
n->next=NULL;

if(head==NULL){
head=n;
}else{
struct node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;
}
}

void display(int a,char n){

if(a==0){
struct node *p = head;
while (p != NULL)
{
printf("%c ", p->data);
p = p->next;
}}else{
struct node *p = head;
while (p->data != n)
{
printf("%c,", p->data);
p = p->next;
}
printf("%c",p->data);
}
}
int process(int *a,int *b){
struct node *p=head;
struct node *q=p->next;

while(q->next != NULL){
p=p->next;
q=q->next;
}
*a=p->v;
*b=q->v;

}
void del(char m,char n){
struct node *p=head;
struct node *q=p->next;
struct node *r=q->next;
struct node *s=r->next;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=n;
newnode->next=NULL;

while(s->data != m){
p=p->next;
q=q->next;
r=r->next;
s=s->next;
}
p->next=newnode;
newnode->next=r->next;
free(q);
free(r);
}

int peek(){
struct node *p=head;
while(p->next != NULL){
p=p->next;
}
return p->data;
}

int order(char c) {
if (c == '*' || c == '/') {
return 3;
} else if (c == '+' || c == '-') {
return 2;
} else {
return 1;
}
}

int main(){
char s[100];
scanf("%s",s);

printf("Step-by-Step Processing:\n");
printf("Scanned\t Stack\t Output\n");
printf("-------------------------------------------------\n");

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

printf("Scanned: %c",s[i]);
printf("\t");
if(isdigit(s[i])){
ans[i_dex]=s[i];
i_dex++;
}else if(s[i] != ')'){
if(s[i]=='+'||s[i]=='-'||s[i]=='/'||s[i]=='*'){
if(head==NULL){
push_back(s[i]);
}else{
if(order(s[i]) > order(peek())){
push_back(s[i]);
}else{
pop_back();
push_back(s[i]);
}
}
}else{
push_back(s[i]);
}
}else if(s[i]==')'){
pop_back();
}
printf("Stack: ");
display(0,'0');
printf("\t");
printf("Output: ");
printf("%s", ans);
printf("\n");
}
while(head!=NULL){
pop_back();
}
head=NULL;

printf("Final Postfix Expression: ");


printf("%s\n",ans);
printf("Given Infix: ");
printf("%s\n",s);
printf("Conversion Postfix: ");
printf("%s\n",ans);
printf("Postfix evaluation steps:\n");
printf("-------------------------------------------------\n");

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


int a,b;
int n;
switch(ans[i]){
case '+':
printf("Processing operator: ");
printf("+\n");
process(&a,&b);
n=(a)+(b);
printf("Popped %d and %d\n",a,b);
printf("Calculated: %d + %d = ",a,b);
printf("%d\n",n);
printf("Pushed result: %d\n",n);
pop_down();
pop_down();
push_down(n);
break;
case '-':
printf("Processing operator: ");
printf("-\n");
process(&a,&b);
n=(a)-(b);
printf("Popped %d and %d\n",a,b);
printf("Calculated: %d - %d = ",a,b);
printf("%d\n",n);
printf("Pushed result: %d\n",n);
pop_down();
pop_down();
push_down(n);
break;
case '/':
printf("Processing operator: ");
printf("/\n");
process(&a,&b);
n=(a)/(b);
printf("Popped %d and %d\n",a,b);
printf("Calculated: %d / %d = ",a,b);
printf("%d\n",n);
printf("Pushed result: %d\n",n);
pop_down();
pop_down();
push_down(n);
break;
case '*':
printf("Processing operator: ");
printf("*\n");
process(&a,&b);
n=(a)*(b);
printf("Popped %d and %d\n",a,b);
printf("Calculated: %d * %d = ",a,b);
printf("%d\n",n);
printf("Pushed result: %d\n",n);
pop_down();
pop_down();
push_down(n);
break;
default:
printf("Pushed operand: %c\n",ans[i]);
push_down(ans[i]-'0');
break;

}
printf("Stack: [");
struct node *p = head;
while (p != NULL)
{
printf("%d", p->v);
if (p->next != NULL)
printf(", ");
p = p->next;
}
printf("]\n");
}
printf("Final Postfix evaluation result: ");
printf("%d",head->v);

Output:
Your This program converts an infix expression to postfix and evaluates it using a stack, showing
Observation step-by-step processing and operand operations with a linked list.

Question 4 Write a program to implement stack operations for Conversion of an infix expression to
prefix and evaluation of the converted prefix expression.

Program #include <stdio.h>


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

struct node {
char data;
int v;
struct node *next;
};
struct node *head=NULL;
char ans[100],pre_ans[100];
int i_dex=0;

void display(int a,char n){

if(a==0){
struct node *p = head;
while (p != NULL)
{
printf("%c", p->data);
if (p->next != NULL) printf(", ");
p = p->next;
}}else{
struct node *p = head;
while (p->data != n)
{
printf("%c,", p->data);
p = p->next;
}
printf("%c",p->data);
}

void pop_back(int n){


if (head == NULL) return;
char val='0';
do{
struct node *p=head;
struct node *q=NULL;
while(p->next!=NULL)
{
q = p;
p = p->next;
}
if (p->data != '(') {
ans[i_dex++] = p->data;
val=p->data;
}else{
val='(';
}

if (q != NULL) {
q->next = NULL;
} else {
head = NULL;
}
if(n==1){
printf("Pop from stack %s\t",ans);
printf("[");
display(0,'0');
printf("]\n");
}

free(p);

}while(val!='(' && head!=NULL);


}

void pop_down(){
if(head->next==NULL){
head=NULL;
}else{
struct node *p=head;
struct node *q=p->next;
while(q->next!=NULL){
p=p->next;
q=q->next;
}
p->next=NULL;
free(q);
}
}

void push_back(char c){


struct node *n=(struct node *)malloc(sizeof(struct node));
n->data=c;
n->v=0;
n->next=NULL;

if(head==NULL){
head=n;
}else{
struct node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;
}
}

void push_down(int m){


struct node *n=(struct node *)malloc(sizeof(struct node));
n->data='0';
n->v=m;
n->next=NULL;

if(head==NULL){
head=n;
}else{
struct node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;
}
}

int process(int *a,int *b){


struct node *p=head;
struct node *q=p->next;
while(q->next != NULL){
p=p->next;
q=q->next;
}
*a=p->v;
*b=q->v;

}
void del(char m,char n){
struct node *p=head;
struct node *q=p->next;
struct node *r=q->next;
struct node *s=r->next;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=n;
newnode->next=NULL;

while(s->data != m){
p=p->next;
q=q->next;
r=r->next;
s=s->next;
}
p->next=newnode;
newnode->next=r->next;
free(q);
free(r);
}

int peek(){
struct node *p=head;
while(p->next != NULL){
p=p->next;
}
return p->data;
}

int order(char c) {
if (c == '*' || c == '/') {
return 3;
} else if (c == '+' || c == '-') {
return 2;
} else {
return 1;
}
}

int main()
{
char string[100],s[100];
scanf("%s",string);
int len = strlen(string);
for(int i=0; i<len; i++){
s[i] = string[len - i - 1];
if (s[i] == '(') s[i] = ')';
else if (s[i] == ')') s[i] = '(';

}
s[len] = '\0';

printf("Step 1: Reverse and Swap Parentheses\n");


printf("Original infix: %s\n",string);
printf("Reversed and modified infix: %s\n",s);
printf("\nStep 2: Convert Reversed Infix to Postfix\n\n");
printf("Character\tAction\t\tOutput Stack\n");
printf("-------------------------------------------------\n");

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

printf("%c",s[i]);
printf("\t");
if(isdigit(s[i])){
ans[i_dex]=s[i];
i_dex++;
printf("Added to output ");
}else if(s[i] != ')'){
if(s[i]=='+'||s[i]=='-'||s[i]=='/'||s[i]=='*'){
if(head==NULL){
push_back(s[i]);
}else{
if(order(s[i]) > order(peek())){
push_back(s[i]);
}else{
pop_back(1);
push_back(s[i]);
printf("%c",s[i]);
printf("\t");
}
}
}else{
push_back(s[i]);
}
printf("Pushed to stack ");
}else if(s[i]==')'){
pop_back(0);
printf("Pop until '(' ");
}

printf("%s",ans);
printf("\t");
printf("[");
display(0,'0');
printf("]\n");
}
while(head!=NULL){
pop_back(0);
}
printf("\tPop remaining %s",ans);
printf("\t[]\n");
printf("Postfix of reversed infix: %s\n\n",ans);
printf("Step 3: Final Prefix Notation\n");
int l = strlen(ans);
for(int i=0; i<l; i++){
pre_ans[l - i - 1]=ans[i];
}
pre_ans[l] = '\0';

printf("Final Prefix Notation: %s\n",pre_ans);


printf("-------------------------------------------------\n");
printf("Given infix Notation: %s\n",string);
printf("Prefix Notation: %s\n",pre_ans);
printf("Prefix Evaluation Steps\n");
printf("-------------------------------------------------\n");

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


int a,b;
int n;
switch(ans[i]){
case '+':
printf("Processing operator: ");
printf("+\n");
process(&a,&b);
n=(a)+(b);
printf(" Popped %d and %d\n",b,a);
printf(" Calculated: %d + %d = ",b,a);
printf("%d\n",n);
printf(" Pushed result: %d\n",n);
pop_down();
pop_down();
push_down(n);
break;
case '-':
printf("Processing operator: ");
printf("-\n");
process(&a,&b);
n=b-a;
printf(" Popped %d and %d\n",b,a);
printf(" Calculated: %d - %d = ",b,a);
printf("%d\n",n);
printf(" Pushed result: %d\n",n);
pop_down();
pop_down();
push_down(n);
break;
case '/':
printf("Processing operator: ");
printf("/\n");
process(&a,&b);
n=b/a;
printf(" Popped %d and %d\n",b,a);
printf(" Calculated: %d / %d = ",b,a);
printf("%d\n",n);
printf(" Pushed result: %d\n",n);
pop_down();
pop_down();
push_down(n);
break;
case '*':
printf("Processing operator: ");
printf("*\n");
process(&a,&b);
n=(a)*(b);
printf(" Popped %d and %d\n",b,a);
printf(" Calculated: %d * %d = ",b,a);
printf("%d\n",n);
printf(" Pushed result: %d\n",n);
pop_down();
pop_down();
push_down(n);
break;
default:
printf("Pushed operand: %c\n",ans[i]);
push_down(ans[i]-'0');
break;

}
printf("Stack: [");
struct node *p = head;
while (p != NULL)
{
printf("%d", p->v);
if (p->next != NULL)
printf(", ");
p = p->next;
}
printf("]\n\n");
}
printf("Final Prefix Evaluation result: ");
printf("%d",head->v);
}
Output:

Your This program converts an infix expression to prefix and evaluates it using a stack,
Observation showing step-by-step processing and operand operations with a linked list.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy