step3 . day1 数据结构之线性表顺序表
大學沒有選修數據結構,只是在C語言書最后提到過幾種數據的 組織形式,也算眼熟,今天學的順序表感覺還是很容易理解,寫了一個有史以來代碼最長、調試時間最短的代碼,甚是感覺提高了不少,貼上Mark一下,寫注釋的習慣也慢慢養起來了,要不首先坑的就是自己。
吐槽一下,還是分開寫模塊比較看著舒服。
#include<stdio.h>
#include <stdlib.h>
#define N 32
typedef int datetpye; //重定義date數據類型
typedef struct list{ //順序表結構體聲明、重命名
datetpye date[N];
datetpye last;
}seqlist,*seqlist_p;
//創建順序表
seqlist_p Create(){
seqlist_p L = NULL;
L = (seqlist_p)malloc(sizeof(seqlist)); //分配空間
L->last = -1; //聲明順序表為空
return L;
}
//判斷是否位滿表
int seqlist_is_full(seqlist_p L){
return ((L->last) >= (N-1)) ? 1:0;
}
//尾部插入
int seqlist_insert(seqlist_p L,datetpye value){
if(seqlist_is_full(L) == 1){
printf("seqlist is full\n");
return 1;
}
L->last++; //插入后重定位尾部
L->date[L->last] = value;
return 0;
}
//遍歷順序表打印
void seqlist_show(seqlist_p L){
int i;
for(i=0;i<=L->last;i++){
printf("%d ",L->date[i]);
}
puts("");
}
//頭插入
int seqlist_insert_head(seqlist_p L,datetpye value){
if(seqlist_is_full(L) == 1){ //判斷順序表是否已滿
printf("seqlist is full\n");
return -1;
}
int i;
for(i=L->last;i>=0;i--){ //插入位置開始內容依次后移
L->date[i+1] = L->date[i];
}
L->date[0] = value;
L->last++; //尾部記號更新
return 0;
}
//按照位置插入
int seqlist_insert_pos(seqlist_p L,int pos,datetpye value){
if(seqlist_is_full(L) == 1){ //判斷順序表是否已滿
printf("seqlist is full\n");
return -1;
}
if(pos < 0 || pos > L->last+1){ //輸入位置合法判斷
printf("position no allowed\n");
return -2;
}
int i;
for(i=L->last;i>=pos;i--){ //插入位置開始內容依次后移
L->date[i+1] = L->date[i];
}
L->date[pos] = value;
L->last++; //尾部記號更新
return 0;
}
//判斷是否為空函數
int seqlist_is_empty(seqlist_p L){
return L->last == -1 ? 1 : 0;
}
//按照位置刪除
int seqlist_del_pos(seqlist_p L,int pos){
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -999;
}
if(pos < 0 || pos > L->last){ //判斷位置是否合法
printf("position not allowed\n");
return -9999;
}
int i;
datetpye value = L->date[pos];
for(i=pos;i<L->last;i++){ //位置后繼賦前驅
L->date[i] = L->date[i+1];
}
L->last--;
return value;
}
//刪除頭數據
int seqlist_del_head(seqlist_p L){
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -999;
}
int i;
datetpye value = L->date[0];
for(i=0;i<L->last;i++){ //位置后繼賦前驅
L->date[i] = L->date[i+1];
}
L->last--;
return value;
}
//刪除尾數據
int seqlist_del_tail(seqlist_p L){
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -999;
}
int i;
datetpye value = L->date[L->last];
L->last--;
return value;
}
//按值刪除
int seqlist_del_value(seqlist_p L,datetpye value){
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -1;
}
int i,pos;
int flag = 0;
for(i=0;i<=L->last;i++){ //尋找值所在位置
if(value == L->date[i]){
pos = i;
flag =1;
seqlist_del_pos(L,pos);
return pos;
}
}
if(flag == 0){
printf("value not found\n");
return -1;
}
}
//按值查找
int seqlist_serach_value(seqlist_p L,datetpye value){
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -1;
}
int i,pos;
for(i=0;i<=L->last;i++){ //尋找值所在位置
if(value == L->date[i]){
pos = i;
return pos;
}
}
printf("value not found\n");
return -1;
}
//按照位置查找
int seqlist_serach_pos(seqlist_p L,int pos){
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -999;
}
if(pos < 0 || pos > L->last){ //判斷位置是否合法
printf("position not allowed\n");
return -999;
}
return L->date[pos];
}
//按照位置修改
int seqlist_upgrade_pos(seqlist_p L,int pos,datetpye value){
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -999;
}
if(pos < 0 || pos > L->last){ //判斷位置是否合法
printf("position not allowed\n");
return -999;
}
L->date[pos] = value;
return 0;
}
//按值修改
int seqlist_upgrade_value(seqlist_p L,datetpye value,datetpye new_value){
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -1;
}
int i,pos;
int flag = 0;
for(i=0;i<=L->last;i++){ //尋找值所在位置
if(value == L->date[i]){
pos = i;
flag =1;
}
}
if(flag == 0){
printf("value not found\n");
return -1;
}
else{
L->date[pos] = new_value;
return 0;
}
}
//刪除表中重復值
int seqlist_repeat_del(seqlist_p L){
if(seqlist_range(L)){ //判斷是為空表
printf("seqlist is empty\n");
return -999;
}
int i;
for(i=0;i<L->last;i++){
if(L->date[i] == L->date[i+1]){
seqlist_del_pos(L,i+1);
i--;
}
}
}
int seqlist_range(seqlist_p L){ //排序
if(seqlist_is_empty(L)){ //判斷是為空表
printf("seqlist is empty\n");
return 1;
}
int i,j;
for(i=0;i<=L->last;i++){
for(j=0;j<=(L->last-i-1);j++){
if((L->date[j]) > (L->date[j+1])){
L->date[j+1] += L->date[j];
L->date[j] = L->date[j+1] - L->date[j];
L->date[j+1] -= L->date[j];
}
}
}
return 0;
}
//兩個表拼接
int seqlist_cat(seqlist_p L,seqlist_p L1){
if((L->last + L1->last)>=32)
{
printf("out of list area!\n");
return -1;
}
seqlist_repeat_del(L);
seqlist_repeat_del(L1);
int i,j=0;
for(i=(L->last+1);i<(L->last+L1->last+2),j<=L1->last;i++,j++){
L->date[i] = L1->date[j];
L->last++;
}
seqlist_repeat_del(L);
return 0;
}
int main(int argc, const char *argv[])
{
seqlist_p L = Create(); //創建表調用
seqlist_insert(L,4); //尾部插入
seqlist_insert(L,2);
seqlist_insert(L,3);
seqlist_insert(L,7);
seqlist_insert(L,6);
seqlist_insert(L,7);
seqlist_insert(L,3);
seqlist_insert(L,3); //尾部插入
seqlist_show(L); //遍歷
/*
seqlist_insert_pos(L,4,0); //位置插入調用
seqlist_show(L);
seqlist_del_pos(L,4); //位置刪除調用
seqlist_show(L);
seqlist_del_pos(L,7); //位置刪除調用
seqlist_show(L);
seqlist_del_head(L); //頭數據刪除調用
seqlist_show(L);
seqlist_del_tail(L); //尾部刪除調用
seqlist_show(L);
seqlist_del_value(L,7); //非重復值單個刪除調用
seqlist_show(L);
printf("value's pos is %d\n",seqlist_serach_value(L,7)); //非重復值單個刪除調用
seqlist_show(L);
printf("pos's value is %d\n",seqlist_serach_pos(L,9)); //非重復值單個刪除調用
seqlist_show(L);
seqlist_upgrade_pos(L,3,10); //非重復值單個刪除調用
seqlist_show(L);
seqlist_upgrade_value(L,4,11); //非重復值單個刪除調用
seqlist_show(L);
*/
seqlist_repeat_del(L); //非復值單個刪除調用
seqlist_show(L);
seqlist_p L1 = Create(); //創建l1表
seqlist_insert(L1,4); //賦值
seqlist_insert(L1,4);
seqlist_insert(L1,5);
seqlist_insert(L1,5);
seqlist_insert(L1,1);
seqlist_insert(L1,2);
seqlist_insert(L1,2);
seqlist_insert(L1,1); //尾部插入
seqlist_show(L1);
seqlist_repeat_del(L1); //非復值單個刪除調用
seqlist_show(L1);
seqlist_cat(L,L1);
seqlist_show(L);
return 0;
}
轉載于:https://www.cnblogs.com/huiji12321/p/11215413.html
總結
以上是生活随笔為你收集整理的step3 . day1 数据结构之线性表顺序表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AutoHotkey纯命令获取Chrom
- 下一篇: ASP.NET 2.0 中的资源与本地化