VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 310|回复: 1

[交流] c学习之路的点滴记录——静态与动态链表

[复制链接]
13_avatar_middle
最佳答案
1 
在线会员 发表于 2021-8-24 20:48:26 | 显示全部楼层 |阅读模式
  1. #pragma warning(disable:4996)
  2. #pragma warning(disable:6011)
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <Windows.h>


  7. //静态链表  节点的声明

  8. struct LinkNode
  9. {
  10.         int num;//数据域
  11.         struct LinkNode* next;//指针域
  12. };

  13. void test01()
  14. {
  15.         struct LinkNode node1 = { 10,NULL };
  16.         struct LinkNode node2 = { 20,NULL };
  17.         struct LinkNode node3 = { 30,NULL };
  18.         struct LinkNode node4 = { 40,NULL };
  19.         struct LinkNode node5 = { 50,NULL };

  20.         //建立关系
  21.         node1.next = &node2;
  22.         node2.next = &node3;
  23.         node3.next = &node4;
  24.         node4.next = &node5;

  25.         //如何遍历这个链表  
  26.         //创建一个指针 指向第一个节点
  27.         struct LinkNode* pCurrent = &node1;
  28.         while (pCurrent != NULL)
  29.         {
  30.                 printf("%d\n", pCurrent->num);
  31.                 pCurrent = pCurrent->next;
  32.         }
  33. }

  34. //动态链表
  35. void test02()
  36. {
  37.         struct LinkNode * node1=malloc(sizeof(struct LinkNode));
  38.         struct LinkNode* node2 = malloc(sizeof(struct LinkNode));
  39.         struct LinkNode* node3 = malloc(sizeof(struct LinkNode));
  40.         struct LinkNode* node4 = malloc(sizeof(struct LinkNode));
  41.         struct LinkNode* node5 = malloc(sizeof(struct LinkNode));

  42.         node1->num = 10;
  43.         node2->num = 20;
  44.         node3->num = 30;
  45.         node4->num = 40;
  46.         node5->num = 50;

  47.         //建立关系
  48.         node1->next = node2;
  49.         node2->next = node3;
  50.         node3->next = node4;
  51.         node4->next = node5;
  52.         node5->next = NULL;

  53.         //遍历链表
  54.         struct LinkNode* pCurrent = node1;
  55.         while (pCurrent != NULL)
  56.         {
  57.                 printf("%d\n", pCurrent->num);
  58.                 pCurrent = pCurrent->next;
  59.         }
  60.         //释放节点
  61.         free(node1);
  62.         free(node2);
  63.         free(node3);
  64.         free(node4);
  65.         free(node5);
  66. }
  67. int main(void)
  68. {

  69.         //test01();
  70.         test02();
  71.         return EXIT_SUCCESS;
  72. }
复制代码

评分

参与人数 2驿站币 +2 热心值 +5 收起 理由
58_avatar_small thzzl + 2 + 2 赞一个!
75_avatar_small wl1383838438 + 3 赞一个!

查看全部评分





上一篇:c语言学习之路的点滴记录——文件的字符读和写基本操作
下一篇:献丑了,C和ASM
75_avatar_middle
最佳答案
21 
online_supermod 发表于 2021-8-26 11:21:34 | 显示全部楼层
闲得无聊      闲得无聊   闲得无聊


#pragma warning(disable:4996)-------------消除错误警告(高版本编译器存在得问题低版本无,具体版本未测试vc6是没有的)
-------------在编译器中可设置-----------
#pragma warning(disable:6011)----同上
#include <stdio.h>--------标准得输入输出库  --io
#include <stdlib.h>---------动态库
#include <string.h>---------字符处理  库
#include <Windows.h> -------window 库


//静态链表&#160;&#160;节点的声明

struct LinkNode
{
&#160; &#160; &#160; &#160; int num;//数据域
&#160; &#160; &#160; &#160; struct LinkNode* next;//指针域
};

void test01()
{
&#160; &#160; &#160; &#160; struct LinkNode node1 = { 10,NULL };
&#160; &#160; &#160; &#160; struct LinkNode node2 = { 20,NULL };
&#160; &#160; &#160; &#160; struct LinkNode node3 = { 30,NULL };
&#160; &#160; &#160; &#160; struct LinkNode node4 = { 40,NULL };
&#160; &#160; &#160; &#160; struct LinkNode node5 = { 50,NULL };

&#160; &#160; &#160; &#160; //建立关系
&#160; &#160; &#160; &#160; node1.next = &node2;
&#160; &#160; &#160; &#160; node2.next = &node3;
&#160; &#160; &#160; &#160; node3.next = &node4;
&#160; &#160; &#160; &#160; node4.next = &node5;

&#160; &#160; &#160; &#160; //如何遍历这个链表&#160;&#160;
&#160; &#160; &#160; &#160; //创建一个指针 指向第一个节点
&#160; &#160; &#160; &#160; struct LinkNode* pCurrent = &node1;
&#160; &#160; &#160; &#160; while (pCurrent != NULL)
&#160; &#160; &#160; &#160; {
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; printf("%d\n", pCurrent->num);
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; pCurrent = pCurrent->next;
&#160; &#160; &#160; &#160; }
}

//动态链表
void test02()
{
&#160; &#160; &#160; &#160; struct LinkNode * node1=malloc(sizeof(struct LinkNode));
&#160; &#160; &#160; &#160; struct LinkNode* node2 = malloc(sizeof(struct LinkNode));
&#160; &#160; &#160; &#160; struct LinkNode* node3 = malloc(sizeof(struct LinkNode));
&#160; &#160; &#160; &#160; struct LinkNode* node4 = malloc(sizeof(struct LinkNode));
&#160; &#160; &#160; &#160; struct LinkNode* node5 = malloc(sizeof(struct LinkNode));

&#160; &#160; &#160; &#160; node1->num = 10;
&#160; &#160; &#160; &#160; node2->num = 20;
&#160; &#160; &#160; &#160; node3->num = 30;
&#160; &#160; &#160; &#160; node4->num = 40;
&#160; &#160; &#160; &#160; node5->num = 50;

&#160; &#160; &#160; &#160; //建立关系
&#160; &#160; &#160; &#160; node1->next = node2;
&#160; &#160; &#160; &#160; node2->next = node3;
&#160; &#160; &#160; &#160; node3->next = node4;
&#160; &#160; &#160; &#160; node4->next = node5;
&#160; &#160; &#160; &#160; node5->next = NULL;

&#160; &#160; &#160; &#160; //遍历链表
&#160; &#160; &#160; &#160; struct LinkNode* pCurrent = node1;
&#160; &#160; &#160; &#160; while (pCurrent != NULL)
&#160; &#160; &#160; &#160; {
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; printf("%d\n", pCurrent->num);
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; pCurrent = pCurrent->next;
&#160; &#160; &#160; &#160; }
&#160; &#160; &#160; &#160; //释放节点
&#160; &#160; &#160; &#160; free(node1);
&#160; &#160; &#160; &#160; free(node2);
&#160; &#160; &#160; &#160; free(node3);
&#160; &#160; &#160; &#160; free(node4);
&#160; &#160; &#160; &#160; free(node5);
}
int main(void)//---------主函数,(其实并非程序最开始执行的地方,按顺序的话应该是第二个执行得在main执行还有其他的函数)
{

&#160; &#160; &#160; &#160; //test01();
&#160; &#160; &#160; &#160; test02();
&#160; &#160; &#160; &#160; return EXIT_SUCCESS;
}
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2021-9-27 05:58

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表