資料結構之順序表

2021-07-02 08:09:02 字數 4412 閱讀 6665

首先是標頭檔案seqlist.h

#ifndef _seqlist_h

#define _seqlist_h

#include#includeusing namespace std;

#define elemtype int

#define seqlist_default_size 10

typedef struct seqlist

seqlist;

int begin();

int end(seqlist *list);

void initseqlist(seqlist *list);

bool isempty(seqlist *list);

bool isfull(seqlist *list);

bool push_back(seqlist *list,elemtype x);

bool push_front(seqlist *list,elemtype x);

bool pop_back(seqlist *list);

bool pop_front(seqlist *list);

bool insert_pos(seqlist *list, int p, elemtype x);

bool delete_val(seqlist *list, elemtype key);

void show(seqlist *list);

int find(seqlist *list,elemtype x);

bool insert_value(seqlist *list,elemtype x);

bool delete_pos(seqlist *list,int n);

elemtype getvalue(seqlist *list,int p);

bool modify(seqlist *list,elemtype val,elemtype nval);

bool clear(seqlist *list);

bool destory(seqlist *list);

bool sort(seqlist *list);

bool resver(seqlist *list);

int length(seqlist *list);

elemtype next(seqlist *list,int p);

elemtype prio(seqlist *list,int p);

#endif

函式的實現,seqlist..h:

#include"seqlist.h"

void initseqlist(seqlist *list)

int begin()

int end(seqlist *list)

bool isempty(seqlist *list)

bool isfull(seqlist *list)

bool push_back(seqlist *list,elemtype x)

bool push_front(seqlist *list,elemtype x)

/*bool push_back(seqlist *list,elemtype x)

bool push_front(seqlist *list,elemtype x)

list->base[0] = x;

list->size++;

return true;}*/

bool pop_back(seqlist *list)

int find(seqlist *list,elemtype x)

} return -1;

}bool delete_pos(seqlist *list,int p)

list->size--;

return true;

}bool insert_value(seqlist *list,elemtype x)

list->base[i]=x;

list->size++;

return true;

}bool pop_front(seqlist *list)

list->size--;

return true;

}bool insert_pos(seqlist *list, int p, elemtype x)

list->base[p] = x;

list->size++;

return true;

}bool delete_val(seqlist *list, elemtype key)

/*bool delete_val(seqlist *list, elemtype key)

if(i >= list->size)

return false;

for(int k=i; ksize-1; ++k)

list->size--;

return true;}*/

void show(seqlist *list)

coutsize)

return false;

return list->base[p];

}bool modify(seqlist *list,elemtype val,elemtype nval)

} return true;

}bool clear(seqlist *list)

bool destory(seqlist *list)

bool sort(seqlist *list)

} }return true;

}bool resver(seqlist *list)

return true;

}int length(seqlist *list)

elemtype next(seqlist *list,int p)

elemtype prio(seqlist *list,int p)

主函式main.cpp:

#include#include"seqlist.h"

using namespace std;

void main()

break;

case 2:

cout<<"請輸入要插入的資料(-1結束):>";

while(cin>>item,item!=-1)

break;

case 3:

show(&mylist);

break;

case 4:

pop_back(&mylist);

break;

case 5:

pop_front(&mylist);

break;

case 6:

cout<<"請輸入要插入的位置:>";

cin>>pos;

cout<<"請輸入要插入的值:>";

cin>>item;

insert_pos(&mylist,pos,item);

break;

case 7:

cout<<"請輸入要插入的值:>";

cin>>item;

insert_value(&mylist,item);

break;

case 8:

cout<<"請輸入要刪除的位置:>";

cin>>pos;

delete_pos(&mylist,pos);

break;

case 9:

cout<<"請輸入要刪除的值:>";

cin>>item;

delete_val(&mylist,item);

break;

case 10:

cout<<"請輸入要查詢的值:>";

cin>>item;

cout<<"該值得下標是:"<";

cin>>pos;

cout<<"該下標對應的值是:"<";

cin>>item;

cout<<"請輸入新值:>";

cin>>nitem;

modify(&mylist,item,nitem);

break;

case 13:

clear(&mylist);

break;

case 14:

destory(&mylist);

break;

case 15:

sort(&mylist);

break;

case 16:

resver(&mylist);

break;

case 17:

cout<<"長度為:"<";

cin>>pos;

cout<<"下乙個值是:"<";

cin>>pos;

cout<<"前乙個值是:"<

資料結構之順序表

順序表的思想容易了解,但是 不容易掌握,我這裡根據老師所提供的 進行一下簡單的總結 這個 包含順序表的查詢,插入,刪除,建表,輸出資料 includeusing namespace std define ok 1 define error 0 define overflow 2 typedef in...

資料結構之順序表

順序表就是按照順序儲存方式儲存的線性表,該線性表的結點按照邏輯次序一次存放在計算機的一組連續的儲存單元中如下圖 由於順序表是一次存放的,只要知道了該順序表的首位址以及每個資料元素所占用的儲存長度,那麼我們就很容易計算出任何乙個資料元素 也就是資料繫結點 的位置。1 結點資料型別 public cla...

資料結構之 順序表

源 本 僅僅顯示了順序表中基本的刪除,顯示等操作。其他相關操作可以進一步擴充。接下來是實現線性表中的鍊錶結構。include include define maxline 1000 typedef struct seqlist void listinit seqlist 初始化順序表 int lis...