Experiment Report 3. 何娜娜
Experiment Report 3. 何娜娜
CLASS :
NUMBER : L202330005
NAME : 何娜娜
TELEPHONE : 13637821594
重庆邮电大学教务处印制
TEACHER COMMENT TABLE
【NOTICE】
This page must be included in experimental report.
The format of this report cannot be changed.
contents?
REPORT COMMENTS:
REPORT SCORE:
TEACHER SIGNATURE:
DATE:
CONTENTS
Experiment 3
一、Purpose
Understand and master the storage structure and implementation of the stack; master the
fundamental operation of the stack; master the application of the stack.
二、Experiment enviroment
Windows operating system、 codeblocks
void push(void);
void pop(void);
void printInfo(void);
int main() {
sptr = stack; // sptr points to stack[0]
empty = stack; // empty points to stack[0]
full = stack + MAXSIZE - 1; // full points to stack[9]
do {
printf("\n\t===============STACK EXAMPLE==============\n");
printf("\n\t 1. Push stack");
printf("\n\t 2. Pop stack");
printf("\n\t 3. Print elements of the stack");
printf("\n\t 4. Exit\n");
switch(choose) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printInfo();
break;
case 4:
exit(0);
default:
printf("\n\n\t==================Input error=================");
break;
}
} while(1);
return 0;
}
void push(void) {
sptr++; // make sptr point to the next position of the array
if(sptr == full) {
printf("\n\n ........The stack is full.......");
sptr--; // move back to previous if full
} else {
printf("Input the %d-th element: ", i++);
scanf("%d", sptr);
}
}
void pop(void) {
if(sptr != empty) {
sptr--;
i--;
} else {
printf("\n\n\t\t ........the stack is empty.......");
i = 1;
}
}
void printInfo(void) {
int *temp;
temp = sptr;
printf("\n\n\t The elements in the stack are: ");
do {
if(temp != empty) {
printf("%d ", *temp); // print the element
temp--;
} else {
break;
}
} while(1);
printf("\n\n\t================END===============\n");
}
五、Summary
The program successfully:
Adds elements into the stack (up to 10 elements).
Removes the most recently added element.
Displays the current elements in the stack.
The stack followed the Last-In-First-Out (LIFO) principle correctly.
Appropriate messages were displayed when trying to push onto a full stack or pop
from an empty stack.
In this experiment, a simple stack data structure was implemented using a one-dimensional
array and pointers in C language.
Key operations performed include:
Push operation:
Adds a new element to the stack if it is not full. The pointer sptr moves forward to
the next position before inserting the value.
Pop operation:
Removes the top element from the stack if it is not empty. The pointer sptr moves
backward, effectively deleting the top element.
Print operation:
Displays all elements currently in the stack by traversing from the top down to the
bottom.
(1) Replenish the missing codes in the above program (must do).
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 10
int i = 1, choose;
int *sptr, *full, *empty;
int stack[MAXSIZE];
void push(void);
void pop(void);
void printInfo(void);
int main() {
sptr = stack; // sptr points to stack[0]
empty = stack; // empty points to stack[0]
full = stack + MAXSIZE - 1; // full points to stack[9]
do {
printf("\n\t===============STACK EXAMPLE==============\n");
printf("\n\t 1. Push stack");
printf("\n\t 2. Pop stack");
printf("\n\t 3. Print elements of the stack");
printf("\n\t 4. Exit\n");
switch(choose) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printInfo();
break;
case 4:
exit(0);
default:
printf("\n\n\t==================Input error=================");
break;
}
} while(1);
return 0;
}
void push(void) {
sptr++; // make sptr point to the next position of the array
if(sptr == full) {
printf("\n\n ........The stack is full.......");
sptr--; // move back to previous if full
} else {
printf("Input the %d-th element: ", i++);
scanf("%d", sptr);
}
}
void pop(void) {
if(sptr != empty) {
sptr--;
i--;
} else {
printf("\n\n\t\t ........the stack is empty.......");
i = 1;
}
}
void printInfo(void) {
int *temp;
temp = sptr;
printf("\n\n\t The elements in the stack are: ");
do {
if(temp != empty) {
printf("%d ", *temp); // print the element
temp--;
} else {
break;
}
} while(1);
printf("\n\n\t================END===============\n");
}