线性顺序表实现(C语言)
2018-02-04 13:45
344 查看
1. SeqList.h
#ifndef _LINKLIST_H
#define _LINKLIST_H
typedef void SeqList;
typedef void SeqListNode;
SeqList* SeqList_Create(intcapacity);
int SeqList_Insert(SeqList*list,SeqListNode*node,
int position);
SeqListNode* SeqList_Get(SeqList*list,intposition);
SeqListNode* SeqList_Delete(SeqList*list,intposition);
int SeqList_GetLength(SeqList*list);
int SeqList_GetCapacity(SeqList*list);
void SeqList_Destroy(SeqList*list);
void SeqList_Clear(SeqList*list);
#endif
2. SeqList.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SeqList.h"
typedef struct _Tag_SeqList{
int capacity;
int length;
unsigned int ** node;
} TSeqList;
SeqList* SeqList_Create(intcapacity) {
int ret = 0;
TSeqList* list = (TSeqList*)malloc(sizeof(TSeqList));
if (list == NULL) {
ret = -1;
printf("function SeqList_Create error: malloc error, ret = %d", ret);
}
memset(list, 0, sizeof(list));
list->node = (unsigned int **)malloc(sizeof(unsignedint *) *capacity);
if (list->node == NULL) {
ret = -2;
printf("function SeqList_Create error: malloc error, ret = %d", ret);
}
list->capacity = capacity;
list->length = 0;
return list;
}
int SeqList_Insert(SeqList*list,SeqListNode*node,
int position) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Insert error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Insert error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (tlist->length >= tlist->capacity) {
ret = -3;
printf("function SeqList_Insert error: ret = %d", ret);
}
if (position >= tlist->length) {
position = tlist->length;
}
int i = 0;
for (i = tlist->length; i >
position; i--) {
tlist->node[i] = tlist->node[i-1];
}
tlist->node[i] = (unsigned
int *)node;
tlist->length++;
return ret;
}
SeqListNode* SeqList_Get(SeqList*list,intposition) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Get error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Get error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (position >= tlist->length) {
ret = -3;
printf("function SeqList_Get error: ret = %d", ret);
}
return tlist->node[position];
}
SeqListNode* SeqList_Delete(SeqList*list,intposition) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Delete error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Delete error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (position >= tlist->length) {
ret = -3;
printf("function SeqList_Delete error: ret = %d", ret);
}
SeqListNode* node = tlist->node[position];
for (int i =position; i < tlist->length-1; i++) {
tlist->node[i] = tlist->node[i+1];
}
tlist->length--;
return node;
}
int SeqList_GetLength(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_GetLength error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
return tlist->length;
}
int SeqList_GetCapacity(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_GetCapacity error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
return tlist->capacity;
}
void SeqList_Destroy(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Destroy error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (tlist->node != NULL) {
free(tlist->node);
tlist->node = NULL;
}
if (tlist != NULL) {
free(tlist);
tlist = NULL;
}
return;
}
void SeqList_Clear(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Clear error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
tlist->length = 0;
return;
}
3. SeqListMain.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SeqList.h"
typedef struct Teacher {
int age;
char name[64];
} Teacher;
void main() {
SeqList* list = SeqList_Create(10);
if (list == NULL) {
return;
}
Teacher t1, t2, t3;
strcpy(t1.name, "A");
strcpy(t2.name, "B");
strcpy(t3.name, "C");
SeqList_Insert(list, (SeqListNode*)&t1, 0);
SeqList_Insert(list, (SeqListNode*)&t2, 0);
SeqList_Insert(list, (SeqListNode*)&t3, 0);
int length = SeqList_GetLength(list);
for (int i = 0; i < length; i++) {
Teacher* t = (Teacher*)SeqList_Get(list, i);
printf("%s\n", t->name);
}
printf("delete\n");
while (SeqList_GetLength(list) > 0) {
Teacher* t = (Teacher*)SeqList_Delete(list, 0);
printf("%s\n", t->name);
}
printf("length:%d\n", SeqList_GetLength(list));
printf("capacity:%d\n", SeqList_GetCapacity(list));
SeqList_Clear(list);
SeqList_Destroy(list);
system("pause");
}
#ifndef _LINKLIST_H
#define _LINKLIST_H
typedef void SeqList;
typedef void SeqListNode;
SeqList* SeqList_Create(intcapacity);
int SeqList_Insert(SeqList*list,SeqListNode*node,
int position);
SeqListNode* SeqList_Get(SeqList*list,intposition);
SeqListNode* SeqList_Delete(SeqList*list,intposition);
int SeqList_GetLength(SeqList*list);
int SeqList_GetCapacity(SeqList*list);
void SeqList_Destroy(SeqList*list);
void SeqList_Clear(SeqList*list);
#endif
2. SeqList.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SeqList.h"
typedef struct _Tag_SeqList{
int capacity;
int length;
unsigned int ** node;
} TSeqList;
SeqList* SeqList_Create(intcapacity) {
int ret = 0;
TSeqList* list = (TSeqList*)malloc(sizeof(TSeqList));
if (list == NULL) {
ret = -1;
printf("function SeqList_Create error: malloc error, ret = %d", ret);
}
memset(list, 0, sizeof(list));
list->node = (unsigned int **)malloc(sizeof(unsignedint *) *capacity);
if (list->node == NULL) {
ret = -2;
printf("function SeqList_Create error: malloc error, ret = %d", ret);
}
list->capacity = capacity;
list->length = 0;
return list;
}
int SeqList_Insert(SeqList*list,SeqListNode*node,
int position) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Insert error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Insert error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (tlist->length >= tlist->capacity) {
ret = -3;
printf("function SeqList_Insert error: ret = %d", ret);
}
if (position >= tlist->length) {
position = tlist->length;
}
int i = 0;
for (i = tlist->length; i >
position; i--) {
tlist->node[i] = tlist->node[i-1];
}
tlist->node[i] = (unsigned
int *)node;
tlist->length++;
return ret;
}
SeqListNode* SeqList_Get(SeqList*list,intposition) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Get error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Get error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (position >= tlist->length) {
ret = -3;
printf("function SeqList_Get error: ret = %d", ret);
}
return tlist->node[position];
}
SeqListNode* SeqList_Delete(SeqList*list,intposition) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Delete error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Delete error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (position >= tlist->length) {
ret = -3;
printf("function SeqList_Delete error: ret = %d", ret);
}
SeqListNode* node = tlist->node[position];
for (int i =position; i < tlist->length-1; i++) {
tlist->node[i] = tlist->node[i+1];
}
tlist->length--;
return node;
}
int SeqList_GetLength(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_GetLength error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
return tlist->length;
}
int SeqList_GetCapacity(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_GetCapacity error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
return tlist->capacity;
}
void SeqList_Destroy(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Destroy error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (tlist->node != NULL) {
free(tlist->node);
tlist->node = NULL;
}
if (tlist != NULL) {
free(tlist);
tlist = NULL;
}
return;
}
void SeqList_Clear(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Clear error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
tlist->length = 0;
return;
}
3. SeqListMain.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SeqList.h"
typedef struct Teacher {
int age;
char name[64];
} Teacher;
void main() {
SeqList* list = SeqList_Create(10);
if (list == NULL) {
return;
}
Teacher t1, t2, t3;
strcpy(t1.name, "A");
strcpy(t2.name, "B");
strcpy(t3.name, "C");
SeqList_Insert(list, (SeqListNode*)&t1, 0);
SeqList_Insert(list, (SeqListNode*)&t2, 0);
SeqList_Insert(list, (SeqListNode*)&t3, 0);
int length = SeqList_GetLength(list);
for (int i = 0; i < length; i++) {
Teacher* t = (Teacher*)SeqList_Get(list, i);
printf("%s\n", t->name);
}
printf("delete\n");
while (SeqList_GetLength(list) > 0) {
Teacher* t = (Teacher*)SeqList_Delete(list, 0);
printf("%s\n", t->name);
}
printf("length:%d\n", SeqList_GetLength(list));
printf("capacity:%d\n", SeqList_GetCapacity(list));
SeqList_Clear(list);
SeqList_Destroy(list);
system("pause");
}
相关文章推荐
- 线性表——顺序结构(c语言实现)
- 数据结构--线性表.顺序表(C语言实现)
- 利用线性表的顺序结构求集合的并、交、差、补(C语言实现)
- 动态分配的顺序线性表的十五种操作—C语言实现
- 线性表的顺序实现(c语言)
- c语言的线性表的顺序存储的实现,数组
- C语言实现线性表之顺序表
- 线性表的顺序静态存储--C语言实现
- 线性表的顺序方式实现(c语言)
- 线性表的顺序表示和实现(C语言)
- 线性表的顺序实现(C语言)
- C语言--线性表的顺序存储的实现
- C语言实现顺序线性表的表示、插入、删除
- 线性表的顺序动态存储——C语言实现简单函数
- 顺序线性表的基本操作(C语言实现)
- 线性表顺序实现(C语言)
- 线性表的顺序表示和实现(C语言描述)
- 数据结构4:顺序表(线性表的顺序存储结构)及C语言实现
- 线性表的顺序表示(C语言实现)
- C语言线性表的顺序表示与实现实例详解