實驗11 鍊錶 程式1奇數值結點鍊錶

2022-07-20 10:54:10 字數 2003 閱讀 1613

輸入若干個正整數(輸入-1為結束標誌)建立乙個單向鍊錶,頭指標為l,將鍊錶l中奇數值的結點重新組成乙個新的鍊錶new,並輸出新建鍊錶的資訊。

第一種方法:逆向思維

#include#include

struct

stu_node;

struct stu_node*create_stu_doc();/*

新建鍊錶

*/struct stu_node*deletedoc(struct stu_node*head);/*

奇數項相連就是去掉偶數項

*/void print_stu_doc(struct stu_node*head);/*

遍歷*/

int main(void

)struct stu_node*create_stu_doc()

return

head;

}struct stu_node*deletedoc(struct stu_node*head)

if(head==null)

return

null;

ptr1=head;

ptr2=head->next;

while(ptr2!=null)

else

ptr1=ptr2;

ptr2=ptr1->next;

}return

head;

}void print_stu_doc(struct stu_node*head)

for(ptr=head;ptr;ptr=ptr->next)

printf("%d

",ptr->num);

printf("\n

");}

第二種方法:正向思維

#include#include

#include

struct

stud_node;

void ptrint_stu_doc(struct stud_node *head);

intmain()

new=tail2=null;

p1=l;

while(p1!=null)

else

break;}

if(p1==null) break; /*

----將鍊錶l中奇數值的結點重新組成乙個新的鍊錶new--

*/p2=(struct stud_node *)malloc(size);

p2->num=p1->num;

p2->next=null;

if(new==null)

new=p2;

else

tail2->next=p2;

tail2=p2;

p1=p1->next;

}tail2->next=null;

ptrint_stu_doc(new);

}void ptrint_stu_doc(struct stud_node *head)

for(ptr=head;ptr;ptr=ptr->next)

printf("%d

",ptr->num);

printf("\n

");}

再加乙個

#includestruct

elem

;int

main()

else

scanf("%d

",&temp);}//

刪除偶數

tempp=head;

while(null!=tempp)

else}}

}deletep=tempp;

tempp=tempp->next;

if(0==deletep->data%2

)delete deletep;}//

列印資料

tempp=head;

while(null!=tempp)

}

奇數值結點鍊錶

輸入樣例 1 2 2 3 4 5 6 7 1 輸出樣例 1 3 5 7 2 2 4 6 include include struct listnode struct listnode readlist struct listnode getodd struct listnode l void pri...

奇數值結點鍊錶

題目 本題要求實現兩個函式,分別將讀入的資料儲存為單鏈表 將鍊錶中奇數值的結點重新組成乙個新的鍊錶。鍊錶結點定義如下 struct listnode 函式介面定義 struct listnode readlist struct listnode getodd struct listnode l 函式...

11 7 奇數值結點鍊錶

奇數值結點鍊錶 輸入若干個正整數 輸入 1為結束標誌 建立乙個單向鍊錶,頭指標為l,將鍊錶l中奇數值的結點重新組成乙個新的鍊錶new,並輸出新建鍊錶的資訊。鍊錶結點定義 struct listnode 函式介面定義 struct listnode readlist struct listnode g...