❶ c语言的链表问题
#include<stdio.h>
#include<malloc.h>
#include<string.h>//forstrlen()
structnode{
chardata;
structnode*link;
};
//structnode*head;
node*init(){
node*head=(structnode*)malloc(sizeof(structnode));
head->link=NULL;
returnhead;
}
node*creatList(node*head,chars[]){
inti=strlen(s)-1;
node*p=head;
while(i>=0){
p->link=(structnode*)malloc(sizeof(structnode));
p->link->data=s[i];
p=p->link;
--i;
}
p->link=NULL;
returnhead;
}
voidshow(node*head){
node*p=head->link;
while(p){
printf("%c",p->data);
p=p->link;
}
printf(" ");
}
intmain(void){
chars[256];
structnode*head=init();
printf("输入一个字符串:");
gets(s);
creatList(head,s);
show(head);
return0;
}
❷ c语言中链表的问题
////////////定义的数据//////////////////
typedef struct x
{
int num;
int age;
char name[20];
int sex :1; //你这是什么东西?
x *next; //你这是什么数据类型?
} DATA;
❸ C语言关于链表的问题
你写的这个程序太乱了,我看了半天,给你提几个建议:
1,string 是关键词,你应该知道,怎么可以用它来命名结构体呢?
2.你的链表很有问题,链表不是这样写的 ,对于链表,要在他的尾指针处加上NULL,这样可以防止内存泄漏,
3,程序要分层,比如,你对你的链表,你都没有办法检测这个链构建的对不对,应该在结构体后面至少分成两个函数,一个负责链表的初始化,另一个负责insert,但你把他们都放在一起了,这样对你定位错误很难,甚至都不知道是哪个出错了,
4,要尽量养成通过对函数的调用来完成任务,main函数只负责对函数的调用,这样你可以迅速定位问题,你可以想象像你这样写个几万行代码,如果出错的后果吗
我说的可能很直接,希望你不要介意,毕竟都是好话
❹ c语言中的链表问题
就是释放由head指针指向的一串链表的空间。
for循环中的意思就是当循环到链表末尾的时候循环结束;p=head->next
意思是用指针p来指向head的下一个节点,
head->next=head->next->next
意思是将head指针的位置移到下下个节点,最后free(p)就是释放p所指向的节点的空间。
❺ c语言程序链表问题
虽然题目一个链表只要3元素,但我不想把代码写死,修改常量可实现任意长度链表。
另外你强调不能用头结点,所以我用指向首节点的指针。(头结点只是方便定位链表,和首节点指针其实意义相同,否则你去哪找之前创建好的链表)
#include<stdio.h>
#include<malloc.h>
#defineLN3//最大链表元素数量,这里题目只要3个
typedefstructlist
{
intnum;
structlist*next;
}LIST;
LIST*creatList();
LIST*px(LIST*listP);
voidprintfLIST(LIST*listP);
LIST*cLists(LIST*listP1,LIST*listP2);
intmain()
{
LIST*listP1=NULL,*listP2=NULL;
printf("输入:
");
listP1=creatList();
px(listP1);
listP2=creatList();
px(listP2);
printf("输出:
");
printfLIST(listP1);
printfLIST(listP2);
printfLIST(cLists(listP1,listP2));
return0;
}
voidprintfLIST(LIST*listP)//打印链表
{
while(listP)
{
printf("%d",listP->num);
if(listP->next==NULL)
break;
listP=listP->next;
}
printf("
");
}
LIST*creatList()//输入创建单链表,返回首节点指针
{
inti=LN;
LIST*listP=NULL,*listTail=NULL;
while(i-->0)
{
LIST*listNew=(LIST*)malloc(sizeof(LIST));
listNew->next=NULL;
scanf("%d",&listNew->num);
if(listP==NULL)
listP=listNew;
else
listTail->next=listNew;
listTail=listNew;
}
returnlistP;
}
LIST*px(LIST*listP)//排序,返回首节点指针
{
LIST*listf=listP,*listn=NULL;
while(listf)
{
listn=listf->next;
while(listn)
{
if(listf->num>listn->num)
listf->num^=listn->num,listn->num^=listf->num,listf->num^=listn->num;
if(listn->next==NULL)
break;
listn=listn->next;
}
if(listf->next==NULL)
break;
listf=listf->next;
}
returnlistP;
}
LIST*cLists(LIST*listP1,LIST*listP2)//连接两个链表,返回首节点指针
{
LIST*listP3=listP1;
while(listP1)
{
if(listP1->next==NULL)
break;
listP1=listP1->next;
}
listP1->next=listP2;
returnlistP3;
}
❻ C语言链表问题
#include
"malloc.h"
struct
num
{
int
a;
struct
num
*next;
};
void
main()
{
int
i,n=0;
struct
num
*head,*p1,*p2;
head=(struct
num
*)
malloc(sizeof(struct
num));
p1=head;
/*p2=head;*/
for
(i=0;i<5;i++)
{
p2=(struct
num
*)
malloc(sizeof(struct
num));
scanf("%d",&n);
p2->a=n;
p1->next=p2;
p1=p2;
}
p1->next=NULL;
p1=head->next;
while(p1!=NULL)
{
printf("%d
",p1->a);
p1=p1->next;
}
printf("\n");
}
***********************************************************
#include
"malloc.h"
struct
num
{
int
a;
struct
num
*next;
};
void
main()
{
int
i,n=0;
struct
num
*head,*p1,*p2;
head=(struct
num
*)
malloc(sizeof(struct
num));
p1=head;
/*p2=head;*/
for
(i=0;i<5;i++)
{
p2=(struct
num
*)
malloc(sizeof(struct
num));
scanf("%d",&(p2->a));
p1->next=p2;
p1=p2;
}
p1->next=NULL;
p1=head->next;
while(p1!=NULL)
{
printf("%d
",p1->a);
p1=p1->next;
}
printf("\n");
}
{
http://chinastar.uueasy.com/read-htm-tid-292-fpage-6.html}
可以看一下这篇文章,链表创建输出
❼ c语言链表问题,求高手解答!
1、函数调用,直接写函数名就行了。函数的名字就是creat,struct student *只是返回值的类型,不需要写的。
head = creat(N);
直接按照上面的写法就可以了。
2、加*号只是说明creat返回的是struct student *类型,不加*号就是返回struct student类型。根据函数的功能,决定函数的返回值就行啦。
3、这个Node就是struct student类型的一个全局变量而已。
如果这么写typedef struct student Node,Node就是struct student类型的意思,代码中的Node就相当于struct student的意思,是结构体的另一个名字,而不是变量。
❽ c语言链表问题
两个程序功能是一样的,但是,第一个程序的main()函数放在自定义函数前,而且没有在main()函数前定义自定义函数;导致整个程序不知道有自定义函数NODE *create(int len);的存在;
当然,自定义函数放在main()函数前是不需要定义自定义函数的。
所以,第一个程序不可以运行,第二个程序是可以运行的;
第一个程序想要运行就把main函数放到自定义函数后面,或者在main函数前定义自定义函数NODE *create(int len)即可运行!