十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
這篇文章主要為大家展示了“web中如何實現實現順序表”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“web中如何實現實現順序表”這篇文章吧。

seqlist.h
#ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include#include #include #define INIT_SIZE 2 #define ADD_SIZE 3 typedef int DataType; typedef struct Seqlist { DataType *data; int size; //當前空間存儲的元素個數 int capacity; //當前空間所能存儲的大容量 }Seqlist,*pSeqlist; void InitSeqlist(pSeqlist pSeq); void DestorySeqlist(pSeqlist pSeq); void PushBack(pSeqlist pSeq,DataType x); void PopBack(pSeqlist pSeq); void PushFront(pSeqlist pSeq,DataType x); void PopFront(pSeqlist pSeq); void Remove(pSeqlist pSeq,DataType x); void RemoveAll(pSeqlist pSeq,DataType x); void BubbleSort(pSeqlist pSeq); void InsertSort(pSeqlist pSeq); void SelectSort(pSeqlist pSeq); int BinarySearch(pSeqlist pSeq,DataType x); void Erase(pSeqlist pSeq,int pos); void PrintSeqlist(pSeqlist pSeq); #endif //SEQLIST_D_H__
seqist.c
#include"seqlist.h"
void InitSeqlist(pSeqlist pSeq)
{
pSeq->data = (DataType *)malloc(INIT_SIZE*sizeof(DataType));
if (pSeq->data == NULL)
{
printf("out of memory\n");
exit(1);
}
pSeq->size = 0;
pSeq->capacity = INIT_SIZE; //將容量置為當前空間所能存儲的大值
}
void DestorySeqlist(pSeqlist pSeq)
{
free(pSeq->data);
pSeq->data = NULL;
pSeq->size = 0;
pSeq->capacity = 0;
}
void CheckCapacity(pSeqlist pSeq) //查看當前空間是否已滿
{
assert(pSeq);
if (pSeq->size == pSeq->capacity) //如果滿了則進行擴容
{
DataType *tmp = NULL;
tmp = (DataType *)realloc(pSeq->data, (pSeq->capacity += ADD_SIZE)*sizeof(DataType));
if (NULL == tmp)
{
printf("out of memory\n");
exit(1);
}
pSeq->data = tmp;
}
}
void PushBack(pSeqlist pSeq, DataType x)
{
assert(pSeq);
CheckCapacity(pSeq); //只要插入元素,首先就要檢查空間是否以滿
pSeq->data[pSeq->size++] = x; //插入元素后size也要變化
}
void PopBack(pSeqlist pSeq)
{
assert(pSeq);
if (pSeq->size == 0) //異常情況,表已空
{
printf("表已空\n");
return;
}
pSeq->size--;
}
void PushFront(pSeqlist pSeq, DataType x)
{
int i = 0;
assert(pSeq);
CheckCapacity(pSeq); //只要插入元素,首先就要檢查空間是否以滿
for (i = pSeq->size; i > 0; i--) //從后往前先將數據移動
{
pSeq->data[i] = pSeq->data[i-1];
}
pSeq->data[0] = x;
pSeq->size++;
}
void PopFront(pSeqlist pSeq)
{
int i = 0;
assert(pSeq);
if (pSeq->size == 0) //異常情況,表空
{
printf("表已空\n");
return;
}
for (i = 0; i < pSeq->size-1; i++) //直接從第二個元素依次向前覆蓋
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->size--;
}
void Remove(pSeqlist pSeq, DataType x) //刪除第一個出現的x
{
int i = 0;
int j = 0;
assert(pSeq);
for (i = 0; i < pSeq->size; i++)
{
if (pSeq->data[i] == x)
{
for (j = i; j < pSeq->size-1; j++) //刪除的時候從這個元素的后面向前覆蓋
{
pSeq->data[j] = pSeq->data[j + 1];
}
pSeq->size--;
return;
}
}
}
void RemoveAll(pSeqlist pSeq, DataType x)
{
int i = 0;
int j = 0;
assert(pSeq);
for (i = 0; i < pSeq->size; i++)
{
if (pSeq->data[i] == x)
{
for (j = i; j < pSeq->size - 1; j++) //刪除的時候從這個元素的后面向前覆蓋
{
pSeq->data[j] = pSeq->data[j + 1];
}
pSeq->size--;
}
}
}
void BubbleSort(pSeqlist pSeq)
{
int flag = 0;
int i = 0;
int j = 0;
int k = pSeq->size-1;
assert(pSeq);
for (i = 0; i < pSeq->size - 1; i--)
{
int m = 0;
flag = 1; //將標記置1
for (j = 0; j < k; j++)
{
if (pSeq->data[j]>pSeq->data[j + 1])
{
DataType tmp = pSeq->data[j];
pSeq->data[j] = pSeq->data[j + 1];
pSeq->data[j + 1] = tmp;
flag = 0;
m = j; //記錄最后一次交換的位置
}
}
if (flag) //標記位1表示已經有序
{
return;
}
m = k; //將k設置成最后一次交換的位置
}
}
void InsertSort(pSeqlist pSeq) //插入排序
{
int i = 0;
int j = 0;
assert(pSeq);
for (i = 1; i < pSeq->size; i++)
{
DataType tmp = pSeq->data[i];
for (j = i-1; j >=0; j--)
{
if (pSeq->data[j]>tmp)
{
pSeq->data[j+1] = pSeq->data[j];
}
else
{
break;
}
}
pSeq->data[j+1] = tmp;
}
}
void SelectSort(pSeqlist pSeq)
{
int i = 0;
int j = 0;
int k = 0;
assert(pSeq);
for (i = 0; i < pSeq->size; i++)
{
k = i;
for (j = i + 1; j < pSeq->size; j++)
{
if (pSeq->data[k]>pSeq->data[j])
{
k = j;
}
}
if (k != i)
{
DataType tmp = pSeq->data[k];
pSeq->data[k] = pSeq->data[i];
pSeq->data[i] = tmp;
}
}
}
int BinarySearch(pSeqlist pSeq, DataType x)
{
int left = 0;
int right = pSeq->size - 1;
int mid=0;
assert(pSeq);
mid = (left&right) + ((left^right) >> 1); //求平均值
while (left <= right)
{
if (pSeq->data[mid]>x)
{
right = mid - 1;
}
else if (pSeq->data[mid] < x)
{
left = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
void Erase(pSeqlist pSeq, int pos)
{
int i = 0;
assert(pSeq);
if (pos>= pSeq->size&&pos < 0)
{
printf("刪除位置不合法\n");
return;
}
for (i = pos; i < pSeq->size - 1; i++) //從pos之后依次向前覆蓋
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->size--;
}
void PrintSeqlist(pSeqlist pSeq)
{
int i = 0;
assert(pSeq);
if (pSeq->size == 0)
{
printf("表為空\n");
return;
}
for (i = 0; i < pSeq->size; i++)
{
printf("%d ", pSeq->data[i]);
}
printf("\n");
}test.c
#include"seqlist.h"
void menu()
{
printf("0.exit 1.PrintSeqlist \n");
printf("2.InitSeqlist 3.PushBack \n");
printf("4.Popback 5.PushFornt \n");
printf("6.PopFornt 7.Erase \n");
printf("8.Remove 9.RemoveAll \n");
printf("10.BubbleSort 11.BinarySearch\n");
printf("12.DestorySeqlist 13.InsertSort \n");
printf("14.SelectSort \n");
printf("請輸入:>");
}
void test(pSeqlist pSeq)
{
DataType x = 0;
int n = 0;
int pos = 0;
int ret=0;
while (1)
{
menu();
scanf("%d", &n);
switch (n)
{
case 0:
exit(1);
break;
case 1:
PrintSeqlist(pSeq);
break;
case 2:
InitSeqlist(pSeq);
break;
case 3:
printf("請輸入元素\n");
scanf("%d", &x);
PushBack(pSeq, x);
break;
case 4:
PopBack(pSeq);
break;
case 5:
printf("請輸入元素\n");
scanf("%d", &x);
PushFront(pSeq, x);
break;
case 6:
PopFront(pSeq);
break;
case 7:
printf("請輸入刪除位置\n");
scanf("%d", &pos);
Erase(pSeq, pos);
break;
case 8:
printf("請輸入元素\n");
scanf("%d", &x);
Remove(pSeq, x);
break;
case 9:
printf("請輸入元素\n");
scanf("%d", &x);
RemoveAll(pSeq, x);
break;
case 10:
BubbleSort(pSeq);
break;
case 11:
printf("請輸入元素\n");
scanf("%d", &x);
ret=BinarySearch(pSeq, x);
if (ret == -1)
printf("沒有這個元素\n");
printf("下標為:%d\n", ret);
break;
case 12:
DestorySeqlist(pSeq);
break;
case 13:
InsertSort(pSeq);
break;
case 14:
SelectSort(pSeq);
break;
default:
printf("輸入無效\n");
break;
}
}
}
int main()
{
Seqlist pSeq;
test(&pSeq);
system("pause");
return 0;
}以上是“web中如何實現實現順序表”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。