#include
#include
typedef struct node //定义结构体
{
int data;
struct node *next; //指向下一个结构体的指针
}STU;
STU* Create(STU *a, int num) //定义create函数返回类型
//为指向构体的指针
{
STU *p, *s;
a = (STU*)malloc(sizeof(STU)); //创建上面定义的结构体
////待会好返回
printf("请输入值:");
scanf("%d",&a->data); //输入数据,下面都差不多
//就是对其进行初始化
a->next = NULL;
p = a;
while(num > 1)
{
s = (STU *)malloc(sizeof(STU));
printf("请输入值:");
scanf("%d",&s->data);
p->next = s;
s->next = NULL;
p = s;
num--;
}
return a;
}
int liulan(STU *a)
{
STU *h;
int i = 0;
h = a;
while(1) ///输出所有数据
{
i ++;
printf("%d ",h->data);
h = h->next;
if(h == NULL)break;
}
printf("\n");
return i;
}
STU* insert(STU *s, int w)
{
STU *p, *h;
p = s;
int a = 1;
while(1)
{
if(a == w)break;
p = p->next;
a++;
}
h = (STU *)malloc(sizeof(STU));
printf("请输入你要插入的元素:");
scanf("%d",&h->data);
h->next = p->next;
p->next = h;
return s;
}
void main()
{
STU *h;
int i;
printf("输入创建的个数");
scanf("%d",&i);
STU *s = Create(h, i);
printf("\n");
printf("链表的长度为:%d\n",liulan(s));
printf("输入你要插入的位置:");
scanf("%d",&i);
s = insert(s, i);
printf("链表的长度为:%d\n",liulan(s));
}
该程序很简单
就用了一个单链表
看不懂
可能是你没学数据结构
有时间看看,现在有点事没时间。
这个程序为啥编译不了啊
:\program files\microsoft visual studio\myprojects\asd\saf.c(49) : error C2143: syntax error : missing ';' before 'type'
d:\program files\microsoft visual studio\myprojects\asd\saf.c(52) : error C2065: 'a' : undeclared identifier
d:\program files\microsoft visual studio\myprojects\asd\saf.c(70) : error C2275: 'STU' : illegal use of this type as an expression
d:\program files\microsoft visual studio\myprojects\asd\saf.c(8) : see declaration of 'STU'
d:\program files\microsoft visual studio\myprojects\asd\saf.c(70) : error C2065: 's' : undeclared identifier
d:\program files\microsoft visual studio\myprojects\asd\saf.c(72) : warning C4047: 'function' : 'struct node *' differs in levels of indirection from 'int '
d:\program files\microsoft visual studio\myprojects\asd\saf.c(72) : warning C4024: 'liulan' : different types for formal and actual parameter 1
d:\program files\microsoft visual studio\myprojects\asd\saf.c(75) : warning C4047: 'function' : 'struct node *' differs in levels of indirection from 'int '
d:\program files\microsoft visual studio\myprojects\asd\saf.c(75) : warning C4024: 'insert' : different types for formal and actual parameter 1
d:\program files\microsoft visual studio\myprojects\asd\saf.c(75) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct node *'
d:\program files\microsoft visual studio\myprojects\asd\saf.c(76) : warning C4047: 'function' : 'struct node *' differs in levels of indirection from 'int '
d:\program files\microsoft visual studio\myprojects\asd\saf.c(76) : warning C4024: 'liulan' : different types for formal and actual parameter 1
执行 cl.exe 时出错.
我看了这个程序。这是数据结构的链表那一节的内容吧。
这个程序的三个子函数分别完成 创建链表,输出链表中的数据,在链表中插入一个数据
说实话,注释并不难。但是即使我们给你写了注释你也未必能完全理解。这里面涉及到的指针方面比较复杂。建议你翻看一下c语言指针那一章,如果那一章你完全弄懂,这个不在话下。否则,你怎样都是隔岸观火,不得其法。