C語言實(shí)現(xiàn)順序表的基本操作指南(注釋很詳細(xì))
創(chuàng)建一個(gè)結(jié)構(gòu)體用于存放順序表相關(guān)數(shù)據(jù)
#define SEQTYPE int
typedef struct SeqList
{
SEQTYPE* data;
int size; //有效數(shù)據(jù)個(gè)數(shù)
int capacity; //容量
}SeqList;
初始化順序表
void SeqListInit(SeqList* pq)
{
CheckNull(pq);
pq->data = NULL;
pq->capacity = 0;
pq->size = 0;
}
插入元素
- 插入到表頭;
- 插入到指定位置;
- 插入到尾部;
先檢查容量是否夠用
void CheckCapacity(SeqList* pq)
{
CheckNull(pq);
//如果空間滿了,擴(kuò)容
if (pq->size >= pq->capacity)
{
int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2;
SEQTYPE* new = (SEQTYPE*)realloc(pq->data, sizeof(SEQTYPE) * newcapacity);
if (new == NULL)
{
perror("realloc");
exit(-1);
}
pq->data = new;
pq->capacity = newcapacity;
}
puts("增容成功");
}
//往順序表指定位置插入數(shù)據(jù)
void SeqListInsert(SeqList* pq, int pos)
{
CheckNull(pq);
assert(pos <= pq->size);
SEQTYPE InsertVal;
if (pos == -1)
{
printf("請分別輸入添加的數(shù)據(jù)和位置,空格隔開:>");
scanf("%d %d", &InsertVal, &pos);
if (pos > pq->size)
{
printf("請正確輸入\n");
return;
}
}
else
{
printf("請輸入添加的數(shù)據(jù):>");
scanf("%d", &InsertVal);
}
//檢查容量是否足夠
CheckCapacity(pq);
//插入數(shù)據(jù)
int end = pq->size;
int begin = pos;
while (begin < end)
{
pq->data[end] = pq->data[end - 1];
--end;
}
pq->data[pos] = InsertVal;
++pq->size;
printf("添加成功\n");
}
//往順序表末位置插入數(shù)據(jù)
void SeqListPushBack(SeqList* pq)
{
CheckNull(pq);
SeqListInsert(pq, pq->size);
}
//往順序表首位置插入數(shù)據(jù)
void SeqListPushFront(SeqList* pq)
{
CheckNull(pq);
SeqListInsert(pq, 0);
}
刪除元素
- 刪除首元素;
- 刪除指定位置元素;
- 刪除尾部元素;
//從順序表指定位置刪除數(shù)據(jù)
void SeqListErase(SeqList* pq, int pos)
{
CheckNull(pq);
if (pos == -1)
{
printf("請輸入要?jiǎng)h除數(shù)據(jù)的位置:>");
scanf("%d", &pos);
if (pos < 0 || pos >= pq->size)
{
printf("請正確輸入\n");
return;
}
}
int begin = pos;
int end = pq->size - 1;
while (begin < end)
{
pq->data[begin] = pq->data[begin + 1];
++begin;
}
--pq->size;
puts("刪除成功");
}
//從順序表末位置刪除數(shù)據(jù)
void SeqListPophBack(SeqList* pq)
{
CheckNull(pq);
SeqListErase(pq, pq->size - 1);
}
//從順序表首位置刪除數(shù)據(jù)
void SeqListPophFront(SeqList* pq)
{
CheckNull(pq);
SeqListErase(pq, 0);
}
元素修改
- 找到目標(biāo)元素;
- 直接修改該元素的值;
//修改順序表指定位置數(shù)據(jù)
void SeqListModify(SeqList* pq)
{
CheckNull(pq);
int pos;
SEQTYPE x;
printf("請輸入修改的位置和新的數(shù)據(jù),空格隔開:>");
scanf("%d %d", &pos, &x);
if (pos < 0 && pos >= pq->size)
{
printf("請正確輸入\n");
return;
}
pq->data[pos] = x;
puts("修改成功");
}
查找元素
查找目標(biāo)元素,算法多種,比如二分,插值等等,這里使用順序查找算法,具體代碼如下:
//查找所需數(shù)據(jù)是否存在順序表中
void SeqListFindData(SeqList* pq)
{
CheckNull(pq);
SEQTYPE x;
printf("請輸入要查找的數(shù)據(jù):>");
scanf("%d", &x);
for (int i = 0; i < pq->size; i++)
{
if (pq->data[i] == x)
{
printf("所需查詢數(shù)據(jù)存在,下標(biāo)為:>%d\n", i);
return;
}
}
printf("找不到\n");
}
排序元素
//排序順序表
void SeqListSort(SeqList* pq)
{
CheckNull(pq);
int option = 0;
printf("輸入0為升序,1為降序:>");
scanf("%d", &option);
for (int i = 0; i < pq->size - 1; i++)
{
for (int j = 0; j < pq->size - i - 1; j++)
{
if (pq->data[j] > pq->data[j + 1])
{
SEQTYPE tmp = pq->data[j];
pq->data[j] = pq->data[j + 1];
pq->data[j + 1] = tmp;
}
}
}
if (option)
{
SeqListReverse(pq);
return;
}
}
元素反轉(zhuǎn)
//順序表反轉(zhuǎn)
void SeqListReverse(SeqList* pq)
{
CheckNull(pq);
int left = 0;
int right = pq->size - 1;
while (left < right)
{
SEQTYPE tmp = pq->data[left];
pq->data[left] = pq->data[right];
pq->data[right] = tmp;
++left;
--right;
}
}
源碼
- 以上是順序表常用的功能操作,下面附上完整代碼,VS2019環(huán)境
SeqList.c
#include "SeqList.h"
void CheckNull(SeqList* pq)
{
if (pq == NULL)
{
perror("pq::");
exit(-1);
}
}
//初始化順序表
void SeqListInit(SeqList* pq)
{
CheckNull(pq);
pq->data = NULL;
pq->capacity = 0;
pq->size = 0;
}
void SeqListDestory(SeqList* pq)
{
CheckNull(pq);
free(pq->data);
pq->data = NULL;
pq->size = 0;
pq->capacity = 0;
}
void CheckCapacity(SeqList* pq)
{
CheckNull(pq);
//如果空間滿了,擴(kuò)容
if (pq->size >= pq->capacity)
{
int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2;
SEQTYPE* new = (SEQTYPE*)realloc(pq->data, sizeof(SEQTYPE) * newcapacity);
if (new == NULL)
{
perror("realloc");
exit(-1);
}
pq->data = new;
pq->capacity = newcapacity;
}
puts("增容成功");
}
void SeqListPrint(SeqList* pq)
{
CheckNull(pq);
if (pq->size == 0)
printf("\n");
else
{
for (int i = 0; i < pq->size; i++)
{
printf("%d ", pq->data[i]);
}
puts("\n--------------------------------------");
}
}
//往順序表末位置插入數(shù)據(jù)
void SeqListPushBack(SeqList* pq)
{
CheckNull(pq);
SeqListInsert(pq, pq->size);
}
//往順序表首位置插入數(shù)據(jù)
void SeqListPushFront(SeqList* pq)
{
CheckNull(pq);
SeqListInsert(pq, 0);
}
//往順序表指定位置插入數(shù)據(jù)
void SeqListInsert(SeqList* pq, int pos)
{
CheckNull(pq);
assert(pos <= pq->size);
SEQTYPE InsertVal;
if (pos == -1)
{
printf("請分別輸入添加的數(shù)據(jù)和位置,空格隔開:>");
scanf("%d %d", &InsertVal, &pos);
if (pos > pq->size)
{
printf("請正確輸入\n");
return;
}
}
else
{
printf("請輸入添加的數(shù)據(jù):>");
scanf("%d", &InsertVal);
}
//檢查容量是否足夠
CheckCapacity(pq);
//插入數(shù)據(jù)
int end = pq->size;
int begin = pos;
while (begin < end)
{
pq->data[end] = pq->data[end - 1];
--end;
}
pq->data[pos] = InsertVal;
++pq->size;
printf("添加成功\n");
}
//從順序表指定位置刪除數(shù)據(jù)
void SeqListErase(SeqList* pq, int pos)
{
CheckNull(pq);
if (pos == -1)
{
printf("請輸入要?jiǎng)h除數(shù)據(jù)的位置:>");
scanf("%d", &pos);
if (pos < 0 || pos >= pq->size)
{
printf("請正確輸入\n");
return;
}
}
int begin = pos;
int end = pq->size - 1;
while (begin < end)
{
pq->data[begin] = pq->data[begin + 1];
++begin;
}
--pq->size;
puts("刪除成功");
}
//從順序表末位置刪除數(shù)據(jù)
void SeqListPophBack(SeqList* pq)
{
CheckNull(pq);
SeqListErase(pq, pq->size - 1);
}
//從順序表首位置刪除數(shù)據(jù)
void SeqListPophFront(SeqList* pq)
{
CheckNull(pq);
SeqListErase(pq, 0);
}
//修改順序表指定位置數(shù)據(jù)
void SeqListModify(SeqList* pq)
{
CheckNull(pq);
int pos;
SEQTYPE x;
printf("請輸入修改的位置和新的數(shù)據(jù),空格隔開:>");
scanf("%d %d", &pos, &x);
if (pos < 0 && pos >= pq->size)
{
printf("請正確輸入\n");
return;
}
pq->data[pos] = x;
puts("修改成功");
}
//查找順序表指定位置數(shù)據(jù)
void SeqListFindPos(SeqList* pq)
{
CheckNull(pq);
int pos;
printf("請輸入要查找數(shù)據(jù)的位置:>");
scanf("%d", &pos);
if (pos < 0 && pos >= pq->size)
{
printf("請正確輸入\n");
return;
}
for (int i = 0; i < pq->size; i++)
{
if (pq->data[i] == pq->data[pos])
{
printf("查找位置的數(shù)據(jù)為:>%d\n", pq->data[pos]);
break;
}
}
}
//查找所需數(shù)據(jù)是否存在順序表中
void SeqListFindData(SeqList* pq)
{
CheckNull(pq);
SEQTYPE x;
printf("請輸入要查找的數(shù)據(jù):>");
scanf("%d", &x);
for (int i = 0; i < pq->size; i++)
{
if (pq->data[i] == x)
{
printf("所需查詢數(shù)據(jù)存在,下標(biāo)為:>%d\n", i);
return;
}
}
printf("找不到\n");
}
//排序順序表
void SeqListSort(SeqList* pq)
{
CheckNull(pq);
int option = 0;
printf("輸入0為升序,1為降序:>");
scanf("%d", &option);
for (int i = 0; i < pq->size - 1; i++)
{
for (int j = 0; j < pq->size - i - 1; j++)
{
if (pq->data[j] > pq->data[j + 1])
{
SEQTYPE tmp = pq->data[j];
pq->data[j] = pq->data[j + 1];
pq->data[j + 1] = tmp;
}
}
}
if (option)
{
SeqListReverse(pq);
return;
}
}
//順序表反轉(zhuǎn)
void SeqListReverse(SeqList* pq)
{
CheckNull(pq);
int left = 0;
int right = pq->size - 1;
while (left < right)
{
SEQTYPE tmp = pq->data[left];
pq->data[left] = pq->data[right];
pq->data[right] = tmp;
++left;
--right;
}
}
test.c
#include "SeqList.h"
void menu()
{
printf("######################################################\n");
printf("##### 1. Print 2. Insert #####\n");
printf("##### 3. PushFront 4. PushBack #####\n");
printf("##### 5. PopFront 6. PopBack #####\n");
printf("##### 7. FindPos 8. FindData #####\n");
printf("##### 9. Modify 10. Erase #####\n");
printf("##### 11. EMPTY 12. Sort #####\n");
printf("##### 13. Reverse 0. Exit #####\n");
printf("######################################################\n");
}
enum Option
{
EXIT,
PRINT,
INSERT,
PUSHFRONT,
PUSHBACK,
POPFRONT,
POPBACK,
FINDPOS,
FINDDATA,
MODIFY,
ERASE,
EMPTY,
SORT,
REVERSE,
};
int main()
{
int option = 0;
int posi = -1;
SeqList s;
SeqListInit(&s);
do
{
menu();
printf("請選擇:>");
scanf("%d", &option);
switch (option)
{
case PRINT:
SeqListPrint(&s);
break;
case INSERT:
SeqListInsert(&s, posi);
break;
case PUSHFRONT:
SeqListPushFront(&s);
break;
case PUSHBACK:
SeqListPushBack(&s);
break;
case POPFRONT:
SeqListPophFront(&s);
break;
case POPBACK:
SeqListPophBack(&s);
break;
case FINDPOS:
SeqListFindPos(&s);
break;
case FINDDATA:
SeqListFindData(&s);
break;
case MODIFY:
SeqListModify(&s);
break;
case ERASE:
SeqListErase(&s, posi);
break;
case EMPTY:
SeqListDestory(&s);
break;
case SORT:
SeqListSort(&s);
break;
case REVERSE:
SeqListReverse(&s);
break;
case EXIT:
SeqListDestory(&s);
printf("退出\n");
break;
default:
printf("請正確輸入\n");
break;
}
} while (option);
return 0;
}
SeqList.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
//
#define SEQTYPE int
typedef struct SeqList
{
SEQTYPE* data;
int size; //有效數(shù)據(jù)個(gè)數(shù)
int capacity; //容量
}SeqList;
//初始化順序表
void SeqListInit(SeqList* pq);
//銷毀順序表
void SeqListDestory(SeqList* pq);
//打印順序表
void SeqListPrint(SeqList* pq);
//往順序表指定位置插入數(shù)據(jù)
void SeqListInsert(SeqList* pq, int pos);
//往順序表末位置插入數(shù)據(jù)
void SeqListPushBack(SeqList* pq);
//往順序表首位置插入數(shù)據(jù)
void SeqListPushFront(SeqList* pq);
//從順序表指定位置刪除數(shù)據(jù)
void SeqListErase(SeqList* pq, int pos);
//從順序表末位置刪除數(shù)據(jù)
void SeqListPophBack(SeqList* pq);
//從順序表首位置刪除數(shù)據(jù)
void SeqListPophFront(SeqList* pq);
//修改順序表指定位置數(shù)據(jù)
void SeqListModify(SeqList* pq);
//查找順序表指定位置數(shù)據(jù)
void SeqListFindPos(SeqList* pq);
//查找所需數(shù)據(jù)是否存在順序表中
void SeqListFindData(SeqList* pq);
//排序順序表
void SeqListSort(SeqList* pq);
//順序表反轉(zhuǎn)
void SeqListReverse(SeqList* pq);
總結(jié)
到此這篇關(guān)于C語言實(shí)現(xiàn)順序表的基本操作指南的文章就介紹到這了,更多相關(guān)C語言實(shí)現(xiàn)順序表基本操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(一)
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)與算法中的插入類和交換類的各種排序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12
Visual?C++?6.0添加一個(gè)對話框的實(shí)現(xiàn)步驟
VC6.0是微軟公司推出的一款集成開發(fā)環(huán)境,本文主要介紹了Visual?C++?6.0添加一個(gè)對話框的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
解決vscode下調(diào)試c/c++程序一閃而過的問題(Windows)
這篇文章主要介紹了解決vscode下調(diào)試c/c++程序一閃而過(Windows),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
C語言實(shí)現(xiàn)直角坐標(biāo)轉(zhuǎn)換為極坐標(biāo)的方法
這篇文章主要介紹了C語言實(shí)現(xiàn)直角坐標(biāo)轉(zhuǎn)換為極坐標(biāo)的方法,涉及C語言進(jìn)行三角函數(shù)與數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
C++指針和數(shù)組:字符和字符串、字符數(shù)組的關(guān)聯(lián)和區(qū)別
字符串是一種重要的數(shù)據(jù)類型,但是c語言并沒有顯示的字符串?dāng)?shù)據(jù)類型,因?yàn)樽址宰址A康男问匠霈F(xiàn)或者存儲(chǔ)于字符數(shù)組中。在C++標(biāo)準(zhǔn)模板庫(STL)中提供了string類,實(shí)現(xiàn)了對字符串的封裝。2022-12-12
C++對cin輸入字符的判斷及分段函數(shù)處理方法示例
這篇文章主要介紹了C++對cin輸入字符的判斷及分段函數(shù)處理方法,結(jié)合實(shí)例形式分析了C++輸入判斷及處理相關(guān)操作技巧,需要的朋友可以參考下2017-09-09

