C语言线性表链式存储

作者: 忆往 分类: C++の生涯,学习の生涯 发布时间: 2018-06-26 11:49

线性结构的链式存储方法

中心思想:在结构体内声明一个节点(放在第一个位置) 节点存放的是下一个节点的位置

<linklist.h>

#ifndef _MYLINKLIST_H_
#define _MYLINKLIST_H_

typedef void LinkList;
typedef struct _tag_LinkListNode
{
struct _tag_LinkListNode* next;
}LinkListNode;

LinkList* LinkList_Create();
void LinkList_Destroy(LinkList* list);
void LinkList_Clear(LinkList* list);
int LinkList_Length(LinkList* list);
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);
LinkListNode* LinkList_Get(LinkList* list, int pos);
LinkListNode* LinkList_Delete(LinkList* list, int pos);

#endif

<linklist.c>

#include"linklist.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>


typedef struct _tag_LinkList
{
LinkListNode header;
int length;
}TLinklist;


LinkList* LinkList_Create()
{
TLinklist* ret = NULL;
ret = (TLinklist*)malloc(sizeof(TLinklist));
ret->length = 0;
ret->header.next = NULL;
return ret;
}

void LinkList_Destroy(LinkList* list)
{
if (list != NULL)
{
free(list);
list = NULL;
}
return;
}

void LinkList_Clear(LinkList* list)
{
TLinklist* tlist = NULL;
if (list == NULL) return;
tlist = (TLinklist*)list;
tlist->length = 0;
tlist->header.next = NULL;
return;
}

int LinkList_Length(LinkList* list)
{
TLinklist* tlist = NULL;
if (list == NULL) return;
tlist = (TLinklist*)list;
return tlist->length;
}

int LinkList_Insert(LinkList* list, LinkListNode* node, int pos)
{
if (list == NULL || node == NULL || pos < 0)
{
printf("LinkList_Insert()参数为空");
return -1;
}

TLinklist* tList = (TLinklist*)list;
LinkListNode* current = &(tList->header);//中间节点
for (int i = 0; i < pos &&(current->next!=NULL) ; i++)
{
current = current->next;
}
//让node链接后续链表  让前面的链表链接node节点
node->next = current->next;
current->next = node;
tList->length++; //链表长度+1
return 0;
}

LinkListNode* LinkList_Get(LinkList* list, int pos)
{
if (list == NULL || pos < 0)
{
printf("LinkList_Insert()参数为空");
return (LinkListNode*)-1;
}
TLinklist* tList = (TLinklist*)list;
LinkListNode* current = &(tList->header);//中间节点
for (int i = 0; i < pos && (current->next != NULL); i++)
{
current = current->next;
}
return current->next;
}

LinkListNode* LinkList_Delete(LinkList* list, int pos)
{
if (list == NULL || pos < 0)
{
printf("LinkList_Insert()参数为空");
return (LinkListNode*)-1;
}
TLinklist* tList = (TLinklist*)list;
LinkListNode* current = &(tList->header);//中间节点
for (int i = 0; i < pos && (current->next != NULL); i++)
{
current = current->next;
}
LinkListNode* ret = current->next;
current->next = ret->next;
tList->length--;
return ret;
}

<main>

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"linklist.h"

typedef struct Teacher
{
LinkListNode node;
int age;
char name[64];
}Teacher;


int main()
{
int len = 0;
Teacher t1, t2, t3, t4, t5;
t1.age = 31;
t2.age = 32;
t3.age = 33;
t4.age = 34;
t5.age = 35;

strcpy(t1.name, "1");
strcpy(t2.name, "2");
strcpy(t3.name, "3");
strcpy(t4.name, "4");
strcpy(t5.name, "5");

LinkList* list = NULL;
list = LinkList_Create();
if (list == NULL)
{
return;
}
len = LinkList_Length(list);

//链表的算法和具体业务的分离
LinkList_Insert(list, (LinkListNode*)&t1, 0);
LinkList_Insert(list, (LinkListNode*)&t2, 0);
LinkList_Insert(list, (LinkListNode*)&t3, 0);
LinkList_Insert(list, (LinkListNode*)&t4, 0);
LinkList_Insert(list, (LinkListNode*)&t5, 0);

//遍历
for (int i = 0; i < LinkList_Length(list); i++)
{
Teacher* tmp = (Teacher*)LinkList_Get(list, i);
if (tmp == NULL) return;
printf("tmo->age:%d tmp->name:%s\n", tmp->age, tmp->name);
}

//删除链表
while(LinkList_Length(list)>0)
{
Teacher* tmp = (Teacher*)LinkList_Delete(list, 0);
if (tmp == NULL) return;
}

LinkList_Destroy((list));

system("pause");
return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注