6-2 順序表操作集(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 int position;
typedef struct lnode *list;
struct lnode ;
list makeempty()
:建立並返回乙個空的線性表;
position find( list l, elementtype x )
:返回線性表中x的位置。若找不到則返回error;
bool insert( list l, elementtype x, position p )
:將x插入在位置p並返回true。若空間已滿,則列印「full」並返回false;如果引數p指向非法位置,則列印「illegal position」並返回false;
bool delete( list l, position p )
:將位置p的元素刪除並返回true。若引數p指向非法位置,則列印「position p empty」(其中p是引數值)並返回false。
#include #include #define maxsize 5
#define error -1
typedef enum bool;
typedef int elementtype;
typedef int position;
typedef struct lnode *list;
struct lnode ;
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-- )
scanf("%d", &n);
while ( n-- )
return 0;
}/* 你的**將被嵌在這裡 */
6
1 2 3 4 5 6
36 5 1
2-1 6
full insertion error: 6 is not in.
finding error: 6 is not in.
5 is at position 0.
1 is at position 4.
position -1 empty deletion error.
full insertion error: 0 is not in.
position 6 empty deletion error.
full insertion error: 0 is not in.
#include
#include
using namespace std;
#define maxsize 5
#define error -1
typedef enum bool;
typedef int elementtype;
typedef int position;
typedef struct lnode *list;
struct lnode lnode;
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-- )
scanf("%d", &n);
while ( n-- )
return 0;
}list makeempty()
position find( list l, elementtype x )
}return error;
}bool insert( list l, elementtype x, position p )
if(p<0||p>l->last+1)
int i;
for(i=l->last;i>=p;i--)
l->data[p]=x;
l->last++;
return true;
}bool delete( list l, position p )
else
l->last--;
return true;}}
MOOC 聽課筆記 資料結構
最大子列和問題 kn 1 n 2 n k n i n i 1 n j 1 i j k幾種不同時間複雜度的解法 1.o n 3 暴搜 int maxsub1 int a,int n return maxsum 2.o n 2 有技巧的暴搜 int maxsub2 int a,int n return ...
資料結構 浙大MOOC 筆記二 線性結構
線性表及其表現 第二章的內容是關於三種最基本的資料結構 結合 ddsa 第三章 表 棧和佇列做乙個總結 首先簡單說明一下各個資料結構的特點 陣列 連續儲存,遍歷快且方便,長度固定,缺點是刪除和新增資料需要移動 1,n 個資料,時間複雜度高 鍊錶 離散儲存,新增和刪除方便,空間和時間消耗大,雙向鍊錶比...
《資料結構》 陳越Mooc
第一章 基本概念 1.1什麼是資料結構 解決問題方法的效率跟資料的組織方式 空間的利用效率 演算法的巧妙程度有關。資料結構 資料物件在計算機中的組織方式。1 邏輯結構 個人存放資料的方式,如線性結構,樹狀結構 2 物理儲存結構 資料物件在機器中的放法 演算法 資料物件必定與加在其上的操作相關聯,這些...