6-5 鏈式表操作集(20 分)
本題要求實現鏈式表的操作集。
其中position find( list l, elementtype x );
list insert( list l, elementtype x, position p );
list delete( list l, position p );
list
結構定義如下:
各個操作函式的定義為:typedef struct lnode *ptrtolnode;
struct lnode ;
typedef ptrtolnode position;
typedef ptrtolnode list;
position find( list l, elementtype x )
:返回線性表中首次出現x的位置。若找不到則返回error;
list insert( list l, elementtype x, position p )
:將x插入在位置p指向的結點之前,返回鍊錶的表頭。如果引數p指向非法位置,則列印「wrong position for insertion」,返回error;
list delete( list l, position p )
:將位置p的元素刪除並返回鍊錶的表頭。若引數p指向非法位置,則列印「wrong position for deletion」並返回error。
#include #include #define error null
typedef int elementtype;
typedef struct lnode *ptrtolnode;
struct lnode ;
typedef ptrtolnode position;
typedef ptrtolnode list;
position find( list l, elementtype x );
list insert( list l, elementtype x, position p );
list delete( list l, position p );
int main()
scanf("%d", &n);
while ( n-- )
}l = insert(l, x, null);
if ( l==error ) printf("wrong answer\n");
else
printf("%d is inserted as the last element.\n", x);
p = (position)malloc(sizeof(struct lnode));
tmp = insert(l, x, p);
if ( tmp!=error ) printf("wrong answer\n");
tmp = delete(l, p);
if ( tmp!=error ) printf("wrong answer\n");
for ( p=l; p; p = p->next ) printf("%d ", p->data);
return 0;
}/* 你的**將被嵌在這裡 */
6
12 2 4 87 10 2
42 12 87 5
2 is found and deleted.
12 is found and deleted.
87 is found and deleted.
finding error: 5 is not in.
5 is inserted as the last element.
wrong position for insertion
wrong position for deletion
10 4 2 5
解答:
position find( list l, elementtype x )
return error;
}list insert( list l, elementtype x, position p )
else
if(l)
else
}}list delete( list l, position p )
else
if(l)
else
}}
PTA 資料結構與演算法題目集(中文)6 2
6 2 順序表操作集 20 分 本題要求實現順序表的操作集。list makeempty position find list l,elementtype x bool insert list l,elementtype x,position p bool delete list l,positio...
PTA資料結構與演算法題目集(中文)7 18
題意 有兩個視窗a,b,題目給出a視窗處理完兩個顧客,b視窗處理完乙個顧客,並且當不同視窗同時處理完2個顧客時,a視窗顧客優先輸出。我們可以給出兩個佇列q1,q2分別表示a,b視窗,編號為奇數的顧客存放到q1中,為偶數的顧客存放到q2中。include include include include...
PTA資料結構與演算法題目集 中文 7 34
7 34 任務排程的合理性 25 分 假定乙個工程專案由一組子任務構成,子任務之間有的可以並行執行,有的必須在完成了其它一些子任務後才能執行。任務排程 包括一組子任務 以及每個子任務可以執行所依賴的子任務集。比如完成乙個專業的所有課程學習和畢業設計可以看成乙個本科生要完成的一項工程,各門課程可以看成...