❶ 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)即可運行!