帮帮忙,感激不尽!!!
[code=C/C++][/code]#include "StdAfx.h"
#include<iostream>
using namespace std;
#define null 0
typedef struct lian{
int data;
struct lian *next;
}*nodetype;
void main(){
nodetype *create();
int d;
nodetype *h=null;
nodetype *s;
nodetype *t;
int i=1;
printf("建立一个链表:\n");
while(1)
{
printf("输入第i各节点的data域值:\n");
scanf("%d",&d);
if(d==0) break;
if(i==1)
{h=(nodetype*)malloc(sizeof(nodetype));
h->data=d;
h->next=null;
t=h;
}
else
{ s=(nodetype*)malloc(sizeof(nodetype));
s->data=d;
s->next=null;
t->next=s;
}
i=i+1;
return h;
}
}
麻烦各位指出问题
[解决办法]
(nodetype*)malloc(sizeof(nodetype));
这句不对,nodetype只是一个结构指针啊,应该申请结构那么大小的空间
[解决办法]
初学者吧,能理解你的困惑。现在旁边没好一点的琏表的程序,先看看我这个代码吧,或许能帮助你。
- C/C++ code
#include<iostream>using namespace std;typedef char etype;typedef struct list{ etype data; list *link;};void createList(list* h){//创建琏表 list *p,*t; etype c; t=h; cout<<"Input nodes' value, end with '#':\n"; while (cin>>c && c!='#'){ p=new list; p->data=c; p->link=NULL; t->link=p; t=t->link; } cout<<"Create List Ended!\n"; return ;}void display(list *h){//输出琏表 list* t; t=h->link; cout<<"The List:"; while (t){ cout<<t->data<<" "; t=t->link; } cout<<endl; return ;}void deleteNode(list *h){//删除结点 list* t,*p; char c; t=h; cout<<"Input the element want to delete:"; cin>>c; if(!t){ cout<<"Empty list!\n"; return ; } while (t->link){ if(t->link->data==c){ p=t->link; t->link=t->link->link; delete p; cout<<"Delete "<<c<<" succssed!\n"; return ; } t=t->link; } cout<<"There may hasn't "<<c<<" in the list!\n"; return ;} void test(list *h){ list* t,*p; t=new list; h->link=t; //p=new list; t->data='a'; t->link=NULL; //t=p; //t=t; p=new list; p->data='b'; p->link=NULL; t->link=p;} int main(){ list *head; head=new list; head->link=NULL; //createList(head); test(head); display(head); /*while(1){ deleteNode(head); display(head); }*/ system("pause"); return 0;}