单向链表问题
c语言吧
全部回复
仅看楼主
level 8
黑緢灬 楼主
用单链表模拟的堆栈……也许挫了点
这是结构体
struct letter_{
char save;
struct letter_ *last_ptr;
};
typedef struct letter_ stack;
//为新节点分配内存并把数据扔进去
int AddStack(stack *sptr,char data)
{
stack *temp = NULL;
if((temp=(stack*)malloc(sizeof(stack)))==NULL)
{
puts("Error to deliver space to the new element of the stack.");
return 0;
}
temp->save = data;
temp->last_ptr = sptr;
sptr = temp;
return 1;
}
//检查链表有多少个节点
int CheckStack(stack *sptr)
{
stack *t_sptr = sptr;
int number;
//for这里条件设置得有点复杂……凑合一下吧
for(number = 0;t_sptr->last_ptr!=NULL;number++)
{
#ifdef check
puts("For loop are doing.");
#endif
t_sptr = t_sptr->last_ptr;
}
return number;
}
然后运行时执行CheckStack总是返回0
是Add那里就已经写错了还是Check的判断出现了逻辑错误?
找了2天 木有找到 =。=

2012年03月10日 04点03分 1
level 8
黑緢灬 楼主
//再贴一段执行代码……恩 OutStack是出栈函数 由于Check一直为0所以无法执行OutStack
所以那段函数代码就不贴出来了=。=
int main(int argc, char* argv[])
{
stack head = {'\0',NULL};
stack *bottom = &head;
char temp;
puts("Enter the string.");
while((temp=getchar())!=EOF)
{
if(temp=='\0') temp = '.';
AddStack(bottom,temp);
}
printf("Now,the stack has %d letters.\n",CheckStack(bottom));
while(CheckStack(bottom))
{
putchar(OutStack(bottom));
}
return 0;
}
2012年03月10日 04点03分 2
level 11
不应该是temp->last_ptr=NULL;吗?
2012年03月10日 05点03分 3
level 13
sptr = temp;这句搞神马?
2012年03月10日 05点03分 4
level 13
stack *AddStack(stack *sptr,char data){
stack *temp = (stack*)malloc(sizeof(stack));
if(temp){temp->save = data;temp->last_ptr = sptr;}
return temp;
}
2012年03月10日 05点03分 5
level 2
stack *sptr AddStack(stack *sptr,char data)
{
stack *temp = NULL;
if((temp=(stack*)malloc(sizeof(stack)))==NULL)
{
puts("Error to deliver space to the new element of the stack.");
return 0;
}
temp->save = data;
temp->last_ptr = sptr;
sptr = temp;
return sptr;
}
看上去像是没有返回任何指针。对栈不是了解,所以就无能为力了。

2012年03月10日 05点03分 6
level 13
另外当前栈指针一般是top而不是bottom
2012年03月10日 05点03分 7
level 12
每次添加节点都加在同一个节点前面 然后检查是检查这个节点后面
你可以试试
stack* AddStack(stack *sptr,char data)
{
stack *temp = NULL;
if((temp=(stack*)malloc(sizeof(stack)))==NULL)
{
puts("Error to deliver space to the new element of the stack.");
return 0;
}
temp->save = data;
temp->last_ptr = sptr;
return temp;
}
然后在main函数里面
while((temp=getchar())!=EOF)
{
if(temp=='\0') temp = '.';
bottom=AddStack(bottom,temp);
}
应该可以 你试试
2012年03月10日 05点03分 8
level 13
NC中,干脆直接发一个完整的抽象
#include <stdio.h>
#include <stdlib.h>
typedef struct node_s{struct node_s*last_ptr;char save;}node_t;
typedef struct stack_s{struct node_s*top;}stack_t;
int AddStack(stack_t *sptr,char data){
    if(!sptr)return 0;
    node_t *temp = (node_t*)malloc(sizeof(node_t));
    if(temp){
        temp->save = data;
        temp->last_ptr = sptr->top;
        sptr->top = temp;
        return 1;
    }else return 0;
}
int PopStack(stack_t *sptr){
    if(!sptr)return 0;
    node_t *temp = sptr->top;
    if(temp){
        sptr->top=temp->last_ptr;
        free(temp);
        return 1;
    }else return 0;
}
int OutStack(stack_t *sptr){
    if(!sptr||!sptr->top)return 0;
    char temp=sptr->top->save;
    PopStack(sptr);
    return temp;
}
int CheckStack(stack_t stack){
    int cnt=0;
    for(;stack.top;++cnt,stack.top=stack.top->last_ptr);
    return cnt;
}
int main(int argc, char* argv[]){
    stack_t stack={0};
    
    char temp;
    puts("Enter the string.");
    while((temp=getchar())!=EOF){
        if(temp=='\0') temp = '.';
        AddStack(&stack,temp);
    }
    printf("Now,the stack has %d letters.\n",CheckStack(stack));
    while(CheckStack(stack)){
        putchar(OutStack(&stack));
    }
    return 0;
}
2012年03月10日 05点03分 9
level 13
while(stack.top){
putchar(stack.top->save);
PopStack(&stack);
}
2012年03月10日 05点03分 10
level 8
黑緢灬 楼主
[揉脸]语义问题就表纠结了
貌似是我对结构体指针理解有错误
sptr = temp;
本意是想将sptr指向temp所指的内存位置
……这么做是不可以的嘛?[不要]
2012年03月10日 09点03分 12
level 8
黑緢灬 楼主
[拍砖]终于解决了……程序搞定……又学到了不少东西
2012年03月10日 10点03分 13
1