新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
c語言復習之順序表(十五)-創(chuàng)新互聯(lián)
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)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站欄目:c語言復習之順序表(十五)-創(chuàng)新互聯(lián)
當前地址:http://www.dlmjj.cn/article/codiog.html