注册 登录
编程论坛 C++教室

帮忙看看链表怎么只输出一个数,且有警告

weiyin868 发布于 2010-08-08 09:50, 445 次点击
#include<stdio.h>
#include<stdlib.h>
struct stu{
    int date;
    struct stu *next;
};
void main(void)
{
    struct stu *h,*s,*w;
    h=(struct stu*)malloc(sizeof(struct stu));
    s=(struct stu*)malloc(sizeof(struct stu));
    h->next=s;
    s->date=1;
    s=(struct stu*)malloc(sizeof(struct stu));
    s->date=2;
    s->next='\0';
    w=h->next;
    while((w->next)!='\0')
    {
      printf("%d",w->date);
      w=w->next;
    }
}
2 回复
#2
ciweitou1632010-08-08 10:08
试试这个,参照你的看看,是不是你需要的?
程序代码:
#include<iostream>
#include<cstdlib>
using namespace std;

struct stu{
    int date;
    struct stu *next;
};

int main(void)
{
    struct stu *h,*s,*w;
    h=(struct stu*)malloc(sizeof(struct stu));
    s=(struct stu*)malloc(sizeof(struct stu));
    w=(struct stu*)malloc(sizeof(struct stu));
    h->next=s;
    s->date=1;
    s->next=w;
    w->date=2;
    w->next='\0';
    //w=h->next;
    while((h->next)!='\0')
    {
      h=h->next;
      cout<<h->date<<endl;      
    }
    return 0;
}
#3
weiyin8682010-08-09 10:52
谢谢了,我想建一个工作指针,就是上面的s开辟新的节点啊,你那个如果链表很长,就要定义许多指针了,指向不同的节点。帮忙再解一下。
1