#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *pnext; } ; struct Node * creat_list(void); int main(int argc, char *argv[]) { struct Node * head =NULL; head = creat_list(void); //ou_list(head); return 0; } struct Node * creat_list(void) { struct Node *head=(struct Node *)malloc(sizeof(struct Node)); if(head==NULL) { printf("分配不成功,推出程序 "); exit(-1); } struct Node *q; q=head; q->pnext=NULL; int i; int len; int val; printf("几个节点?len="); scanf("%d",&len); for(i=0;i<len;i++) { printf("请输入第%d个节点的信息:",i+1); scanf("%d",&val); struct Node *new1=(struct Node*)malloc(sizeof(struct Node)); if(new1==NULL) { printf("分配不成功,推出程序 "); exit(-1); } new1->data =val; new1->pnext=NULL; q->pnext = new1; q=new1; } return head; }
c错误:
请找到定位
[Error] C:\Users\Administrator\Documents\C-Free\Temp\未命名1.cpp:14: error: expected primary-expression before "void"
解决方案:creat_list () 多写了 void 将其去掉;
完成解决后的完美体验:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *pnext;
} ;
struct Node * creat_list(void);
void ou_list(struct Node *phead);
int main(int argc, char *argv[])
{
struct Node * phead =NULL;
phead = creat_list();//这里如果编写成 creat_list(void) 会提示错误
//
ou_list(phead);
return 0;
}
struct Node * creat_list(void)
{
struct Node *head=(struct Node *)malloc(sizeof(struct Node));
struct Node *q;
if(head==NULL)
{
printf("分配不成功,推出程序 ");
exit(-1);
}
q=head;
// q->pnext=NULL;
int i;
int len;
int val;
printf("几个节点?len=");
scanf("%d",&len);
for(i=0;i<len;i++)
{
printf("请输入第%d个节点的信息:",i+1);
scanf("%d",&val);
struct Node *new1=(struct Node*)malloc(sizeof(struct Node));
if(new1==NULL)
{
printf("分配不成功,推出程序 ");
exit(-1);
}
new1->data =val;
new1->pnext=NULL;
q->pnext = new1;
q=new1;
}
return head;
}
bool is_empty(struct Node *phead)
{
if(phead ==NULL)
return true;
else
return false;
}
void ou_list(struct Node *phead)
{
if(is_empty(phead))
printf("空链表");
struct Node *q;
q=phead->pnext;
while(!is_empty(q))
// 这里是从首节点开始遍历,所以不能写成是 while(!is_empty(phead))
{
printf("%d\n",q->data);
q= q->pnext;
}
return ;
}
此程序如果用vs2010 编译的话,需要把 变量 放到最前面 相当于全局了 ,同样的指针,q需要用取地址符号来判断是否为空。
简单表示图
那么此上的链表是 具有 头结点的时候,不具有头结点的代码又该如何变幻嗯?
//有头结点的例子
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *pnext;
};
struct Node * creat_list(void);
void out_list(struct Node* p);
int main(int argc, char *argv[])
{
struct Node* head=creat_list();//=NULL;
// head = creat_list(void);
out_list(head);
return 0;
}
struct Node * creat_list(void)
{
struct Node *q;
int i;
int len;
int val;
struct Node *new1;
struct Node *head;//=(struct Node *)malloc(sizeof(struct Node));
/*
if(head==NULL)
{
printf("分配不成功,推出程序 ");
exit(-1);
}
q=head;
q->pnext=NULL;
*/
printf("几个节点?len=");
scanf("%d",&len);
for(i=0;i<len;i++)
{
printf("请输入第%d个节点的信息:",i+1);
scanf("%d",&val);
new1=(struct Node*)malloc(sizeof(struct Node));
if(new1==NULL)
{
printf("分配不成功,推出程序 ");
exit(-1);
}
new1->data =val;
new1->pnext=NULL;
if(i==0)
{
head=new1;
q=new1;
}
else
{
q->pnext = new1;
q=new1;
}
}
return head;
}
void out_list(struct Node* p)
{
//struct Node *q;
if(p==NULL)
printf("this list is null");
else
{// q= p->pnext;
while(p!=NULL)
{
printf("%3d\n",p->data);
p=p->pnext;
}
}
printf("\n");
}