C中堆栈操作中的指针变量与全局变量
Pointer Variable vs Global variables in Stack operation in C
这是一个通过链表进行堆栈操作的简单程序,这是正式的方式。我理解指示堆栈顶部所需的指针和指向下一个堆栈的指针的概念node.But在这种情况下是否有必要制作结构变量的指针变量 struct student variable 即 typedef to Student 。我声明为 Student *(任何名称);.然后通过 malloc.I 动态分配内存可以改为使用全局变量as Student (anyname);and use is to fill information then push to stack.So 使用全局变量或指针有什么区别和优缺点。
关于我自己编写的程序,它运行良好。
typedef struct student{
int roll;
char name[30];
struct student *next;
}Student;
typedef struct stack{
Student *top;
}Stack;
Student *makestudent(int r,char n[]){
Student *newstudent;
if((newstudent=(Student *)malloc(sizeof(Student)))==NULL){
printf("Out Of Memory");
}
else{
newstudent->roll=r;
strcpy(newstudent->name,n);
newstudent->next=NULL;
}
return newstudent;
}
void push(int r,char n[],Stack *s){
Student *student=makestudent(r,n);
student->next=s->top;
s->top=student;
}
Student *pop(Stack *s){
Student *st=s->top;
s->top=s->top->next;
return st;
}
这里是一些使用静态数组后进先出堆栈的代码,没有使用指针。主要问题是你必须猜测这个堆栈有多大。如果你想在它变得太大时重新分配,你又回到了使用指针的方式。
#include <stdio.h>
#include <string.h>
#define MAXS 10 // stack size
typedef struct {
int roll;
char name[30];
} student;
student studs [MAXS];
int stacktop = 0;
int push(student *s) {
if (stacktop >= MAXS)
return 1; //mega fail
studs [stacktop++] = *s;
return 0;
}
int pop(student *s) {
if (stacktop <= 0)
return 1; //mega fail
*s = studs [--stacktop];
return 0;
}
int main(void) {
student entryput = {42, "Arthur Dent"};
student entryget;
if (push (&entryput))
return 1; // need to handle better than this
if (pop (&entryget))
return 1; // need to handle better than this
printf ("Name: %s, Roll: %d\n", entryget.name, entryget.roll);
return 0;
}
程序输出:
Name: Arthur Dent, Roll: 42
这是一个通过链表进行堆栈操作的简单程序,这是正式的方式。我理解指示堆栈顶部所需的指针和指向下一个堆栈的指针的概念node.But在这种情况下是否有必要制作结构变量的指针变量 struct student variable 即 typedef to Student 。我声明为 Student *(任何名称);.然后通过 malloc.I 动态分配内存可以改为使用全局变量as Student (anyname);and use is to fill information then push to stack.So 使用全局变量或指针有什么区别和优缺点。
关于我自己编写的程序,它运行良好。
typedef struct student{
int roll;
char name[30];
struct student *next;
}Student;
typedef struct stack{
Student *top;
}Stack;
Student *makestudent(int r,char n[]){
Student *newstudent;
if((newstudent=(Student *)malloc(sizeof(Student)))==NULL){
printf("Out Of Memory");
}
else{
newstudent->roll=r;
strcpy(newstudent->name,n);
newstudent->next=NULL;
}
return newstudent;
}
void push(int r,char n[],Stack *s){
Student *student=makestudent(r,n);
student->next=s->top;
s->top=student;
}
Student *pop(Stack *s){
Student *st=s->top;
s->top=s->top->next;
return st;
}
这里是一些使用静态数组后进先出堆栈的代码,没有使用指针。主要问题是你必须猜测这个堆栈有多大。如果你想在它变得太大时重新分配,你又回到了使用指针的方式。
#include <stdio.h>
#include <string.h>
#define MAXS 10 // stack size
typedef struct {
int roll;
char name[30];
} student;
student studs [MAXS];
int stacktop = 0;
int push(student *s) {
if (stacktop >= MAXS)
return 1; //mega fail
studs [stacktop++] = *s;
return 0;
}
int pop(student *s) {
if (stacktop <= 0)
return 1; //mega fail
*s = studs [--stacktop];
return 0;
}
int main(void) {
student entryput = {42, "Arthur Dent"};
student entryget;
if (push (&entryput))
return 1; // need to handle better than this
if (pop (&entryget))
return 1; // need to handle better than this
printf ("Name: %s, Roll: %d\n", entryget.name, entryget.roll);
return 0;
}
程序输出:
Name: Arthur Dent, Roll: 42