数据结构基础 - 链表的遍历
C代码:
2
3struct llist
4{
5 int num;
6 char name[10];
7 struct llist *next;
8};
9typedef struct llist node;
10typedef node *llink;
11
12
13/**//*链表的创建*/
14llink createllist()
15{
16 llink head;
17 llink ptr,ptr1;
18 int i;
19
20 head = (llink)malloc(sizeof(node)); //分配第一个节点
21 if(!head)
22 return NULL;
23 printf("请输入六项邮寄数据:\n");
24 printf("请输入编号 ==> ");
25 scanf("%d",&head->num);
26 printf("请输入编号(%d)的姓名 ==> ",head->num);
27 scanf("%s",head->name);
28 head->next = NULL;
29 ptr = head;
30 for(i = 1;i < 6; i++)
31 {
32 ptr1 = (llink)malloc(sizeof(node));
33 if(!ptr1)
34 return NULL;
35 printf("请输入编号 ==> ");
36 scanf("%d",&ptr1->num);
37 printf("请输入编号(%d)的姓名 ==> ",ptr1->num);
38 scanf("%s",ptr1->name);
39 ptr1->next = NULL;
40 ptr -> next = ptr1;
41 ptr = ptr ->next;
42 }
43 return head;
44}
45
46/**//*链表的节点遍历*/
47llink findnode(llink head,int num)
48{
49 llink ptr;
50
51 ptr = head;
52 while(ptr != NULL)
53 {
54 if(ptr->num == num)
55 return ptr;
56 ptr = ptr->next;
57 }
58 return ptr;
59}
60
61
62void main()
63{
64 llink head;
65 llink ptr;
66 int num;
67
68 head = createllist();
69 if(!head)
70 {
71 printf("内存分配失败!\n");
72 exit(1);
73 }
74 while(1)
75 {
76 printf("请输入要寻找的邮寄编号 ==> ");
77 scanf("%d",&num);
78 if(num != 0)
79 {
80 ptr = findnode(head,num);
81 if(!ptr)
82 printf("没有找到\n");
83 else
84 printf("姓名:%s\n",ptr->name);
85 }
86 else
87 exit(1);
88 }
89}
C#代码:
2
3class Node
4{
5 private int num;
6
7 public int Num
8 {
9 set{num = value;}
10 get{return num;}
11 }
12
13 private string name;
14
15 public string Name
16 {
17 set{name = value;}
18 get{return name;}
19 }
20
21 private Node next;
22
23 public Node Next
24 {
25 set{next = value;}
26 get{return next;}
27 }
28}
29
30class Llist
31{
32 //链表的创建
33 public static Node CreateLlist()
34 {
35 Node head;
36 Node tempNode,tempNode1;
37 int i;
38
39 head = new Node();
40 Console.WriteLine("请输入六项邮寄数据:\n");
41 Console.WriteLine("请输入编号==>");
42 head.Num = Convert.ToInt32(Console.ReadLine());
43 Console.WriteLine("请输入编号({0})的姓名 ==>",head.Num);
44 head.Name = Console.ReadLine();
45 head.Next = null;
46 tempNode = head;
47 for( i = 1 ;i < 6; i++)
48 {
49 tempNode1 = new Node();
50 Console.WriteLine("请输入编号 ==>");
51 tempNode1.Num = Convert.ToInt32(Console.ReadLine());
52 Console.WriteLine("请输入编号({0})的姓名",tempNode1.Num);
53 tempNode1.Name = Console.ReadLine();
54 tempNode1.Next = null;
55 tempNode.Next = tempNode1;
56 tempNode = tempNode.Next;
57 }
58 return head;
59 }
60
61 //链表的节点遍历
62 public static Node FindNode(Node head,int num)
63 {
64 Node tempNode;
65
66 tempNode = head;
67 while(tempNode != null)
68 {
69 if(tempNode.Num == num)
70 return tempNode;
71 tempNode = tempNode.Next;
72 }
73 return tempNode;
74 }
75}
76
77class Test
78{
79 public static void Main()
80 {
81 Node head;
82 Node tempNode;
83 int num;
84
85 head = Llist.CreateLlist();
86 while(true)
87 {
88 Console.WriteLine("请输入要寻找的邮寄编号 ==>");
89 num = Convert.ToInt32(Console.ReadLine());
90 if(num != 0)
91 {
92 tempNode = Llist.FindNode(head,num);
93 if(tempNode == null)
94 Console.WriteLine("没有找到\n");
95 else
96 Console.WriteLine("姓名:{0}\n",tempNode.Name);
97 }
98 else
99 break;
100 }
101 }
102}
C#代码显得有些生硬,还请见谅。
数据结构基础 - 链表的遍历相关推荐
- 数据结构基础——链表
1.链表的特点 (1)申请的空间可以不连续 (2)链表访问不方便 (3)插入.删除不需要移动元素,比较方便 头结点:数据域无效 首结点:第一个数据域有效的结点 尾结点:指针域无效 空链表:既是头结点又 ...
- 【算法学习笔记】07.数据结构基础 链表 初步练习
写这篇文章时离学完列表已经很久了 最近真的是很懒. 数据结构这部分在刘汝佳的白书上的部分太少了,我一点要找个时间好好的看一下程杰的大话. 链表的优点是它强调的是相关元素之间的相对顺序,左和右.其实可以 ...
- 数据结构基础 后序遍历和中序遍历还原二叉树
[问题描写叙述] 二叉树 A / / B C / / / / D E F G ...
- python数据结构基础(单链表,多链表,二叉树)
python数据结构基础(单链表,多链表,二叉树) 数据结构指数据对象中数据元素之间的关系 Python 给我们提供了很多现成的数据结构类型,这些系统自己定义好的,不需要我们自己去定义的数据结构叫做 ...
- 【数据结构基础】图的遍历方法与应用
前言 数据结构,一门数据处理的艺术,精巧的结构在一个又一个算法下发挥着他们无与伦比的高效和精密之美,在为信息技术打下坚实地基的同时,也令无数开发者和探索者为之着迷. 也因如此,它作为博主大二上学期最重 ...
- 求单链表的最大值与原地逆转_数据结构基础复习09.ppt
数据结构基础复习09.ppt 数据结构考研辅导 基础复习 浙江大学计算机学院 内容提纲 考研概述 考察目标理解数据结构的基本概念 掌握数据结构的逻辑结构 存储结构及其差异 以及各种基本操作的实现 在掌 ...
- 算法与数据结构基础四----数据结构基础之动态数据结构基础:链表上
接着上一次https://www.cnblogs.com/webor2006/p/15195969.html的数据结构继续往下学习,这次会进入一个非常重要的数据结构的学习----链表,这个是未来学习复 ...
- 【数据结构基础笔记】【链表】
代码参考<妙趣横生的算法.C语言实现> 文章目录 前言 1.链表基础 2.创建一个链表 3.插入结点 4.删除结点 5.销毁链表 6.实例分析 前言 本章总结:链表的定义.创建.销毁,结点 ...
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
链表简介 数组的缺点: 1.元素插入:除了在数组的末尾插入元素之外,在数组的其他任何位置插入元素都需要进行数组元素的频繁移动(插入位置之后的元素都需往后移动), 时间复杂度约为O(N); 2.数组的删 ...
最新文章
- python mongodb orm_Django 通过 mongoengine 连接 MongoDB 进而使用orm进行CRUD
- linux 多线程聚集写程序,Linux篇二:Makefile写多线程多文件程序-Go语言中文社区...
- Redis中的Cluster总结
- Windows 8.1下安装Mac OS X 10.8虚拟机
- c++清空串口缓冲区
- java请求百度短链接_长链接生成短链接Java源码(调用百度接口)
- Oracle rac误删归档,rac 归档的存放路径下的不同的恢复方法
- 这几天我的生活就是这样的
- Redis分布式锁之:RedLock
- 北航计算机组成原理课程设计-2020秋 PreProject-Logisim-入门指南与Logisim门电路
- java生成KML文件
- 测试驱动开发、验收测试驱动开发和行为驱动开发
- 全球与中国组合锁市场深度研究分析报告
- 每天5分钟玩转Kubernetes | Liveness探测
- 18. --plic--=--ply--=--pli--=--ple--=--plex--=--plo-- to fold 倍,重,折叠 (词19、20)
- 网络层HTPPS和HTTP的概念与区别
- 2019年了时间过得好快啊!我的小感慨
- python的实验报告大一心理_Python程序设计实验报告: 实验六
- WordPress教程:如何隐藏并替换WordPress管理路径?
- XShell6 循环执行脚本
热门文章
- 存储分类分析:DAS、NAS、SAN[转]
- jQuery弹出层插件大全
- c++ builder 中的 XMLDocument 类详解(2) - 记要
- 中信国健临床通讯 2011年3月期 目 录
- 你犯了没?电脑最忌讳的18个小动作之三
- SQL注入绕过登录验证
- Could not find a suitable table factory for ‘org.apache.flink.table.factories.CatalogFactory‘
- OpenGL:使用FBO为渲染对象并从GPU取出存图
- html标签书写注意事项,HTML一些标签注意事项
- python解释器下安装z3_再次:在Windows上安装Z3 Python