[go: up one dir, main page]

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

Experiment Report 3. 何娜娜

This document is a student experimental report for the Data Structures course at Chongqing University of Posts and Telecommunications. It details an experiment involving the implementation of a stack using C programming, covering stack operations, pointer manipulation, and array usage. The report includes purpose, environment, code examples, and practical applications of stack data structures.

Uploaded by

wasanafer09ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Experiment Report 3. 何娜娜

This document is a student experimental report for the Data Structures course at Chongqing University of Posts and Telecommunications. It details an experiment involving the implementation of a stack using C programming, covering stack operations, pointer manipulation, and array usage. The report includes purpose, environment, code examples, and practical applications of stack data structures.

Uploaded by

wasanafer09ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

学生实验实习报告册

TERM : 2024 -2025 (2)

COURSE : DATA STRUCTURES

COLLAGE: COMPUTER SCIENCE

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.

Meets the norms? ☑YES □ NO

Meets the standards? ☑YES □ NO

Complete the required ☑ YES □ NO

contents?

REPORT COMMENTS:

REPORT SCORE:

TEACHER SIGNATURE:

DATE:
CONTENTS

 TEACHER COMMENT TABLE


 EXPERIMENTAL REPORT
EXPERIMENTAL REPORT
COURSE DATA STRUCTURES COURSE NO L0400019
COLLEGE School of Computer Science and Technology
TEACHER JIAYU ZHU
LOCATION Comprehensive laboratory building B509
STUDENT STUDENT
henana L202330005
NAME NUMBER
SPECIALITY CLASS

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

三、Experiment contents (the function of the program)


stack operations, pointer manipulation, and array usage in C programming.
this program implements a simple stack using a one-dimensional array and pointers in C
language. It allows users to perform basic stack operations through a menu, including
pushing new elements onto the stack, popping elements from the stack, and printing all
current elements. The stack follows the Last-In-First-Out (LIFO) principle, ensuring that
the last element inserted is the first to be removed. The program also handles stack
overflow and underflow conditions by checking if the stack is full or empty before
performing operations. This experiment helps in understanding the core concepts of
stack operations and pointer manipulation.

四、Codes(please mark the modified code)


#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");

printf("\n\t Please choose [1-4]: ");


scanf("%d", &choose);

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.

The experimental task

(1) Replenish the missing codes in the above program (must do).

sptr = stack; // sptr points to stack[0]

sptr++; // make sptr point to the next position of the array

printf("%d ", *temp); // print the element pointed by temp

#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");

printf("\n\t Please choose [1-4]: ");


scanf("%d", &choose);

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");
}

(2) Think the practical application of the stack.


· Expression Evaluation and Syntax Parsing
(e.g., calculators, compilers parsing expressions like (a+b)*(c-d).)
· · Undo/Redo operations in editors
(Word processors, Photoshop use stacks to manage "undo" history.)
· · Function Call Management
(Every function call in C uses a stack internally to store return addresses, local variables.)
· · Balanced Parentheses Checking
(Checking if an expression has properly nested brackets () [] {}.)
· · Backtracking Algorithms
(Maze solving, solving puzzles like Sudoku.)
· · Browser History Management
(Back/Forward navigation uses stack.)

You might also like