链栈(问题程序)
我做的一个链栈,不知道哪出毛病了
显示会出问题,可能是输入或是函数体没有带出我定义的结构体,帮我修改下~~万分感谢哦
/*operate_lnode.h*/
typedef struct lnode{
int data;
struct lnode *next;
}L_node;
/*====================================*/
/*输入栈 */
/*====================================*/
void Insert_node(L_node **node, int scan_max)
{
int i,scan_x;
L_node *temp;
temp=(L_node *)malloc(sizeof(L_node));
if(temp!=NULL)
{
for(i=0; i<scan_max; i++)
{
printf("元素%d=",i+1);
scanf("%d",&scan_x);
temp->data=scan_x;
temp->next=(*node);
(*node)=temp;
}
}
}
/*====================================*/
/*显示栈 */
/*====================================*/
void Print_node(L_node *node)
{
//L_node *Strat=(*node);
if(node==NULL)
printf("em");
else
{
while(node->next!=NULL)
{
printf("%d",node->data);
node=node->next;
}
printf("%d",node->data);
}
}
/*main.c*/
#include <stdio.h>
#include <stdlib.h>
#include "operate_lnode.h"
main()
{
int scan_switch,scan_max;
L_node *top=NULL;
while(1)
{
printf("\n1.\n");
printf("2.\n");
printf("3.\n");
printf("4.\n");
printf("选择操作码:");
scanf("%d",&scan_switch);
switch(scan_switch)
{
case 1:
{
printf("输入元素的个数:");
scanf("%d",&scan_max);
Insert_node(&top,scan_max);
}break;
case 2:Print_node(top);break;
}
}
}
[解决办法]
/*operate_lnode.h*/
typedef struct lnode{
int data;
struct lnode *next;
}L_node;
/*====================================*/
/*输入栈 */
/*====================================*/
void Insert_node(L_node **node, int scan_max)
{
int i,scan_x;
L_node *temp;
temp=(L_node *)malloc(sizeof(L_node));
if(temp!=NULL)
{
for(i=0; i <scan_max; i++)
{
printf("元素%d=",i+1);
scanf("%d",&scan_x);
temp->data=scan_x;
temp->next=(*node);
(*node)=temp;
temp=(L_node *)malloc(sizeof(L_node)); ---->add
}
}
}
/*====================================*/
/*显示栈 */
/*====================================*/
void Print_node(L_node *node)
{
//L_node *Strat=(*node);
if(node==NULL)
printf("em");
else
{
while(node->next != NULL)
{
printf("%d", node->data);
node = node->next;
}
printf("%d", node->data);
}
}
/*main.c*/
#include <stdio.h>
#include <stdlib.h>
#include "operate_lnode.h"
int main()
{
int scan_switch,scan_max;
L_node *top=NULL;
while(1)
{
printf("\n1.\n");
printf("2.\n");
printf("3.\n");
printf("4.\n");
printf("选择操作码:");
scanf("%d",&scan_switch);
switch(scan_switch)
{
case 1:
{
printf("输入元素的个数:");
scanf("%d",&scan_max);
Insert_node(&top,scan_max);
break;
}
case 2:
Print_node(top);
//break;
return 0;
}
}
}