解释上面代码void initStack(struct node **top) { *top = NULL; }
时间: 2023-12-22 20:06:28 浏览: 15
这段代码定义了一个名为initStack的函数,函数的参数是一个指向指针的指针top,指针top指向栈顶元素所在的节点。函数的返回值类型为void,即无返回值。
函数体内的语句*top = NULL; 的作用是将指针top指向的节点设置为NULL,即表示栈为空。这个函数的作用是初始化一个栈,使其为空栈。
相关问题
请找出下列代码的问题并解决:#include<iostream> using namespace std; #define Maxsize 100 typedef struct node//二叉树结构 { char data; struct node* lchild; struct node* rchild; }BTnode; void CreateNode(BTnode*& bt) { char h; h = getchar(); if (h != '#') { bt = (BTnode*)malloc(sizeof(BTnode)); bt->data = h; CreateNode(bt->lchild); CreateNode(bt->rchild); } else bt = NULL; } void DestoryNode(BTnode*& bt) { if (bt != NULL) { DestoryNode(bt->lchild); DestoryNode(bt->rchild); free(bt); } } typedef struct//顺序队列 { BTnode* data[Maxsize]; int front; int rear; }SqQueue; typedef struct//顺序栈 { BTnode* data[Maxsize]; int top; }SqStack; void InitQueue(SqQueue*& q) { q = (SqQueue*)malloc(sizeof(SqQueue)); q->front = q->rear = -1; } void InitStack(SqStack*& s) { s = (SqStack*)malloc(sizeof(SqStack)); s->top = -1; } void DestoryQueue(SqQueue*& q) { free(q); } void DestoryStack(SqStack*& s) { free(s); } bool QueueEmpty(SqQueue* q) { return(q->front == q->rear); } bool StackEmpty(SqStack* s) { return(s->top == -1); } bool enQueue(SqQueue*& q, BTnode*& node) { if (q->rear == Maxsize - 1) return 0; q->rear++; q->data[q->rear] = node; return 1; } bool Push(SqStack*& s, BTnode*& node) { if (s->top == Maxsize - 1) return 0; s->top++; s->data[s->top] = node; return 1; } bool deQueue(SqQueue*& q, BTnode*& node) { if (q->front == q->rear) return 0; q->front++; node = q->data[q->front]; return 1; } bool Pop(SqStack*& s) { if (s->top == -1) return 0; cout << s->data[s->top] << "\t"; s->top--; return 1; } void LevelOrder(BTnode* bt) { BTnode* p; p = new BTnode; SqQueue* qu; SqStack* st; InitStack(st); InitQueue(qu); enQueue(qu, bt); while (!QueueEmpty(qu)) { deQueue(qu, p); Push(st, bt); if (p->lchild != NULL) enQueue(qu, p->lchild); if (p->rchild != NULL) enQueue(qu, p->rchild); } cout << "二叉树的自下而上,从右到左的层次遍历结果:" << endl; if (!StackEmpty(st)) Pop(st); DestoryQueue(qu); DestoryNode(p); DestoryStack(st); } int main() { BTnode* B; cout << "输入二叉树:" << endl; CreateNode(B); LevelOrder(B); DestoryNode(B); return 0; }
代码存在以下问题:
1. 在LevelOrder函数中,创建了一个新的BTnode指针p,但没有初始化,可能会引起未知的错误。
2. 在LevelOrder函数中,将二叉树节点指针p入栈时,使用了错误的变量bt,应该使用p。
3. 在LevelOrder函数中,调用Pop函数时,打印的是栈顶元素的data值,但实际上应该打印的是指针p指向的节点的data值。
修改后的代码如下:
```
#include<iostream>
using namespace std;
#define Maxsize 100
typedef struct node//二叉树结构
{
char data;
struct node* lchild;
struct node* rchild;
}BTnode;
void CreateNode(BTnode*& bt)
{
char h;
h = getchar();
if (h != '#')
{
bt = (BTnode*)malloc(sizeof(BTnode));
bt->data = h;
CreateNode(bt->lchild);
CreateNode(bt->rchild);
}
else bt = NULL;
}
void DestoryNode(BTnode*& bt)
{
if (bt != NULL)
{
DestoryNode(bt->lchild);
DestoryNode(bt->rchild);
free(bt);
}
}
typedef struct//顺序队列
{
BTnode* data[Maxsize];
int front;
int rear;
}SqQueue;
typedef struct//顺序栈
{
BTnode* data[Maxsize];
int top;
}SqStack;
void InitQueue(SqQueue*& q)
{
q = (SqQueue*)malloc(sizeof(SqQueue));
q->front = q->rear = -1;
}
void InitStack(SqStack*& s)
{
s = (SqStack*)malloc(sizeof(SqStack));
s->top = -1;
}
void DestoryQueue(SqQueue*& q)
{
free(q);
}
void DestoryStack(SqStack*& s)
{
free(s);
}
bool QueueEmpty(SqQueue* q)
{
return(q->front == q->rear);
}
bool StackEmpty(SqStack* s)
{
return(s->top == -1);
}
bool enQueue(SqQueue*& q, BTnode*& node)
{
if (q->rear == Maxsize - 1)
return 0;
q->rear++;
q->data[q->rear] = node;
return 1;
}
bool Push(SqStack*& s, BTnode*& node)
{
if (s->top == Maxsize - 1)
return 0;
s->top++;
s->data[s->top] = node;
return 1;
}
bool deQueue(SqQueue*& q, BTnode*& node)
{
if (q->front == q->rear)
return 0;
q->front++;
node = q->data[q->front];
return 1;
}
bool Pop(SqStack*& s)
{
if (s->top == -1)
return 0;
cout << s->data[s->top]->data << "\t";
s->top--;
return 1;
}
void LevelOrder(BTnode* bt)
{
BTnode* p = NULL;
SqQueue* qu;
SqStack* st;
InitStack(st);
InitQueue(qu);
enQueue(qu, bt);
while (!QueueEmpty(qu))
{
deQueue(qu, p);
Push(st, p);
if (p->lchild != NULL)
enQueue(qu, p->lchild);
if (p->rchild != NULL)
enQueue(qu, p->rchild);
}
cout << "二叉树的自下而上,从右到左的层次遍历结果:" << endl;
while (Pop(st));
DestoryQueue(qu);
DestoryStack(st);
}
int main()
{
BTnode* B;
cout << "输入二叉树:" << endl;
CreateNode(B);
LevelOrder(B);
DestoryNode(B);
return 0;
}
```
#include<iostream> #include<stdlib.h> #define MAXSIZE 100 using namespace std; typedef struct Node{ char data; struct Node *LChild; struct Node *RChild; }BiNode,*BiTree; typedef struct SNode{ BiTree elem[MAXSIZE]; int top; }SeqStack; void InitStack(SeqStack*S){ S->top==-1; } int Push(SeqStack *S,BiTree &p){ if(S->top==MAXSIZE-1){ cout<<"栈满" ; return 1; } else{ S->elem[++S->top]=p; return 0; } } int Pop(SeqStack *S,BiTree &p){ if(S->top==-1){ cout<<"栈空"; return 1; } else{ p=S->elem[S->top]; S->top--; return 0; } }BiTree CreateTree(){ BiTree root; char ch; scanf("%c",&ch); if(ch=='#'){ return NULL; }else{ root=(BiTree)malloc(sizeof(BiNode)); if(root==NULL){ cout<<"创建失败"; return NULL; }root->data=ch; root->LChild=CreateTree(); root->RChild=CreateTree(); return root; } }void PreOrder2 (BiTree T){ BiTree p=T; SeqStack s; InitStack(&s); while(p!=NULL) {cout<<p->data;//访问当前节点 Push(&s,p); if(p->LChild!=NULL){ p=p->LChild; } else if(s.top!=-1) { while(s.top!=-1){ Pop(&s,p); p=p->RChild; if(p!=NULL)break; } } else p=NULL;//无后继 } } int main(){ BiTree T=CreateTree(); PreOrder2(T);cout<<endl; return 0; }为什么输出不全
经过分析,程序的问题在于在 `InitStack` 函数中没有正确初始化栈顶指针 `top`,应该使用赋值运算符 `=` 而不是比较运算符 `==`。正确的代码如下:
```
void InitStack(SeqStack *S) {
S->top = -1; // 使用赋值运算符初始化栈顶指针
}
```
此外,程序还存在一个潜在的问题,即当输入的表达式中包含空格或其他非法字符时,程序会陷入死循环,因此需要对输入进行处理,只读取有效的字符,忽略空格和其他非法字符。可以使用 `cin.get()` 替换 `scanf("%c",&ch)` 来实现这一功能,代码如下:
```
char ch;
cin.get(ch);
while (ch == ' ' || ch == '\n' || ch == '\r') { // 忽略空格和其他非法字符
cin.get(ch);
}
if (ch == '#') {
return NULL;
} else {
BiTree root = (BiTree)malloc(sizeof(BiNode));
if (root == NULL) {
cout << "创建失败";
return NULL;
}
root->data = ch;
root->LChild = CreateTree();
root->RChild = CreateTree();
return root;
}
```
经过上述修改,程序应该就可以正确输出了。