帮忙找找这个链表栈程序错在哪里(附程序)
我想把一行字符串装入链表栈中,可是感觉很对的一个程序不知道错在哪里了,问了几个同学,他们也不知道,大家帮忙找找吧,thank you!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node *next;
}Snode;
void initStack(Snode *s)
{
s = NULL;
}
void push(Snode *s, char x)
{
Snode *p;
p = (Snode *)malloc(sizeof(Snode));
p-> data = x;
p-> next = NULL;
if(s == NULL)
s = p;
else
{
p-> next = s;
s = p;
}
}
char pop(Snode *s)
{
char x;
Snode *p = s;
if(s == NULL)
return 0;
else
{
x = s-> data;
s = s-> next;
free(p);
return x;
}
}
int main()
{
Snode *s = (Snode *)malloc(sizeof(Snode));
char *link1, *link2;
int len, i;
link1 = (char *)malloc(sizeof(char));
link2 = (char *)malloc(sizeof(char));
initStack(s);
printf( "输入一行字符串: ");
scanf( "%s ", link1);
len = strlen(link1);
for(i = 0; i < len; i++)
push(s, link1[i]);
while(s != NULL)
{
printf( "%c ", s-> data);
s = s-> next;
}
}
[解决办法]
Snode *s = NULL;
void push(Snode*& s, char x);
最后动态内存清理