2.定義
- 數(shù)組(存儲):
- 靜態(tài)數(shù)組
- 動態(tài)
int* parr=calloc(size,sizeof(type));
- 容量:capacity
- 有效元素個數(shù):size
typedef int Elementtype; //ADT抽象數(shù)據(jù)類型
typedef struct seqlist
{int capacity; //容量
int size; //有效元素個數(shù)
Elementtype* data; //定義數(shù)組指針
}seqlest;
3.斷言#include
assert(...); //斷言...為真,如果不為真,則會中斷程序
4.初始化//法一:
void seqlist_init(seqlist* list)
{list->size = 0;
list->capacity = 8;
list->data = calloc(list->capacity,sizeof(Elementtype));
if (!list->data)
{assert(list->data);
}
}
//法二:
seqlist* seqlist_creat()
{seqlist* list = calloc(1, sizeof(seqlist));
if(!list)
{assert(list);
}
seqlist_init(list);
return list;
}
void seqlist_destory(seqlist* list)
{//先釋放數(shù)組
free(list->data);
//再釋放順序表
if (list)
{free(list);
}
}
5.元素的插入
1.尾插//尾插
void seqlist_pushback(seqlist* list, Elementtype val)
{if(seqlist_full(list))
{printf("seqlist is full , can't insert ! \n");
return;
}
list->data[list->size++] = val;
}
2.頭插//頭插
void seqlist_pushfront(seqlist* list, Elementtype val)
{if (seqlist_full(list))
{printf("seqlist is full , can't insert !\n");
return;
}
//先移動后面的元素,為新插入的元素的元素的騰出空間
for ( int i = list->size; i >0 ; i--)
{list->data[i] = list->data[i - 1];
}
//插入新元素
list->data[0] = val;
list->size++;
}
3.任意插//任意插
void seqlist_insert(seqlist* list, int pos, Elementtype val)
{if (seqlist_full(list))
{printf("seqlist is full , can't insert !\n");
return;
}
//判斷pos是否合法
if( pos< 0 || pos >list->size )
{pos = list->size;
}
//移動
for ( int i = list->size ; i >pos ; i--)
{list->data[i] = list->data[i - 1];
}
list->data[pos] = val;
list->size++;
}
6.元素的刪除
1.尾刪//尾刪
void seqlist_popback(seqlist* list)
{if(seqlist_empty(list))
{printf("seqlist is emoty , can't delete !\n");
return;
}
list->size--;
}
2.頭刪//頭刪
void seqlist_popfront(seqlist* list)
{if (seqlist_empty(list))
{printf("seqlist is emoty , can't delete !\n");
return;
}
for ( int i = 0; i< list->size-1; i++)
{list->data[i] = list->data[i + 1];
}
list->size--;
}
3.查找//查找
int seqlist_find(seqlist* list, Elementtype val)
{for (int i = 0; i< list->size; i++)
{if (list->data[i] == val)
{ return i;
}
}
return -1;
}
4.指定刪(元素)//指定刪(元素)
void seqlist_removeOne(seqlist* list, Elementtype val)
{if (seqlist_empty(list))
{printf("seqlist is emoty , can't delete !\n");
return;
}
if(seqlist_find(list,val))
{for (int j = seqlist_find(list, val); j< list->size - 1; j++)
{ list->data[j] = list->data[j + 1];
}
}
list->size--;
}
5.指定位置刪除//指定位置刪除
void seqlist_removePos(seqlist* list, int pos)
{if (seqlist_empty(list))
{printf("seqlist is emoty , can't delete !\n");
return;
}
assert(pos >0 && pos< list->size);
for (int i = pos; i< list->size-1 ; i++)
{list->data[i] = list->data[i + 1];
}
list->size--;
}
7.擴容bool inc(seqlist* list)
{seqlist* newlist = realloc(list->data, list->capacity + list->capacity * 2, sizeof(Elementtype));
if( !newlist )
{return false;
}
list->data = newlist;
list->capacity += list->capacity * 2;
return true;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
本文名稱:c語言復(fù)習(xí)之順序表(十五)-創(chuàng)新互聯(lián)
URL分享:http://aaarwkj.com/article6/codiog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站內(nèi)鏈、用戶體驗、建站公司、關(guān)鍵詞優(yōu)化、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容