【数据结构之线性表的顺序存储】用指针的方式实现

来源:互联网 发布:数控编程视频教程 编辑:程序博客网 时间:2024/06/06 17:30

线性表的顺序存储实际很简单,但是本文努力将其写的更加规范。

代码 C

seqlist.h

#ifndef _SEQLIST_H_#define _SEQLIST_H_typedef void List;typedef void ListNode;//创建并且返回一个线性表List* List_Create(int capacity);//销毁一个线性表listvoid List_Destroy(List* list);//将一个线性表list中的所有元素清空, 线性表回到创建时的初始状态void List_Clear(List* list);//返回一个线性表list中的所有元素个数int List_Length(List* list);//向一个线性表list的pos位置处插入新元素nodeint List_Insert(List* list, ListNode* node, int pos);//获取一个线性表list的pos位置处的元素ListNode* List_Get(List* list, int pos);//删除一个线性表list的pos位置处的元素返回值为被删除的元素,NULL表示删除失败ListNode* List_Delete(List* list, int pos);#endif

seqlist.c

#include "seqlist.h"#include <stdlib.h>#include <stdio.h>#include <string.h>typedef struct SeqList{int capacity;int length;unsigned int **node;//相当于unsigned int *arrqy[]}SeqList;//创建并且返回一个线性表List* List_Create(int capacity){int ret = 0;SeqList *seqlist = (SeqList*)malloc(sizeof(SeqList));if (seqlist == NULL){ret = -1;printf("fuc List_Create() err:%d\n", ret);}memset(seqlist, 0, sizeof(SeqList));seqlist->node = (unsigned int**)malloc(sizeof(unsigned int*) * capacity);if (seqlist->node == NULL){ret = -2;printf("fuc List_Create() err:%d\n", ret);return NULL;}seqlist->capacity = capacity;seqlist->length = 0;return seqlist;}//销毁一个线性表listvoid List_Destroy(List* list){SeqList *temp = NULL;if (list == NULL){return;}temp = (SeqList *)list;if (temp->node != NULL){free(temp->node);temp->node = NULL;}free(temp);temp = NULL;}//将一个线性表list中的所有元素清空, 线性表回到创建时的初始状态void List_Clear(List* list){SeqList *temp = NULL;if (list == NULL){return;}temp = (SeqList *)list;temp->length = 0;}//返回一个线性表list中的所有元素个数int List_Length(List* list){SeqList *temp = NULL;if (list == NULL){return -1;}temp = (SeqList *)list;return temp->length;}//向一个线性表list的pos位置处插入新元素nodeint List_Insert(List* list, ListNode* node, int pos){int ret = 0;int i;SeqList *temp = NULL;if (list == NULL || node == NULL|| pos<0){ret = -1;printf("fuc List_Insert() err:%d\n", ret);return ret;}temp = (SeqList *)list;//判断是不是满了if (temp->length >= temp->capacity){ret = -2;printf("fuc List_Insert() err:%d\n", ret);return ret;}//如果插入的位置如果不连续则修正if (pos > temp->length){pos = temp->length;}//元素后移for (i = temp->length; i > pos; i--){temp->node[i] = temp->node[i - 1];}//插入元素temp->node[pos] = node;//temp->node是二级指针,temp->node[pos]是一级指针temp->length++;return 0;}//获取一个线性表list的pos位置处的元素ListNode* List_Get(List* list, int pos){int ret = 0;SeqList *temp = NULL;if (list == NULL || pos<0){ret = -1;printf("fuc List_Get() err:%d\n", ret);return NULL;}temp = (SeqList *)list;return (ListNode*)temp->node[pos];}//删除一个线性表list的pos位置处的元素返回值为被删除的元素,NULL表示删除失败ListNode* List_Delete(List* list, int pos){int ret = 0;int i;SeqList *temp = NULL;ListNode *node1 = NULL;if (list == NULL || pos<0){ret = -1;printf("fuc List_Get() err:%d\n", ret);return NULL;}temp = (SeqList *)list;node1 = temp->node[pos];//元素前移/*for (i = temp->length; i>pos; i--){temp->node[i - 1] = temp->node[i];//切勿这样写,因为每次前移之后,后面的就会覆盖前面的}*/for (i = pos; i < temp->length; i++){temp->node[i] = temp->node[i + 1];//元素前移}temp->length--;return (ListNode*)node1;}

main.c

#include <stdio.h>#include <stdlib.h>#include "seqlist.h"typedef struct Teacher{char name[20];int age;}Teacher;int main(vooid){Teacher t1, t2, t3;t1.age = 20;t2.age = 30;t3.age = 40;List* mylist = List_Create(10);if (mylist == NULL){printf("fuc  List_Create() err:%d\n");return -1;}int ret1 = List_Insert(mylist, &t1, 0);int ret2 = List_Insert(mylist, &t2, 0);int ret3 = List_Insert(mylist, &t3, 0);for (int i = 0; i < List_Length(mylist);i++){Teacher* re = (Teacher*)List_Get(mylist, i);printf("年龄%d为age=%d\n",i, re->age);}List_Delete(mylist, 0);printf("在0号位置删除一个元素之后\n");for (int i = 0; i < List_Length(mylist); i++){Teacher* re = (Teacher*)List_Get(mylist, i);printf("年龄%d为age=%d\n", i, re->age);}List_Clear(mylist);system("pause");}


执行结果:




0 0
原创粉丝点击