順序表的相關操作。實現了順序表的建立,判斷順序表是否為空,順序表的插入,順序表的刪除(下標),順序表的查詢,刪除表中一元素,列印順序表的功能。
#include
#include
#include
#define false 0
#define true 1
typedef int datatype;
// 順序表資料型別
struct seqlist
;typedef struct seqlist *pseqlist;
// 順序表建立的演算法,m為申請的結點個數
pseqlist createnulllist_seq(int m)
else
}printf("out of space!\n");
return null;
}// 順序表判空
int isnulllist_seq(pseqlist palist)
// 順序表插入,在下標為p的元素之前插入元素
// 1.移動結點 2.插入結點 3.增加表長
// 先看錶空間是否已滿,檢查插入位置的有效性0
<=pint insertpre_seq(pseqlist palist, int p, datatype x)
if (p<0 || p>palist->n)
for(q=palist->n-1; q>=p; q--)
palist->element[q+1] = palist->element[q];
palist->element[p] = x;
palist->n = palist->n+1;
return true;
}// 順序表刪除,刪除下標為p的元素
// 檢查刪除位置有效性 0
<=pint deletep_seq(pseqlist palist, int p)
for(q=p; qn-1; q++)
palist->n = palist->n-1;
return true;
}// 順序表的查詢。存在返回下標,不存在返回false
int locate_seq(pseqlist palist, int x)
}return false;
}// 刪除表中一元素
int deletev_seq(pseqlist palist, datatype x)
}return true;
}// 列印順序表
void print(pseqlist palist)
int main()
printf("對陣列賦初值:\n");
print(lx_alist);
printf("\n修改其中乙個元素的值:\n");
lx_alist->element[5] = 9;
print(lx_alist);
printf("\n刪除剛改的元素的值:\n");
temp = 9;
deletev_seq(lx_alist, temp);
print(lx_alist);
printf("現在所含元素個數為:%d\n", lx_alist->n);
temp = 5;
printf("\n在第%d個元素前插入乙個數:\n", temp);
insertpre_seq(lx_alist, temp, 11);
print(lx_alist);
system("pause");
return
0;}
初學資料結構,有的**寫的不是很簡潔。輕噴。 資料結構 順序表
順序表的特徵 1由唯一的表名標識 2佔據一塊連續的儲存空間 3資料順序存放,元素之間有先後關係 定義動態的順序表 define maxsize 100 typedef struct sqlist 這個結構體型別存放的是順序表的資訊和順序表的資料 初始化順序表 void initsqlist sqli...
資料結構 順序表
順序表示最簡單的乙個資料結構,直接貼 吧,因為比較簡單。include include typedef struct sqlist sqlist void initlist sqlist l l length 0 void getelem sqlist l 初始化 l length j printf...
資料結構順序表
include include include include include include include include include include include include include include using namespace std define maxn 100000...