利用C語言實(shí)現(xiàn)順序表的實(shí)例操作
更新時(shí)間:2016年08月22日 11:59:00 投稿:daisy
順序表是線性表中的一種重要的數(shù)據(jù)結(jié)構(gòu),也是最基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu),所以他不僅是學(xué)習(xí)中的重點(diǎn),也是應(yīng)用開發(fā)非常常用的一種數(shù)據(jù)結(jié)構(gòu)。這篇文章介紹如何利用C語言實(shí)現(xiàn)順序表。
本文實(shí)例講述了C語言實(shí)現(xiàn)順序表(線性表)的方法。分享給大家供大家參考,具體如下:
一:順序表代碼實(shí)現(xiàn)
#ifndef _SEQ_LIST_H
#define _SEQ_LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define ElemType float //以float類型測試算法通用性,而不是以慣用的int
#define INIT_SIZE 10 //順序表默認(rèn)初始化大小
#define LIST_INCREMENT 5 //順序表容量增量,容量不夠使用malloc申請
#define list_full(list) ((list)->size >= (list)->capacity ? 1 : 0) //順序表判滿
#define list_empty(list) ((list)->size == 0 ? 1 : 0) //判空
typedef ElemType value_type; //元素類型
typedef value_type* value_ptr; //元素指針類型
typedef enum {false, true}bool; //為C語言增加bool量
typedef enum {POSITION, VALUE}DELETE_MODE; //刪除元素模式選擇,分別為按位置刪除和按值刪除
typedef struct sequelize_list{
ElemType *base; //順序表基址
int size; //順序表元素大小
int capacity; //順序表容量
} seq_list, *list_ptr;
void init_list(list_ptr lp) //初始化
{
assert(lp != NULL);
lp->base = (value_ptr)malloc(sizeof(value_type)*INIT_SIZE); //free
assert(lp->base != NULL);
memset(lp->base, 0, sizeof(value_type)*INIT_SIZE);
lp->size = 0;
lp->capacity = INIT_SIZE;
}
bool insert_elem(list_ptr lp, const int pos, const value_type elem) //插入元素
{
assert(lp != NULL && pos >= 1 && pos <= lp->size+1);
if(list_full(lp)){ //如果表滿,追加申請空間
value_ptr new_base = (value_ptr)realloc(lp->base,
sizeof(value_type)*(lp->capacity+LIST_INCREMENT));//此處注意realloc用法,用新地址接收是為了防止realloc失敗,原地址有效內(nèi)容被釋放
assert(new_base != NULL); //并且realloc填寫的申請空間大小必須是之前的大小+增量的大小,而不是只寫增量,否則如果原地址內(nèi)存不夠,在新地址申請?jiān)隽看笮〉目臻g,將之前的內(nèi)容挪至新空間,內(nèi)存溢出,將發(fā)生段錯誤
lp->base = new_base; //再賦回給原地址
lp->base[lp->capacity] = elem;
lp->capacity += LIST_INCREMENT;
++lp->size;
}
else{ //未滿,插入元素
for(int i=lp->size-1; i>=pos-1; --i){ //將元素后移,騰出位置
lp->base[i+1] = lp->base[i];
}
lp->base[pos-1] = elem; //插入元素
++lp->size;
//}
return true;
}
bool remove_elem_pos(list_ptr *lp, const int pos) //按位置刪除
{
assert(pos >= 1 && pos <= (*lp)->size);
for(value_ptr ptmp=&(*lp)->base[pos-1]; ptmp<&(*lp)->base[(*lp)->size-1]; ++ptmp)
*ptmp = *(ptmp+1);
(*lp)->base[(*lp)->size-1] = 0;
--(*lp)->size;
return true;
}
bool remove_elem_val(list_ptr *lp, value_type elem) //按值刪除
{
for(int i=0; i<(*lp)->size; ++i)
if((*lp)->base[i] == elem){
for(int j=i; j<(*lp)->size-1; ++j) //所有符合要求的值都被刪除
(*lp)->base[j] = (*lp)->base[j+1];
(*lp)->base[(*lp)->size-1] = 0;
--(*lp)->size;
}
return true;
}
bool remove_elem(list_ptr lp, const void* clue, DELETE_MODE mode) //外部接口,第三個(gè)參數(shù)選擇按位置或按值刪除模式
{
assert(lp != NULL);
if(list_empty(lp)){ //表空,不能刪
fprintf(stdout, "already empty, can not remove.\n");
return false;
}
if(mode == POSITION){ //參數(shù)為POSITION,按位置刪除
remove_elem_pos(&lp, *(int *)clue);
}
else{ //否則按值刪除
remove_elem_val(&lp, *(value_ptr)clue);
}
return true;
}
void print_list(const seq_list sl) //打印函數(shù)
{
if(sl.size == 0)
fprintf(stdout, "nil list.");
for(int i=0; i<sl.size; ++i)
printf("%f ", sl.base[i]);
printf("\n");
}
int compare(const void *vp1, const void* vp2) //比較函數(shù)
{
return *(value_ptr)vp1 - *(value_ptr)vp2;
}
int locate_elem(const seq_list sl, const value_type elem,
int (*compare)(const void *, const void *)) //定位函數(shù)
{
if(list_empty(&sl)){
fprintf(stdout, "list empty, con not locate.\n");
}
else{
for(int i=0; i<sl.size; ++i)
if((*compare)(&sl.base[i], &elem) == 0) //相等則找到,返回位置
return i+1;
}
return 0;
}
void sort_list(list_ptr lp, int (*compare)(const void *, const void *)) //排序函數(shù)
{
assert(lp != NULL);
qsort(lp->base, lp->size, sizeof(value_type), compare); //調(diào)用系統(tǒng)快速排序
}
void merge_list(list_ptr lpa, list_ptr lpb, list_ptr combine,
int (*compare)(const void *, const void *)) //合并兩個(gè)順序表
{
assert(lpa != NULL && lpb != NULL && combine != NULL);
combine->base = (value_ptr)malloc(sizeof(value_type)
*(lpa->size+lpb->size)); //此處為新表申請空間,因此新表無需另外調(diào)用初始化函數(shù),否則會發(fā)生內(nèi)存泄漏
assert(combine->base != NULL);
combine->capacity = combine->size = lpa->size+lpb->size;
value_ptr it_pc = combine->base;
value_ptr it_pa=lpa->base;
value_ptr it_pb=lpb->base;
while(it_pa<lpa->base+lpa->size && it_pb<lpb->base+lpb->size){ //由小到大插入新表
if(compare(it_pa, it_pb) <= 0)
*it_pc++ = *it_pa++;
else
*it_pc++ = *it_pb++;
}
while(it_pa < lpa->base+lpa->size) //從上面while出循環(huán)只有兩種情況,此處為pa為插完,pb已插完,pa剩余元素直接插入,天然有序
*it_pc++ = *it_pa++;
while(it_pb < lpb->base+lpb->size) //同理,若pb剩有元素,直接插入,天然有序
*it_pc++ = *it_pb++;
}
#endif /*seq_list*/
二:測試代碼
為保證通用性,不用int,用float測試。
#include "seq_list.h"
#define ARRAY_SIZE 10
int main()
{
value_type array[] = {39.1, 32.1, 10.1, 11.1, 22.1, 5.1, 36.1, 28.1, 46.1, 32.1};
seq_list list; //順序表1
init_list(&list);
for(int i=0; i<ARRAY_SIZE; ++i)
insert_elem(&list, 1, array[i]);
printf("list:\n");
print_list(list);
#if 1
int clue = 1; //按順序刪除,刪除第一個(gè)元素
//value_type clue = 32.1; //按值刪除,刪除值為32.1的元素,需修改下面參數(shù)為VALUE
printf("after remove:\n");
remove_elem(&list, &clue, POSITION);
print_list(list);
#endif
#if 1
int found = locate_elem(list, 22.1, compare); //定位22.1元素所在位置
if(found >= 1)
fprintf(stdout, "found %f at the position %d\n", list.base[found-1], found);
else
fprintf(stdout, "not found.\n");
#endif
sort_list(&list, compare);
printf("list after sort:\n");
print_list(list);
value_type array2[] = {98.1, 65.1, 4.1, 86.1, 34.1, 21.1, 86.1, 74.1, 93.1, 46.1};
seq_list list2;
init_list(&list2);
for(int i=0; i<ARRAY_SIZE; ++i)
insert_elem(&list2, 1, array2[i]);
printf("list2:\n");
print_list(list2);
printf("list2 after sort:\n");
sort_list(&list2, compare);
print_list(list2);
seq_list new_list; //無需調(diào)用init函數(shù)初始化,因?yàn)樾卤碓趍erge_list中會初始化。如果強(qiáng)行調(diào)用,那么會出現(xiàn)內(nèi)存泄漏。
merge_list(&list, &list2, &new_list, compare);
printf("new merge_list:\n");
print_list(new_list);
free(list.base);
free(list2.base);
free(new_list.base); //三個(gè)釋放,防止內(nèi)存泄漏
return 0;
}
三:測試結(jié)果

四:總結(jié)
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)使用C語言能有所幫助。
相關(guān)文章
C++數(shù)據(jù)結(jié)構(gòu)之雙向鏈表
這篇文章主要為大家詳細(xì)介紹了C++數(shù)據(jù)結(jié)構(gòu)之雙向鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
使用C++實(shí)現(xiàn)Range序列生成器的示例代碼
在C++編程中,經(jīng)常需要迭代一系列數(shù)字或其他可迭代對象,本文將使用C++來實(shí)現(xiàn)一個(gè)簡單的Range封裝,文中的示例代碼講解詳細(xì),感興趣的可以了解下2023-11-11
C語言數(shù)據(jù)結(jié)構(gòu)之使用鏈表模擬棧的實(shí)例
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之使用鏈表模擬棧的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-08-08

