C语言中链栈 栈顶指针始终指向NULL

C语言中链栈 栈顶指针始终指向NULL,第1张

你的问题很简单,就是你没有区分清楚函数形参和实参之间传值和传址的区别。

#include <stdioh>

#include <malloch>

typedef struct node { int data;

struct node next;

} stacknode;

/

stacknode initnode()

{

stacknode top; //这声明的是个局部变量,你这里这样用是没有意义的,因为局部变量在函数结束后就释放了

top = NULL;

return top;

}

//修改为:/

void initnode(stacknode top)

{

top = NULL;

}

int emptynode(stacknode top) //判空

{

if (top == NULL)

return 1;

else

return 0;

//return top == NULL;//可写为一行代码

}

/从本质上来说,函数调用都是值传递的。这里,crnode(top)的实参top把top的值赋给了crnode函数里形参top的值,即相当于top(形参) = top(实参);注意,这里两个top是不一样的变量,两者的内存地址也不同,形参top是crnode函数里分配的内存,而实参top则是main函数里分配的内存。所以你crnode函数里去改变top的值当然就不会相对应的改变main函数里top的值/

/void crnode(stacknode top) //进栈

{

stacknode p; int i;

for (i = 0; i < 5; i++) {

p = (stacknode ) malloc(sizeof(stacknode));

printf("input data:\n");

scanf("%d", &p->data);

p->next = top;

top = p; //这样是无法改变主函数里top的值的,只是改变了该函数里top指针的值

}

}

//修改为:/

void crnode(stacknode top) //进栈,

{

stacknode p; int i;

for (i = 0; i < 5; i++) {

p = (stacknode ) malloc(sizeof(stacknode));

printf("input data:\n");

scanf("%d", &p->data);

p->next = top;

top = p;

}

}

int numnode(stacknode top) //计栈中结点数

{

stacknode p, s;

int i = 0; //i应该从0开始,这样当top为NULL时i才会为0

s = top;

while (s != NULL) {

p = s->next;

s = p;

i++;

}

return i;}

void outnode(stacknode top) //出栈

{

stacknode p;

int x;

while (top != NULL) {

x = top->data;

printf("%d\t", top->data);

p = top->next;

top = p;

//top = top->next;//上面两个代码写为一行就行了。。。

}

}

void main()

{

int i;

stacknode top;

initnode(&top);//top = initnode();

i = emptynode(top);

if (i == 1)

printf("栈空");

crnode(&top);//crnode(top);

i = emptynode(top);

if (i == 1)

printf("栈空");

printf("%d\n", numnode(top));

outnode(top);

i = emptynode(top);

if (i == 1)

printf("栈空");

}

//十进制转化成N进制的算法是

//整数部分为除N取余法,小数部分为乘N取整法

#include <stdioh>

#include <stdlibh>

#include<mathh>

double EPS=10e-8;

int dblcmp(double x)

{

if(fabs(x)<EPS)return 0;

return x<0-1:1;

}

void printfFloat(double x,int base)

{

if(dblcmp(x==0))return ;

printf("");

int tmp;

while(dblcmp(x))

{

x=base;

tmp=x;

x-=tmp;

printf("%d",tmp);

}

}

void printfInt(int n,int base)

{

if(n==0)return ;

printfInt(n/base,base);

printf("%d",n%base);

}

void change(double x,int base)

{

int tmp=x;

if(tmp==0)

{

printf("0");

}

else

{

printfInt(tmp,base);

}

printfFloat(x-tmp,base);

puts("");

}

int main()

{

double x;

while(scanf("%lf",&x)!=EOF)

{

printf("输出二进制:");

change(x,2);

printf("输出八进制:");

change(x,8);

printf("输出十六进制:");

change(x,16);

}

return 0;

}

#include <stdioh>

#include <malloch>

#include <conioh>

typedef struct pos   /描述迷宫当前位置和方向/

{

 int x;

 int y;

 int way;    /0:无效,1:左,2:下,3:右,4:上/

}P;

typedef struct LinkNode  /链表结构,用于栈的元素类型/

{

 P pos;

 struct LinkNode next;

}LinkNode;

typedef struct stack  /链式栈,用于存储迷宫路径信息/

{

 LinkNode head;   /头指针/

}Stack;

int row=0;     /迷宫的行数/

int line=0;     /迷宫的列数/

void InitStack(Stack s) /栈置空/

{

 s->head=NULL;

}

void Push(Stack s,P p)  /数据入栈/

{

 LinkNode LN=(LinkNode )malloc(sizeof(LinkNode));

 LN->pos=p;

 LN->next=s->head;

 s->head=LN;

}

  

P Pop(Stack s)    /栈顶元素出栈/

{

 P pos;

 LinkNode LN;

 LN=s->head;

 s->head=s->head->next;

 pos=LN->pos;

 delete LN;

 return pos;

}

P GetPop(Stack s)   /读取栈顶元素/

{

 return shead->pos;

}

int Empty(Stack s)   /判空/

{

 if(shead==NULL)

  return 1;

 else 

  return 0;

}

int InitMaze()   /返回迷宫的二维指针/

{

 int maze=NULL;

 int i;

 int j;

 printf("请输入迷宫的行跟列(x,y):"); 

 scanf("%d,%d",&row,&line);   /输入迷宫的行和列/

 maze=(int )malloc((row+2)sizeof(int )); 

 for(i=0;i<row+2;i++)  

 {

  maze[i]=(int )malloc(sizeof(int)(line+2));  

 }

 printf("请输入迷宫\n");    /输入迷宫,1代表路障,0代表通行/

 for(i=1;i<=row;i++)

  for(j=1;j<=line;j++)

   scanf("%d",&maze[i][j]);

 for(i=0;i<line+2;i++)    /将迷宫的四周都变为1/

  maze[0][i]=1;

 for(i=0;i<line+2;i++)

  maze[row+1][i]=1;

 for(i=0;i<row+2;i++)

  maze[i][0]=1;

 for(i=0;i<row+2;i++)

  maze[i][line+1]=1;

 for(i=0;i<row+2;i++)    /输出迷宫/

 {

  for(j=0;j<line+2;j++)

   printf("%3d",maze[i][j]);

  printf("\n");

 }

 return maze;

}

void PrintWay(Stack p)     /输出路径/

{

 P pos;

 Stack t;       /临时栈,用于存储从入口到出口的路径/

 LinkNode temp;

 int a,b;

 InitStack(&t);

 temp=(LinkNode )malloc(sizeof(LinkNode));

 temp->pos=Pop(&p);     /取栈顶元素,即出口/

 Push(&t,temp->pos);     /入栈/

 free(temp);

 while(!Empty(p))

 {

  temp=(LinkNode )malloc(sizeof(LinkNode));

  temp->pos=Pop(&p);    /获取下一个元素/

  a=GetPop(t)x-temp->posx;  /左:a=0,b=1;   下:a=1,b=0/

  b=GetPop(t)y-temp->posy;  /右:a=0,b=-1   上:a=-1,b=0;/

  if(b==1)

   temp->posway=1;

  else if(a==1)

   temp->posway=2;

  else if(b==-1)

   temp->posway=3;

  else if(a==-1)

   temp->posway=4;

  Push(&t,temp->pos);    /新位置入栈/

  free(temp);

 }

 printf("迷宫的路径为:\n");

 printf("前两个数字表示位置,最后一个数字表示方向\n");

 printf("1表示向左,2表示向下,3表示向右,4表示向上,0表示无方向\n");

 while(!Empty(t))     /输出路径/

 {

  pos=Pop(&t);

  printf("%3d,%3d,%3d\n",posx,posy,posway);

 }

}

int FindMaze(int maze,int r,int l) /寻找路径,找到返回1,没找到返回0/

{

 Stack p,q;       /栈p,q分别表示探索迷宫的路径和过程/

 P temp1,temp2;

 int a,b;

 InitStack(&p);

 InitStack(&q);

 temp1x=1;

 temp1y=1;       //

 maze[1][1]=-1;      /标记已经走过/

 Push(&p,temp1);

 Push(&q,temp1);

 while(!Empty(q))

 {

  temp2=GetPop(q);

  if(!(GetPop(p)x==GetPop(q)x

    &&GetPop(p)y==GetPop(q)y))  

   Push(&p,temp2);    /若有新元素入栈,就把q的栈顶元素存入p中/ 

  a=temp2x;

  b=temp2y+1;     /当前位置左方向相邻的方块/

  if(maze[a][b]==0)

  {

   temp1x=a;

   temp1y=b;

   maze[a][b]=-1;    /标记已走过/

   Push(&q,temp1);

  }

  if(a==r&&b==l)     /到达出口/

  { 

   temp1way=0;

   Push(&p,temp1);

   PrintWay(p);

   return 1;

  }

  a=temp2x+1;

  b=temp2y;     /当前位置下方向相邻的方块/

  if(maze[a][b]==0)

  {

   temp1x=a;

   temp1y=b;

   maze[a][b]=-1;    /标记已走过/

   Push(&q,temp1);

  }

  if(a==r&&b==l)     /到达出口/

  {

   temp1way=0;

   Push(&p,temp1);

   PrintWay(p);

   return 1;

  }

  a=temp2x;

  b=temp2y-1;     /当前位置右方向相邻的方块/

  if(maze[a][b]==0)

  {

   temp1x=a;

   temp1y=b;

   maze[a][b]=-1;    /标记已走过/

   Push(&q,temp1);

  }

  if(a==r&&b==l)     /到达出口/

  {

   temp1way=0;

   Push(&p,temp1);

   PrintWay(p);

   return 1;

  }

  a=temp2x-1;

  b=temp2y;     /当前位置上方向相邻的方块/

  if(maze[a][b]==0)

  {

   temp1x=a;

   temp1y=b;

   maze[a][b]=-1;    /标记已走过/

   Push(&q,temp1);

  }

  if(a==r&&b==l)     /到达出口/

  {

   temp1way=0;

   Push(&p,temp1);

   PrintWay(p);

   return 1;

  }

  if(GetPop(p)x==GetPop(q)x

     &&GetPop(p)y==GetPop(q)y)   /若四个方向都走不通,则数据出栈,退回一格/

  { 

   Pop(&p);

   Pop(&q);

  }

 }

 return 0;       /探索迷宫失败/

}

void main()

{

 int maze=InitMaze();    /初始化迷宫/

 if(FindMaze(maze,row,line))   /探索路径/

  printf("路径探索成功!\n");

 else

  printf("路径探索失败!\n");

 getch();

}

这些都是数据结构中的知识。堆栈的特征是先入后出,而不是队列先入先出。堆栈的顶部是最后一个推入的元素,是链的末端,堆栈的底部是第一个推入的元素,是链的末端。

在创建线程时,堆栈是内存中的一个快速空间,用于处理函数被调用时生成的临时变量,以及当前正在执行的函数(调用函数zhidao号)的地址。当被调用的函数在运行后返回时,程序继续从保存在那里的地址执行。

栈采用后进先出的数据存储方式。底部的堆栈栈存储变量的起始地址,和堆栈指针的地址指向当前的存储数据,当你推到堆栈数据,根据数据类型,字节的堆栈指针是上升的反应(如数据存储类型,移动第四节单词让),堆栈指针指向四个字节后的内存地址。

扩展资料:

事实上,链堆栈也是链表的一种形式。头指针总是指向表的第一个节点(或头节点),而顶部指针总是指向堆栈的顶部。在创建链表时,通常有两种插补方法:一种是头插补方法,另一种是尾插补方法。

链栈是相同的,假设所创建的栈没有头节点,即第一个节点开始存储数据,按照头节点插入的方法来构建栈,头指针是顶部指针,两者之间没有区别;当使用尾部插入方法构建堆栈时,头指针不是堆栈的顶部指针。

在这种情况下,应该定义一个尾部指针,以始终指向堆栈的最后一个元素(即要推入堆栈的最后一个元素),以便尾部指针是堆栈的顶部指针。

以上就是关于C语言中链栈 栈顶指针始终指向NULL全部的内容,包括:C语言中链栈 栈顶指针始终指向NULL、用数据结构(C语言版)中链栈和链队列,实现进制转换。 做的好的话多加分、数据结构迷宫问题,程序有错,请问下面的程序应该怎么修改,急!!!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10088043.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存