帶頭結點的鏈式表操作集 20分

2021-10-01 15:14:51 字數 3899 閱讀 3067

本題要求實現帶頭結點的鏈式表操作集。

函式介面定義:

list makeempty()

; position find

( list l, elementtype x )

;bool insert

( list l, elementtype x, position p )

;bool delete

( list l, position p )

;

其中list結構定義如下:

typedef

struct lnode *ptrtolnode;

struct lnode

;typedef ptrtolnode position;

typedef ptrtolnode list;

各個操作函式的定義為:

list makeempty():建立並返回乙個空的線性表;

position find( list l, elementtype x ):返回線性表中x的位置。若找不到則返回error;

bool insert( list l, elementtype x, position p ):將x插入在位置p指向的結點之前,返回true。如果引數p指向非法位置,則列印「wrong position for insertion」,返回false;

bool delete( list l, position p ):將位置p的元素刪除並返回true。若引數p指向非法位置,則列印「wrong position for deletion」並返回false。

裁判測試程式樣例:

#include

#include

#define error null

typedef

enum

bool;

typedef

int elementtype;

typedef

struct lnode *ptrtolnode;

struct lnode

;typedef ptrtolnode position;

typedef ptrtolnode list;

list makeempty()

; position find

( list l, elementtype x )

;bool insert

( list l, elementtype x, position p )

;bool delete

( list l, position p )

;int

main()

scanf

("%d"

,&n)

;while

( n--)}

flag =

insert

(l, x,

null);

if( flag==false )

printf

("wrong answer\n");

else

printf

("%d is inserted as the last element.\n"

, x)

; p =

(position)

malloc

(sizeof

(struct lnode));

flag =

insert

(l, x, p);if

( flag==true )

printf

("wrong answer\n");

flag =

delete

(l, p);if

( flag==true )

printf

("wrong answer\n");

for( p=l->next; p; p = p->next )

printf

("%d "

, p->data)

;return0;

}/* 你的**將被嵌在這裡 */

輸入樣例

6122

487102

421287

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 deletion104

25

思路:

帶有頭結點

帶有頭結點時插入和刪除不必考慮頭插和尾刪

注意查詢時要從l->next開始查詢而不是l

list makeempty()

position find

( list l, elementtype x )

while

(l->next!=

null

) l=l->next;

}return error;

}bool insert

( list l, elementtype x, position p )

l=l->next;

}printf

("wrong position for insertion\n");

return false;

}bool delete

( list l, position p )

l=l->next;

}printf

("wrong position for deletion\n");

return false;

}/*list makeempty() //這樣構造出來的鍊錶是帶有頭結點的鍊錶因為n的下乙個為空,實際上n的下乙個就是鍊錶的第乙個

position find( list l, elementtype x )

while(m!=null)

m=m->next;

} return error;

}bool insert( list l, elementtype x, position p )

l=l->next;

}printf("wrong position for insertion\n");

return false;

}bool delete( list l, position p )

l=l->next;

}printf("wrong position for deletion\n");

return false;}/*

list makeempty()

position find( list l, elementtype x )

return error;

}bool insert( list l, elementtype x, position p )

l = l->next;

} printf("wrong position for insertion\n");

return false;

}bool delete( list l, position p )

printf("wrong position for deletion\n");

return false;

}*/

6 6 帶頭結點的鏈式表操作集 20分

本題要求實現帶頭結點的鏈式表操作集。函式介面定義 list makeempty position find list l,elementtype x bool insert list l,elementtype x,position p bool delete list l,position p 其...

6 6 帶頭結點的鏈式表操作集 20分

list makeempty position find list l,elementtype x bool insert list l,elementtype x,position p bool delete list l,position p 其中list結構定義如下 typedef struc...

6 6 帶頭結點的鏈式表操作集 20分

本題要求實現帶頭結點的鏈式表操作集。函式介面定義 list makeempty position find list l,elementtype x bool insert list l,elementtype x,position p bool delete list l,position p 其...