華農oj資料結構 8580

2021-10-05 08:05:01 字數 2965 閱讀 6660

8580 合併鍊錶

時間限制:1000ms **長度限制:10kb

提交次數:3724 通過次數:2077

題型: 程式設計題 語言: g++;gcc

description

線性鍊錶的基本操作如下:

#include

#include

#define error 0

#define ok 1

#define elemtype int

typedef int status;

typedef struct lnode

lnode,*linklist;

status listinsert_l(linklist &l, int i, elemtype e)

if (!p || j > i-1) return error; // i小於1或者大於表長

s = (linklist)malloc(sizeof(lnode)); // 生成新結點

s->data = e; s->next = p->next; // 插入l中

p->next = s;

return ok;

} // linstinsert_l

status listdelete_l(linklist &l, int i, elemtype &e)

if (!(p->next) || j > i-1) return error; // 刪除位置不合理

q = p->next;

p->next = q->next; // 刪除並釋放結點

e = q->data;

free(q);

return ok;

} // listdelete_l

設計乙個演算法將兩個非遞減有序鍊錶a和b合併成乙個新的非遞減有序鍊錶c。

輸入格式

第一行:單鏈表a的元素個數

第二行:單鏈表a的各元素(非遞減),用空格分開

第三行:單鏈表b的元素個數

第四行:單鏈表b的各元素(非遞減),用空格分開

輸出格式

第一行:單鏈表a的元素列表

第二行:單鏈表b的元素列表

第三行:合併後單鏈表c的元素列表

輸入樣例

612 24 45 62 84 96

415 31 75 86

輸出樣例

list a:12 24 45 62 84 96

list b:15 31 75 86

list c:12 15 24 31 45 62 75 84 86 96

#include

#include

#define error 0

#define ok 1

#define elemtype int

typedef

int status;

typedef

struct lnode

lnode,

*linklist;

status listinsert_l

(linklist &l,

int i, elemtype e)if(

!p || j > i-1)

return error;

// i小於1或者大於表長

s =(linklist)

malloc

(sizeof

(lnode));

// 生成新結點

s->data = e; s-

>next = p-

>next;

// 插入l中

p->next = s;

return ok;

}// linstinsert_l

status listdelete_l

(linklist &l,

int i, elemtype &e)if(

!(p-

>next)

|| j > i-1)

return error;

// 刪除位置不合理

q = p-

>next;

p->next = q-

>next;

// 刪除並釋放結點

e = q-

>data;

free

(q);

return ok;

}// listdelete_l

intmain()

printf

("list a:");

linklist p=a-

>next;

for(

int i=

1;i<=a;i++

)printf

("\n");

scanf

("%d"

,&b)

;for

(int i=

1;i<=b;i++

)printf

("list b:");

p=b-

>next;

for(

int i=

1;i<=b;i++

)printf

("\n");

linklist pa=a-

>next,pb=b-

>next,pc=c;

linklist et;

while

(pa!=

null

&&pb!=

null

)else

}while

(pa!=

null

)while

(pb!=

null

)printf

("list c:");

et=c-

>next;

for(

int i=

1;i<=

(a+b)

;i++

)printf

("\n");

}

華農oj資料結構 8578

8578 順序表逆置 時間限制 1000ms 長度限制 10kb 提交次數 3660 通過次數 2149 題型 程式設計題 語言 g gcc 輸入格式 第一行 輸入順序表的元素個數 第二行 輸入順序表的各元素,用空格分開 輸出格式 第一行 逆置前的順序表元素列表 第二行 逆置後的順序表元素列表 輸入...

希爾排序 華農oj 8640

8640 希爾 shell 排序 時間限制 1000ms 長度限制 10kb 提交次數 1858 通過次數 1304 題型 程式設計題 語言 g gcc description 用函式實現希爾 shell 排序,並輸出每趟排序的結果,初始增量d n 2,其後d d 2 輸入格式 第一行 鍵盤輸入待排...

選擇排序 華農oj 8643

8643 簡單選擇排序 時間限制 1000ms 長度限制 10kb 提交次數 2235 通過次數 1301 題型 程式設計題 語言 g gcc description 用函式實現簡單選擇排序,並輸出每趟排序的結果 輸入格式 第一行 鍵盤輸入待排序關鍵的個數n 第二行 輸入n個待排序關鍵字,用空格分隔...