CS162 - 2
CS162 - 2
struct stack{
int size;
int top;
int *arr;
};
s->top++;
s->arr[s->top]=val;
}
int main()
{
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.
struct node{
int data;
struct node *next;
};
void push_back(){
int val;
scanf("%d",&val);
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;
}
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.
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);
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);
}
}
if(head==NULL){
head=n;
}else{
struct node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;
}
}
if(head==NULL){
head=n;
}else{
struct node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=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");
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("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.
struct node {
char data;
int v;
struct node *next;
};
struct node *head=NULL;
char ans[100],pre_ans[100];
int i_dex=0;
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);
}
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);
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);
}
}
if(head==NULL){
head=n;
}else{
struct node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;
}
}
if(head==NULL){
head=n;
}else{
struct node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;
}
}
}
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("%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("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.