C++建立循环链表,出错了

来源:百度知道 编辑:UC知道 时间:2024/05/28 03:37:48
#include<iostream.h>
#include<stdlib.h>

struct node{
int data;
node *prior;
node *next;
}*linklist;
void main(){
node *h;
h=(linklist)malloc(sizeof(node));
if(!h->data){
cout<<"111"<<endl;
}
h->prior=h;
h->next=h;
while(1){
int x;
cin>>x;
if(x!=-1){
node *p;
p=(linklist)malloc(sizeof(node));

p->data=x;
p->prior=h->prior;
p->next=h;
h->prior->next=p;
h->prior=p;
}
else
{
node *i;
i=h->next;
while(i->data){
cout<<i->data<<endl;
i=i->next;
}
break;
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Compiling...
exercise.cpp
C:\Documents and Settings\Administrator\exercise.cpp(12) : error C2146:

struct node{ //前面加上typedef
int data;
node *prior;
node *next;
}*linklist;
你原来那样只是定义了一个指针,并不是命名了一种类型
逻辑没看

呵呵 就你的代码看看这里:h=(node *)malloc(sizeof(node)); 和你的有什么不同?

#include<iostream.h>
#include<stdlib.h>

struct node
{
int data;
node *prior;
node *next;
}*linklist;

void main()
{
node *h;
h=(node *)malloc(sizeof(node));
if(!h->data)
{
cout<<"111"<<endl;
}
h->prior=h;
h->next=h;
while(1)
{
int x;
cin>>x;
if(x!=-1)
{
node *p;
p=(node *)malloc(sizeof(node));

p->data=x;
p->prior=h->prior;
p->next=h;
h->prior->next=p;
h->prior=p;
}
else
{
node *i;
i=h->next;
while(i->data)
{
cout<<i->data<<endl;
i=i->nex