① c語言鏈表:填空題,我只寫了sort()函數,目的是將結點按num順序排列的。函數錯了,怎麼改還
sort 函數裡面 prev =head->next;
prev->next = null;//出錯的關鍵是這句。讓head->next->next = null了。
p = head->next; //相當於 p ->next =null了
進入一次循環,p = p ->next ; p == null了 ;直接跳出循環了
② 急救!!C語言一道關於結構體鏈表的填空題,就一個空
上面那位大哥,你出現了一個小錯誤,我改了一下p = pa; //將p指針指向首地址
printf("%c", p.value); 不知你結構另外一個成員是哪個,就當value了,別的你改
p = p->next; //1樓指針應該用指針成員
③ c語言 鏈表 填空 速度追加給分
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}Linknode;
//創建一個鏈表,其數據元素為2,4,2,8,3,2,2
Linknode *create()
{
Linknode * head,*p,*n;
int temp=0;
head=(Linknode *)malloc(sizeof(Linknode));
head->next=NULL;
p=head;//head is first node
while(1){
scanf("%d",&temp);
if(!temp)break;
n=(Linknode *)malloc(sizeof(Linknode));
n->data=temp;
n->next=NULL;
p->next=n;
p=n;
}
return head;
}
int print(Linknode *h)
{
Linknode *p;
p=h->next;
while(p){
printf("%d,",p->data);
p=p->next;
}
return 0;
}
//計算數據域為x的節點個數
int countx(Linknode *h,int x)
{
Linknode *p;
int count=0;
/***********SPACE***********/
p=h->next;
while(p){
/***********SPACE***********/
if(x==p->data)
/***********SPACE***********/
count++;
/***********SPACE***********/
p=p->next;
}
return count;
}
int main()
{
Linknode *head;
int m;
head=create();
print(head);
printf("\n------------------\n");
m=countx(head,2);
printf("共%d個元素",m);
system("pause");
return 0;
}
④ c語言編程問題 鏈表
鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。
鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態生成。
每個結點包括兩個部分:
1、存儲數據元素的數據域;
2、存儲下一個結點地址的指針域。
在C語言中,鏈表的構建是通過結構體實現的,一個結構體變數,形成一個鏈表的節點。
在結構體內,需要由至少一個類型為結構體本身類型的指針的元素,作為指針域存在,用來指向下一個或者上一個(僅用於雙向鏈表)元素節點。
⑤ C語言 鏈表 這個題目怎麼做
#include<stdio.h>
#include<malloc.h>
structlist{
intID;
structlist*next;
};
structlist*CreateList(){
intnum,i;
structlist*head,*p;
scanf("%d",&num);//num:鏈表結點個數
head=p=(structlist*)malloc(sizeof(structlist));//這是鏈表的頭結點
i=0;
while(i<num){
p->next=(structlist*)malloc(sizeof(structlist));
scanf("%d",&p->next->ID);
p=p->next;
i++;
}
p->next=NULL;
return(head);
}
intmain(){
structlist*head;
head=CreateList();
return0;
}
⑥ 鏈表習題(C語言)
設鏈表長度為n,找到倒數第m個元素(約定0為最後一個元素),也就是找到正數第n - m - 1個元素,計數方法當然也是從0開始。
#include<stdio.h>
#include<stdlib.h>
typedefintDataType;
typedefstructlist{
DataTypeelem;
structlist*next;
}*LIST,*pNode;
LISTInitList(){
LISTL=(pNode)malloc(sizeof(structlist));
L->elem=0;
L->next=NULL;
returnL;
}
voidCreateList(LISTL,DataTypea[],intn){
inti;
pNodep=L;
for(i=0;i<n;++i){
p->next=(pNode)malloc(sizeof(structlist));
p->next->elem=a[i];
p=p->next;
}
p->next=NULL;
}
intlenList(LISTL){//求取表長
intlen=0;
pNodep=L->next;//有頭結點
while(p){++len;p=p->next;}
returnlen;
}
intGetData(LISTL,intm,DataType*e){
inti=-1,len=lenList(L);
pNodep=L->next;
if(m<0||m>len-1){
printf("沒有第%d個元素。 ",m);
return0;
}
while(i<len-m-1){++i;p=p->next;}
*e=p->elem;
return1;
}
intmain(){
DataTypearr[]={16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
intn,m,res;
LISTL=InitList();
n=sizeof(arr)/sizeof(arr[0]);
CreateList(L,arr,n);
while(scanf("%d",&m)==1){
if(GetData(L,m,&res))
printf("倒數第%d元素是:%d. ",m,res);
}
return0;
}
⑦ 關於鏈表的程序填空
(1)struct node *p,*q; //當然是先定義兩個struct node 型的指針
(2)count++; //count自增,直到與k相等時退出循環
(3)p=p->next; //當與q指針相差k個結點時,p、q指針同步移動。
(4)q->data; //返回該結點的data域值
答案絕對正確,絕非網上搜索答案,本人是C語言驅動工程師。給分,謝謝
⑧ C語言鏈表填空,求大佬
樓主,你要的答案
#include <stdio.h>
#include <math.h>
void fun(int *a,int n,int *odd,int *even){
for(int i=0;i<n;i++){
if(a[i]%2==0)
*even+=a[i];
else
*odd+=a[i];
}}
void main(){
int a[5];
int *even;
int *odd;
int x=0,y=0;
odd=&x;
even=&y;
printf("請輸入數組a的值\n");
for(int i=0;i<5;i++)
scanf("%d",&a[i]);
fun(a,5,even,odd);
printf("偶數和為%d,奇數和為%d",*even,*odd);
}
⑨ 一道C語言鏈表題
#include<stdio.h>
#include<malloc.h>
/*UserCodeBegin(考生可在本行後添加代碼,定義程序中使用的結構體類型、聲明自定義函數的原型,行數不限)*/
structstudent
{intnum,score;
structstudent*next;
};
structstudent*creat();
structstudent*merge(structstudent*ah,structstudent*bh);
/*UserCodeEnd(考生添加代碼結束)*/
/*print以規定的格式完成遍歷顯示指定的鏈表*/
voidprint(char*Info,structstudent*Head);
intmain(void)
{
structstudent*ah,*bh;
printf("創建鏈表A,請輸入學號及成績(均輸入為0時表示停止):
");
ah=creat();
printf("
創建鏈表B,請輸入學號及成績(均輸入為0時表示停止):
");
bh=creat();
print("
鏈表A:",ah);
print("
鏈表B:",bh);
ah=merge(ah,bh);
print("
鏈表A、B合並後:",ah);
return0;
}
voidprint(char*Info,structstudent*Head)
{
printf("%s",Info);
while(Head!=NULL)
{
printf("%d,%d",Head->num,Head->score);
Head=Head->next;
}
}
/*UserCodeBegin(考生在此後完成自定義函數的設計,行數不限)*/
structstudent*creat()
{structstudent*h,*p,*H;
intxh,cj,b=1;
do
{scanf("%d%d",&xh,&cj);
if(xh==0&&cj==0)break;
h=(structstudent*)malloc(sizeof(structstudent));
h->num=xh;h->score=cj;
if(b){b=0;H=h;}
elsep->next=h;
p=h;
}while(1);
h->next=NULL;
returnH;
}
structstudent*merge(structstudent*ah,structstudent*bh)
{structstudent*p=ah;
while(p->next!=NULL)p=p->next;
p->next=bh;
returnah;
}
⑩ C語言程序填空:假設有兩個已排序的單鏈表a,b,現將它們合並成一個鏈表c,且不改變其有序性。
你不是有答案嗎,你想要什麼