先貼出前台測試**
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "seqlist.h"
typedef struct _teacher
teacher;
void main()
//迴圈刪除
for (i=0; i
順序表**
#ifndef __my_seqlist_h__
#define __my_seqlist_h__
typedef void seqlist;
typedef void seqlistnode;
seqlist* seqlist_create(int capacity);
void seqlist_destroy(seqlist* list);
void seqlist_clear(seqlist* list);
int seqlist_length(seqlist* list);
int seqlist_capacity(seqlist* list);
int seqlist_insert(seqlist* list, seqlistnode* node, int pos);
seqlistnode* seqlist_get(seqlist* list, int pos);
seqlistnode* seqlist_delete(seqlist* list, int pos);
#endif //__my_seqlist_h__
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "seqlist.h"
typedef struct _tag_seqlist
tseqlist;
seqlist* seqlist_create(int capacity)
// 這是比較複雜的一種寫法,下面會有一種比較巧妙和方便的一種寫法
// // seqlist->node = (unsigned int*)malloc(capacity * sizeof(unsigned int));
// if (!seqlist->node)
// // seqlist->ncapacity = capacity;
// seqlist->nlength = 0;
//}// 下面這種方法比較簡單
// 直接申請一塊連續的記憶體,其大小可以容納tseqlist和其中的node
// 然後seqlist + 1指向該記憶體tseqlist的末尾,所以剩下的一塊記憶體就可以強制轉換成node的記憶體空間了
// 這樣的話不單單申請的時候沒有那麼複雜,釋放的時候也很簡單,直接釋放就可以
seqlist->node = (unsigned int *)(seqlist + 1); //
seqlist->ncapacity = capacity;
seqlist->nlength = 0;
} return seqlist;
}void seqlist_destroy(seqlist* list)
// 對應比較複雜的申請方式
//if (seqlist->node)
// //free(seqlist);
// 對應簡單的申請方式
free(seqlist);
}void seqlist_clear(seqlist* list)
seqlist->nlength = 0;
}int seqlist_length(seqlist* list)
return seqlist->nlength;
}int seqlist_capacity(seqlist* list)
return seqlist->ncapacity;
}int seqlist_insert(seqlist* list, seqlistnode* node, int pos)
if (pos < 0 || pos >= seqlist->ncapacity)
if (seqlist->nlength + 1 >= seqlist->ncapacity)
for (i = seqlist->nlength; i > pos; i--)
seqlist->node[pos] = (unsigned int)node;
seqlist->nlength++;
return 0;
}seqlistnode* seqlist_get(seqlist* list, int pos)
if (pos > seqlist->nlength)
return (seqlistnode*)seqlist->node[pos];
}seqlistnode* seqlist_delete(seqlist* list, int pos)
if (pos > seqlist->nlength)
rev = (seqlistnode*)seqlist->node[pos];
for (i = pos; i + 1 <= seqlist->nlength; i++)
seqlist->nlength--;
return rev;
}
順序儲存方式儲存線性表
include 標頭檔案 include define list init size 100 定義儲存容量 define listincremnet 10 擴充套件儲存容量 define overflow 0 允許溢位為0 定義順序表的結構 typedef struct listonde 自定義順序...
順序儲存線性表實現
在計算機中用一組位址連續的儲存單元依次儲存線性表的各個資料元素,稱作線性表的順序儲存結構。順序儲存結構的主要優點是節省儲存空間,因為分配給資料的儲存單元全用存放結點的資料 不考慮c c 語言中陣列需指定大小的情況 結點之間的邏輯關係沒有占用額外的儲存空間。採用這種方法時,可實現對結點的隨機訪問,即每...
線性表 順序儲存實現
linearlist.h pragma once const int maxsize 20 typedef int datatype class dataarr class linearlist linearlist.cpp include linearlist.h include using na...