栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言单链表学习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言单链表学习

  1. 单链表

    链表是一种常见的基础数据结构,链表的出现很大程度上弥补了数组的先天不足。链表是结构体变量和结构体变量连接在一起

    单链表和数组相比较,单链表插入指定位置的效率要比数组高很多。

    链表的一个结点拥有信息域(head)和指针域

  2. 单链表结点的声明
    #include 
    struct Book
    {
    	char title[128];
    	char author[40];
    	struct Book* next;
    };
    int main(void)
    {
    	return 0;
    }
  3. 动态申请一个链表(动态内存申请+模块化设计)
    (1)创建链表(创建一个表头表示整个链表)
    (2)创建结点
    (3)插入结点
    (4)删除结点
    (5)打印遍历链表(测试)
    创建一个简单的单链表:
    #include 
    #include 
    
    struct Node
    {
    	int data;			//数据域
    	struct Node* next;	//指针域
    };
    struct Node*creatlist()
    {
    	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    	headNode->next = NULL;
    	return headNode;
    };
    int main(void)
    {
    	struct Node* lis = creatlist();
    	return 0;
    }

    然后为链表创建结点

    struct Node*creatNode(int data)
    {
    	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    	newNode->data = data;
    	newNode->next = NULL;
    	return newNode;
    };
  4. 在单链表中插入元素(头插法)
    #include 
    #include 
    
    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", pMove->data);
    		pMove = pMove->next;
    	}
    	printf("n");
    }
    void insertNodeByHead(struct Node* headNode, int data)
    {
    	//创建插入的结点
    	struct Node* newNode = creatNode(data);
    	newNode->next = headNode->next;
    	headNode->next = newNode;
    }
    int main(void)
    {
    	struct Node* list = creatlist();
    	insertNodeByHead(list, 1);
    	insertNodeByHead(list, 2);
    	insertNodeByHead(list, 3);
    	printList(list);
    	return 0;
    }

  5. 链表的删除(指定位置删除)
    #include 
    #include 
    
    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("%dt", pMove->data);
    		pMove = pMove->next;
    	}
    	printf("n");
    }
    void insertNodeByHead(struct Node* headNode, int data)
    {
    	//创建插入的结点
    	struct Node* newNode = creatNode(data);
    	newNode->next = headNode->next;
    	headNode->next = newNode;
    }
    void deleteNodeByAppin(struct Node* headNode, int posData)
    {
    	struct Node* posNode = headNode->next;
    	struct Node* posNodeFront = headNode;
    	if (posNode==NULL)
    	{
    		printf("链表为空,无法删除n");
    	}
    	else
    	{
    		while (posNode->next != posNode)
    		{
    			posNodeFront = posNode;
    			posNode = posNodeFront->next;
    			if (posNode==NULL)
    			{
    				printf("没有相关信息,无法删除n");
    				return;
    			}
    		}
    		posNodeFront->next = posNode->next;
    		free(posNode);
    	}
    
    }
    int main(void)
    {
    	struct Node* list = creatlist();
    	insertNodeByHead(list, 1);
    	insertNodeByHead(list, 2);
    	insertNodeByHead(list, 3);
    	printList(list);
    	deleteNodeByAppin(list, 4);
    	printList(list);
    	return 0;
    }

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384756.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号