C言語-ユニバーサルリンクリスト

思考:たとえば、プロジェクトで複数の構造タイプのリンクリストを使用する必要がある場合、注文システムには2つの構造の人員とレシピがあります。これら2つのタイプのリンクリストを操作する場合は、2つのセットを作成する必要があります。リンクリストの作成、ノードの追加、ノードの削除、リンクリストノードの数の取得、リンクリストノードデータの取得、リンクリストの解放などの操作には時間がかかります。これら2種類のサービスでは、リンクリストの操作は同じですが、一般的なリンクリストの操作関数のセットを記述できますか?これら2つのアプリケーションは、対応する操作関数を呼び出すだけで済みますか?このような問題に直面して、普遍的なリンクリストがあります。

ユニバーサルリンクリストデザイン

一般的なリンクリストの構造:

struct list
{
    
    
	void *Data;  //存放数据的地址 
	struct list *next;
}

従業員情報構造(データは構造を指します)

struct staff
{
    
    
	int iStaffID;
	char acName[20];
	char acPasswd[10];
}

次に、ユニバーサルリンクリストのデータ構造図は次のように表すことができます(ヘッドノードはデータを格納しません)
ここに画像の説明を挿入

ユニバーサルリンクリストの基本操作

リンクリストを初期化する

void *List_Init(void *data)
{
    
    
	struct list * head;
	head = (struct list *)malloc(sizeof(struct list));
	head->data=data;
	head->next=NULL;
	return head;
}

リンクリストノードを追加します(リンクリストの最後に追加します)。

void List_Add(struct list *head,void *data)
{
    
    
	struct list *pNode,p1=head;
	pNode=(struct list *)malloc(sizeof(struct list ));
	while(p1->next != NULL )
	{
    
      
		p1=p1->next;  //遍历链表,找到最末尾的节点
	}  
	p1->next=pNode;
	pNode->data=data;
	pNode->next=NULL;
}

リンクリストノードの数を取得します。

int  LIST_Count(struct list * head)
{
    
    
	struct list * p1;
	int nCount = 0;
	p1=head->next;
	while(p1 != NULL)
	{
    
    
		nCount++;
		p1=p1->next;
	}
	return nCount;
}

リンクリストのノードを取得します(リンクリストノードのデータを返します)。

void *LIST_GetNode(struct list * head,int index)
{
    
    
	struct list * p1;
	int nCount = 0;
	p1=head;
	while(p1->next != NULL)
	{
    
    
		if(nCount == index)
		{
    
    
			return p1->data;
		}
		p1=p1->next;
		nCount++;
	}
	return NULL;
}

リンクリストのノードを削除します

int List_Del(struct list * head,int index)
{
    
    
	struct list * p1;
	struct list * p2;  //要删除结点的临时结点
	int nCount = 0;
	p1=head;
	while(p1->next != NULL)
	{
    
    
		if(nCount == index)
		{
    
    
			p2 = p1->next;
			p1->next = p1->next->next;
			delete p2;
		}
		p1=p1->next;
		nCount++;
	}
	return nCount;
}

リリースリスト

void *List_Free(struct list *head)
{
    
    
	struct list *ptr=head;
	while(ptr!=NULL)
	{
    
    
		ptr=ptr->next;
		free(head->data);//先释放数据存储的内存空间
		free(head);//再释放链表节点的内存空间
		head=ptr;
	}
	return head;
}

ユニバーサルチェーンテーブルの例

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

struct list
{
    
    
	void *Data;  //存放数据的地址 
	struct list *next;
};
struct staff
{
    
    
	int iStaffID;
	char acName[20];
	char acPasswd[10];
};

void Staff_Print(struct list *head)
{
    
    
	struct list *p1=head->next;
	struct staff *people1;
	while(p1 != NULL)
	{
    
     
		people1=(struct staff*)p1->Data;
		printf("%5d%10s%10s\n",people1->iStaffID,
			people1->acName,people1->acPasswd);
		p1=p1->next;
	}
}
void List_Add(struct list *head,void *data)
{
    
    
	struct list *pNode,*p1=head;
	pNode=(struct list *)malloc(sizeof(struct list ));
	while(p1->next != NULL )
	{
    
       p1=p1->next; }   //遍历链表,找到最末尾的节点

	p1->next=pNode;
	pNode->Data=data;
	pNode->next=NULL;
}
void *List_Init(void *data)
{
    
    
	struct list * head;
	head = (struct list *)malloc(sizeof(struct list));
	head->Data=data;
	head->next=NULL;
	return head;
}
void main()
{
    
    
	struct list *head;
	struct staff *people1,*people2;
	//初始化链表
	head=(struct list *)List_Init(NULL);//头节点不存储数据,参数为NULL

	people1=(struct staff *)malloc(sizeof(struct staff));
	people2=(struct staff *)malloc(sizeof(struct staff));
	people1->iStaffID=1001;
	strcpy(people1->acName,"张三");
	strcpy(people1->acPasswd,"123456");

	people2->iStaffID=1002;
	strcpy(people2->acName,"李四");
	strcpy(people2->acPasswd,"123456");
	//添加链表节点
	List_Add(head,people1);
	List_Add(head,people2);
	//员工信息打印函数
	Staff_Print(head);
}

演算結果:

 1001      张三    123456
 1002      李四    123456

おすすめ

転載: blog.csdn.net/qq_46485161/article/details/115276571