C言語ベースの一重連結リストを実現(2つの完全な詳細コード+シミュレーション結果付き)

C 言語の助けを借りて、データ構造内の単一リンク リストをモジュラー方式で実現します (2 つの完全な詳細コードを使用)。

目次

序文

1. リンクリストとは?

2.単方向リスト

1.コード

2. シミュレーション

 2. 単一リンクリストの簡単な適用 --- 学生管理システム

1.コード

2. シミュレーション

要約する


序文

        リンクされたリストは、同じデータ型のコレクションであり、連続したメモリ スペースを占有しません。不連続な分類保存スペースに適していますが、大量の連続保存スペースが必要です (アーカイブに似ています). 欠点は、検索速度が遅く、費やされる時間が固定されていないことです. この記事では、単一のリンク リストを使用して、リンク リスト ヘッダーの作成、ノードの作成、指定された場所でのノードの削除、ノードのトラバースと印刷、ヘッド挿入によるノードの挿入、テール挿入によるノードの挿入など、リンク リストの使用法を説明します。 .


ヒント: 以下はこの記事の本文です. 記事を書くのは簡単ではありません. 参考になれば幸いです. 転載の際はリンクを添付してください.

1. リンクリストとは?

         単刀直入に言えば、連結リストは構造体の集合であり、同じデータ型の集合であり、連続したメモリ空間を占有せず、動的メモリ適用により構造体ポインタを構造体変数に変更し、構造体変数を使用します。リンクされたリストを操作します。不連続な分類保存スペースに適していますが、大量の連続保存スペースが必要です (アーカイブに似ています). 欠点は、検索速度が遅く、費やされる時間が固定されていないことです.

2.単方向リスト

1.コード

コードは以下のように表示されます:

#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int data;
    struct Node *next;
};
//创建链表表头 
struct Node *creatlist()//结构指针经动态内存申请变成结构体变量 
{
    struct Node *headNode=(struct Node*)malloc(sizeof(struct Node));
    headNode->next=NULL;
    return headNode;
};
//创建节点,要插入节点得先有节点 
struct Node *creatNode(int data)
{
    struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data=data;
    newNode->next=NULL;
    return newNode;
};
//打印,除去表头,从第二个节点开始遍历打印 
void printList(struct Node *headNode)
{
    struct Node *Pmove=headNode->next;
    while(Pmove)
    {
        printf("%d\t",Pmove->data);
        Pmove=Pmove->next;
    }
    printf("\n");
}
//插入节点 头插法 
void insert_Node_By_HeadNode(struct Node *headNode,int data)
{
    struct Node *newNode=creatNode(data);//首先创建要插入的节点 
    newNode->next=headNode->next;//创建完后插入 
    headNode->next=newNode;
}
//尾插法
void insertNodebyRear(struct Node *headNode, int data)
{
	struct Node *newNode=creatNode(data);
	while(headNode->next)
	{
		headNode = headNode->next;
	}
	headNode->next = newNode;
	newNode->next = NULL;
}
//指定位置删除
void deleteNoteByAppoin(struct Node *headNode,int posData)
{
	struct Node *posNode=headNode->next;
	struct Node *posNodeFront=headNode;
	if(posNode==NULL)
	{
		printf("无法删除链表为空\n");
	} 
	else
	{
		while(posNode->data!=posData)
		{
			posNodeFront=posNode;
			posNode=posNodeFront->next;
			if(posNode==NULL)
			{
				printf("没有找到相关信息无法删除\n");
				return;
			}
		}
		posNodeFront->next=posNode->next;
		free(posNode);
	}
 } 
int main()
{
    struct Node *list=creatlist();
    insert_Node_By_HeadNode(list,1);
    insert_Node_By_HeadNode(list,2);
    insert_Node_By_HeadNode(list,3);
    printList(list);
    deleteNoteByAppoin(list,1);
    printList(list);
    insertNodebyRear(list,4);
    insertNodebyRear(list,5);
    printList(list);
    system("pause");
    return 0;
}

2. シミュレーション

シミュレーション結果

 2. 単一リンクリストの簡単な適用 --- 学生管理システム

1.コード

コードは以下のように表示されます:

#include <stdio.h>
#include <stdlib.h>
struct student
{
	char name[20];//姓名
	int num;//学号
	int math;//成绩
 }; 
struct Node
{
    struct student date;
    struct Node *next;
};
//创建链表表头 
struct Node *creatlist()//结构指针经动态内存申请变成结构体变量 
{
    struct Node *headNode=(struct Node*)malloc(sizeof(struct Node));
    headNode->next=NULL;
    return headNode;
};
//创建节点,要插入节点得先有节点 
struct Node *creatNode(struct student date)
{
    struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->date=date;
    newNode->next=NULL;
    return newNode;
};
//打印,除去表头,从第二个节点开始遍历打印 
void printList(struct Node *headNode)
{
	printf("name\tnum\tmath\n");
    struct Node *Pmove=headNode->next;
    while(Pmove)
    {
        printf("%s\t%d\t%d\n",Pmove->date.name,Pmove->date.num,Pmove->date.math);
        Pmove=Pmove->next;
    }
    printf("\n");
}
//插入节点 头插法 
void insert_Node_By_HeadNode(struct Node *headNode,struct student date)
{
    struct Node *newNode=creatNode(date);//首先创建要插入的节点 
    newNode->next=headNode->next;//创建完后插入 
    headNode->next=newNode;
}
//尾插法
void insertNodebyRear(struct Node *headNode, struct student data)
{
	struct Node *newNode=creatNode(data);
	while(headNode->next)
	{
		headNode = headNode->next;
	}
	headNode->next = newNode;
	newNode->next = NULL;
}
//指定位置删除,通过学号删除 
void deleteNoteByAppoinNum(struct Node *headNode,int num)
{
	struct Node *posNode=headNode->next;
	struct Node *posNodeFront=headNode;
	if(posNode==NULL)
	{
		printf("无法删除链表为空\n");
	} 
	else
	{
		while(posNode->date.num!=num)
		{
			posNodeFront=posNode;
			posNode=posNodeFront->next;
			if(posNode==NULL)
			{
				printf("没有找到相关信息无法删除\n");
				return;
			}
		}
		posNodeFront->next=posNode->next;
		free(posNode);
	}
 } 
int main()
{
    struct Node *list=creatlist();
	struct student info;
	while(1)
	{
		printf("学生的姓名 学号 数学成绩:");
		setbuf(stdin,NULL);//清除缓存 
		scanf("%s%d%d",info.name,&info.num,&info.math);
		insertNodebyRear(list,info);
		printf("continue(1/0)?\n");
		setbuf(stdin,NULL);
		int choice=0;
		scanf("%d",&choice);
		if(choice==0)
		{
			break;
		}
	}
    printList(list);
    printf("要删除学生的学号:");
    int a;
    setbuf(stdin,NULL);
    scanf("%d",&a);
    deleteNoteByAppoinNum(list,a);
    printList(list);
    system("pause");
    return 0;
}

2. シミュレーション

シミュレーション結果

 参考リンク:

単連リスト、C言語データ構造を学ぶ1時間 topic_哔哩哔哩_bilibili


要約する

        以上が本日の言いたいことですが、この記事ではシングルリンクリストの使い方を紹介します。

おすすめ

転載: blog.csdn.net/m0_66360845/article/details/129209474