链表的遍历跟数组的遍历很相似,不过不同的是,数组可以通过索引值随机访问数据,而链表一定要通过遍历的方式访问其中的节点,如果要知道第n个节点的内容,就需要遍历n-1个节点。
C代码:
C#代码:
C#代码显得有些生硬,还请见谅。
C代码:
1#include<stdio.h>
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}
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#代码:
1using System;
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}
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#代码显得有些生硬,还请见谅。