順序表靜態與動態實現及基本的操作(增刪查改等)

2021-07-25 08:21:57 字數 4108 閱讀 4803

#define _crt_secure_no_warnings 1

#ifndef _seqlist_

#define _seqlist_

#define max_size 8

typedef

int datatype;

typedef

struct seqlist//靜態順序表的宣告

seqlist;

void initseqlist(seqlist* s);

void printseqlist(seqlist* s);

void pushback(seqlist* s, datatype a);

void popback(seqlist* s);

void pushfront(seqlist* s, datatype a);

void popfront(seqlist* s);

void insert(seqlist* s, unsigned

int pos, datatype a);

void erase(seqlist* s, unsigned

int pos);

int find(seqlist* s, datatype a);

void remove(seqlist* s, datatype a);

void removeall(seqlist* s, datatype a);

void bubblesort(seqlist* s);

void selectesort(seqlist* s);

void insertsort(seqlist* s);

int binarysearch(seqlist* s,datatype a);

#endif

#define _crt_secure_no_warnings 1

#include "seqlist.h"

#include

#include

#include

void initseqlist(seqlist* s)//初始化順序表

void printseqlist(seqlist* s)//列印順序表

printf("\n");

}void pushback(seqlist* s, datatype a)//尾插

s->array[s->size] = a;

s->size++;

}void popback(seqlist* s)//尾刪

s->size--;

}void pushfront(seqlist* s, datatype a)

int end = s->size - 1;

while (end>=0)

s->array[0] = a;

s->size++;

}void popfront(seqlist* s)

else

s->size--;

}}void insert(seqlist* s, unsigned

int pos, datatype a)//在指定位置插入資料

unsigned

int i = 0;

for (i = (int)s->size-1; i>=pos-1; i--)

s->array[pos-1] = a;

s->size++;

}int find(seqlist* s, datatype a)

}return -1;

}void erase(seqlist* s, unsigned

int pos)

unsigned

int i = 0;

for (i = pos-1; i < s->size-1; i++)

s->size--;

}void remove(seqlist* s, datatype a)

if ( pos == -1)

int i = 0;

for (i = pos ; i < (int)s->size - 1; i++)

s->size--;

}void removeall(seqlist* s, datatype a)

else

}s->size -= count;

}void bubblesort(seqlist* s)

}if (!flag)

}}void selectesort(seqlist* s)

if (s->array[i]>s->array[max_index])

}datatype temp1 = s->array[start];

s->array[start] = s->array [min_index];

s->array[min_index] = temp1;

if (max_index == start)

datatype temp2 = s->array[end];

s->array[end] = s->array[max_index];

s->array[max_index] = temp2;

end--;

start++;

}}void insertsort(seqlist* s)

s->array[end+1] = temp;

}}int binarysearch(seqlist* s,datatype a)

else

if (s->array[mid] < a)

else

}return -1;

}

#include "seqlist.h"

#include

#include

//void test1()//pushback,popback

//void test2()//pushfront popfront

void test3()

void test4()//sort

else

}int main()

//實現順序表的動態增長

dseqlist.h

#pragma once

#include

#include

#include

#include

typedef

int datatype;

typedef

struct dseqlist

dseqlist;

void initdseqlist(dseqlist* seq);

void checkcapacity(dseqlist* seq);

void printseqlist(dseqlist* seq);//列印順序表

void pushback(dseqlist* seq, datatype a);//尾插

void pushfront(dseqlist* seq, datatype a);

void destory(dseqlist* seq);

#define _crt_secure_no_warnings 1

#include "dseqlist.h"

void initdseqlist(dseqlist* seq)

void checkcapacity(dseqlist* seq)

//檢查順序表是否溢位,並擴容

}void printseqlist(dseqlist* seq)//列印順序表

printf("\n");

}void pushback(dseqlist* seq, datatype a)//尾插

void pushfront(dseqlist* seq, datatype a)

seq->array[0] = a;

seq->size++;

}void destory(dseqlist* seq)

seq->array = null;

seq->size = 0;

seq->capacity = 0;

}

動態順序表的基本實現

include include define list init size 100 define listincrement 10 define status int define overflow 1 define ok 1 define error 0 define elemtype int t...

資料結構 實現基於靜態陣列的順序表的基本操作

實現基於靜態陣列的順序表的以下基本操作 首先先定義乙個結構體型別seqlist arr size 有效的元素個數 預定義乙個陣列的最大長度,將整型資料變數typedef為datatype,為了以後在插入刪除別的型別時方便修改 初始化 void initseq seqlist s 將陣列的size置為...

用列表實現順序表的基本操做

用列表實現順序表的基本操做 class seqlist def init self,max space 30 self.max space max space max space最大分配空間 self.sl max space 0 申請乙個列表 self.length 0 記錄實際元素的個數 def...