本文目錄一覽:
- 1、請幫我解決在c語言鏈表中代碼:return head; 有什麼作用!
- 2、c語言鏈表插入一個新節點的函數問題
- 3、C語言鏈表問題
- 4、c語言程序設計 實現一個鏈表的插入,刪除,輸出 結果這個代碼輸出時老是出錯,求幫忙
- 5、剛入c語言 有沒有大佬幫我看看這咋寫
請幫我解決在c語言鏈表中代碼:return head; 有什麼作用!
就是你的鏈表的表頭了,鏈表好比一條繩索,這是繩索的一頭,你只有找到這一頭才能繼續往下面查找其他的,好比順藤摸瓜。。。。。
c語言鏈表插入一個新節點的函數問題
首先,主函數中,“請輸入插入的數據”那裡scanf應該是b,這是引發崩潰的原因。
其次,insert函數的目的應該是想插入數據後仍是有序鏈表。但你的insert函數邏輯太亂,有些不必要的判斷,我修正了你的代碼,貼給你看看。(雖然你insert是想保證有序,但你在創建的時候沒有保證有序,所以最終結果不一定是有序。例如,創建 1,5,2,插入3,最後輸出的是 1,3,5,2)
代碼修改:
scanf(“%d”, b);
重寫了insert函數,簡化邏輯;
動態分配的內存記得釋放,增加freeNode釋放空間
#include stdio.h
#include stdlib.h
struct link
{
int data;
struct link *next;
};
struct link *add(struct link *head);//創建鏈表
void display(struct link *head);//輸出數據
struct link *insert(struct link *head, int b); //插入新節點
void freeNode(struct link *); //釋放空間
int main()
{
char c;
struct link *head = NULL;
printf(“要創建一個鏈表嗎?”);
scanf(” %c”, c);
while (c == ‘y’ || c == ‘Y’)
{
head = add(head);
printf(“要繼續創建節點嗎?”);
scanf(” %c”, c);
}
display(head);
int b;
printf(“輸入插入的數據”);
scanf(“%d”, b);
head = insert(head, b);
display(head);
freeNode(head);
}
struct link *add(struct link *head)
{
int data;
struct link *p = (struct link*)malloc(sizeof(struct link));
if (head == NULL)
{
head = p;
}
else
{
struct link *pr = head;//一個臨時指針pr先保存下head的地址
while (pr-next != NULL)
{
pr = pr-next;
}
pr-next = p;
}
printf(“輸入數據”);
scanf(“%d”, p-data);
p-next = NULL;
return head;
}
void display(struct link *head)
{
struct link *pr = head;
while (pr != NULL)
{
printf(“%d\n”, pr-data);
pr = pr-next;
}
}
struct link *insert(struct link *head, int b)
{
struct link *ptr = head, *prev = head;
struct link *newNode = (struct link *)malloc(sizeof(struct link));
newNode-data = b;
while (ptr b ptr-data) {
prev = ptr;
ptr = ptr-next;
}
newNode-next = ptr;
if (ptr == head) head = newNode;
else prev-next = newNode;
return head;
}
void freeNode(struct link *node) {
if (!node) return;
freeNode(node-next);
free(node);
}
C語言鏈表問題
head-next=NULL; /*頭結點next指針置為空*/
tail=head; /*開始時尾結點指向頭結點*/
這個是你開始的時候建設的head指針對吧,它的實際內容裡面沒有數據,只有他的next指向一個NULL,之後你不斷添加元素的時候,這個head裡面數據始終是空的,所以下面用了這幾句話來刪除這個空的頭節點:
pNewElement=head;
head=head-next; /*修正頭指針的位置*/
free(pNewElement); /*釋放資源*/
return head;
這幾句話的作用也就是把head往後移動一個節點(刪除你產生鏈表時候的頭結點),這樣的話,返回的鏈表的頭節點的元素就是有學生信息的,希望你看懂了。
c語言程序設計 實現一個鏈表的插入,刪除,輸出 結果這個代碼輸出時老是出錯,求幫忙
按照閣下的代碼,我又寫了一遍
直接就可以用
#includestdio.h
#includestdlib.h
typedef struct listnode
{
int data;
struct listnode *next;
}
linknode,*linklist;
/***************
* 建立鏈表
***************/
linklist creatlist(int length)
{
int number;
linklist head = (listnode *) malloc(sizeof(listnode));
listnode *p = head;
printf(“請輸入鏈表中的元素:\n”);
for(int i = 0; i length; i++)
{
scanf(“%d”,number);
p-data = number;
p-next = (listnode *) malloc(sizeof(listnode));
p = p-next;
}
return head;
}
/***************
* 插入
***************/
linklist insertlist(linklist head, int pos)
{
int n;
listnode *q = (linklist) malloc(sizeof(linknode));
listnode *p = head;
printf(“\n請輸入插入的數值:\t”);
scanf(“%d”,n);
for(int i = 0; i pos-1; i++)
p = p-next;
q-data = n;
q-next = p-next;
p-next = q;
return head;
}
/***************
* 刪除
***************/
linklist deletelist(linklist head, int pos)
{
listnode *p = head,*q;
for(int i = 0; i pos-2; i++)
p = p-next;
q = p-next;
p-next = q-next;
free(q);
return head;
}
/***************
* 輸出
***************/
void output(linklist head)
{
linklist p = head;
while(p!=NULL)
{
printf(“%d “,p-data);
p = p-next;
}
}
/***************
* 主函數
***************/
int main()
{
linklist head;
int l,a,b;
printf(“請輸入鏈表中元素的個數:\t”);
scanf(“%d”,l);
head=creatlist(l);
printf(“\n輸入要插入元素的位置:\t”);
scanf(“%d”,a);
if(a 1 || a l)
printf(“error”);
else
{
insertlist(head, a);
printf(“\n插入後的新鏈表是:\t”);
output(head);
}
printf(“\n輸入要刪除元素的位置:\t”);
scanf(“%d”,b);
if(b 1 || a l)
printf(“error”);
else
{
printf(“\n刪除後的新鏈表是:\t”);
deletelist(head,b);
output(head);
}
return 0;
}
剛入c語言 有沒有大佬幫我看看這咋寫
#includestdio.h
#includestdlib.h
#includeconio.h
/*定義結構體*/
struct student
{
int num;
float score;
struct student *next;
};
/*創建一個只有頭結點的空鏈表*/
struct student *create_head()
{
struct student *head;
head=(struct student*)malloc(sizeof (struct student) );
if(head==NULL) //小心別漏這個
{
printf(“申請頭結點失敗!\n”);
return NULL;
}
head-next=NULL;
return head;
}
/*將s指向的結點插入鏈表,使鏈表保持升序,並返回頭結點*/
struct student *insert(struct student *head,struct student *s)
{
struct student *p=head;
while(p-next!=NULLs-scorep-next-score)//特別注意左右不能寫反,若s最大,最後p-next=NULL,p-next-score運行出錯
p=p-next;
if(p-next==NULL) //s-score最大的情況 //其實兩種情況可以並在一塊寫
{
p-next=s; //連接結點
s-next=NULL; //p-next就等於NULL
}
else
{
p-next=s; //連接結點
s-next=p-next;
}
return head ;
}
/*查找符合條件的結點,並返回指向該結點的指針*/
struct student *search(struct student *head)
{
struct student *p=head-next;
int num;
printf(“請輸入要查找學生的學號:\n”);
scanf(“%d”,num);
while(p!=NULLp-num!=num) //特別注意兩條件不能寫反,若寫反最後p指向NULL時p-num找不到 運行出錯
p=p-next;
if(p==NULL) //特別注意兩個if不能調換,若調換最後p指向NULL時p-num運行出錯
{
printf(“找不到符合條件的結點!!!”);
return NULL; //查找不到返回空指針
}
if(p-num==num)
{
printf(“找到符合條件的結點\n該結點為%d\t%f”,p-num,p-score);
return p; //返回查找到的指針
}
}
/*輸出鏈表各結點的值,也稱對鏈表的遍歷*/
void print(struct student *head)
{
struct student *p;
printf(” 鏈表如下: \n”);
p=head-next;
while(p!=NULL)
{
printf(“%d\t%.1f\n”,p-num,p-score);
p=p-next;
}
}
/*釋放鏈表*/
void free_list(struct student *head)
{
struct student *p=head ;
printf(“釋放鏈表:\n”);
while(p!=NULL)
{
head=head-next;
free(p);
p=head;
}
printf(“釋放鏈表成功!\n”);
}
/*刪除鏈表中值為num的結點,並返回鏈表的首指針*/
struct student *delete_note(struct student *head,int num_x)
{
struct student *p1=head-next , *p2=head ;
while(p1!=NULLp1-num!=num_x) //特別注意左右條件不能調換,若調換如果p1指向NULL時p1-num運行出錯
{
p2=p1;
p1=p1-next;
}
if(p1==NULL) //特別注意兩個if不能調換,若調換如果p1指向NULL時,p1-num運行出錯
printf(“找不到符合刪除要求的結點!!!\n”);
if(p1-num==num_x)
{
p2-next=p1-next;
free(p1);
printf(“結點刪除成功!\n”);
}
return head;
}
/*完整的有頭結點鏈表操作程序*/
void main()
{
struct student *p , *head ;
char c;
int num ;
float score ;
printf(“有頭結點鏈表操作程序:\n”);
head=create_head();
while(1)
{
printf(“I:插入結點(自動升序) P:輸出鏈表 S:查找結點 D:刪除結點 E:釋放鏈表並退出程序! “);
c=getch();
switch(c)
{
case’I’:
printf(“請分別輸入要插入學生的學號和分數:\n”);
scanf(“%d%f”,num,score);
p=(struct student*)malloc( sizeof(struct student) );
if(p==NULL)
{
printf(“申請該結點失敗!!!\n”);
exit (0) ;
}
p-num=num; p-score=score; //給p賦值
insert(head,p);
printf(“插入成功!\n”);
break;
case’P’:
print(head);
break;
case’S’:
search(head);
break;
case’D’:
printf(“請輸入要刪除的學生的學號:\n”);
scanf(“%d”,num);
delete_note(head,num);
break;
case’E’:
free_list(head);
exit (0);
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/193156.html