寫了一下順序鍊錶,為了防止自己忘記,於是乎寫在部落格裡方便檢視。
程式中的容錯處理做的不好,是我懶了,就簡單處理了一下
下面才給出這兒一套api函式
標頭檔案:
#ifndef __seqlist_h__
#define __seqlist_h__
#include #include #include typedef void seqlist;
typedef void seqlistnode;
seqlist * seqlist_create(int capacity);//建立鍊錶 capacity 為鍊錶的容量
void seqlist_destroy(seqlist* list); //析構鍊錶 free記憶體
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
然後是標頭檔案中函式的實現:
#include "seqlist.h"
typedef struct tseqlist
tseqlist;
seqlist * seqlist_create(int capacity)
tseqlist *list = null;
list = (tseqlist *)malloc(sizeof(tseqlist));
if (list == null)
memset(list, 0, sizeof(tseqlist));
list->capacity = capacity;
list->length = 0;
list->node = (unsigned int *)malloc(sizeof(unsigned int )*capacity);
return (seqlist *)list;
}void seqlist_destroy(seqlist* list)
tseqlist * tmp = null;
tmp = (tseqlist *)list;
if (tmp == null)
if (tmp->node != null)
free(list);
return;
}void seqlist_clear(seqlist* list)
tseqlist * tmp = null;
tmp = (tseqlist *)list;
if (tmp == null)
tmp->length = 0;
return;
}int seqlist_length(seqlist* list)
tseqlist * tmp = null;
tmp = (tseqlist *)list;
if (tmp == null)
int ret = 0;
ret = tmp->length;
return ret;
}int seqlist_capacity(seqlist* list)
return tmp->capacity;
}int seqlist_insert(seqlist* list, seqlistnode* node, int pos)
tseqlist * tmp = null;
tmp = (tseqlist *)list;
if (tmp == null)
if (tmp->length >= tmp->capacity)//檢查是否已滿
if (pos >= tmp->length)//位置修正
for (int i = tmp->length; i > pos; i--)//陣列中元素後移
tmp->node[pos] = (unsigned int)node;
tmp->length++;
return ret;
}seqlistnode* seqlist_get(seqlist* list, int pos)
tseqlist * tmp = null;
tmp = (tseqlist *)list;
if (tmp == null)
return (seqlistnode*)(tmp->node[pos]);
}seqlistnode* seqlist_delete(seqlist* list, int pos)
tseqlist * tmp = null;
tmp = (tseqlist *)list;
if (tmp == null)
seqlistnode * ret = (seqlistnode *)tmp->node[pos];//快取刪除的節點
for (int i = pos+1; i < tmp->length; i++)
(tmp->length)--;
return ret;
}
下面是簡單的測試程式:
#include "seqlist.h"
typedef struct teacher
teacher;
void main()
int length = seqlist_length(list);
printf("%d\n", length);
ret = seqlist_insert(list, (seqlistnode*) &t1, 0);
if (ret != 0)
ret = seqlist_insert(list, (seqlistnode*)&t2, 0);
if (ret != 0)
ret = seqlist_insert(list, (seqlistnode*)&t3, 0);
if (ret != 0)
length = seqlist_length(list);
printf("%d\n", length);
int capacity = seqlist_capacity(list);
printf("%d\n", capacity);
for (int i = 0; i < length; i++)
else
}teacher *t4 =(teacher *) seqlist_delete(list, 0);
printf("%d", t4->age);
seqlist_destroy(list);
system("pause");
}
順序鍊錶的實現
1 include2 define max 5034 define ok 1 5using namespace std 6 順序鍊錶c語言版 a1,a2.an 7 typedef int elemtype 8 typedef struct s順序鍊錶定義 9listlink 1314 int lis...
順序鍊錶的實現和鍊錶查詢的實現
程式實現鍊錶的建立,鍊錶的查詢功能 create 方法實現了順序鍊錶的建立,並呼叫鍊錶查詢模組的find 方法 find 方法實現順序鍊錶的查詢 include define maxsize 100 定義乙個陣列的長度 定義乙個連續陣列sqlist typedefstructsqlistsqlist...
順序,鍊錶棧的實現
順序棧的實現 include include include define initsize 10 define increment 10 typedef struct sstack sstack,stack void initstack stack st int gettop stack st r...