数据结构基础 - 链表的遍历
生活随笔
收集整理的這篇文章主要介紹了
数据结构基础 - 链表的遍历
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈表的遍歷跟數組的遍歷很相似,不過不同的是,數組可以通過索引值隨機訪問數據,而鏈表一定要通過遍歷的方式訪問其中的節點,如果要知道第n個節點的內容,就需要遍歷n-1個節點。
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}
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}
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}
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}
C#代碼顯得有些生硬,還請見諒。
總結
以上是生活随笔為你收集整理的数据结构基础 - 链表的遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用InstallShield9打包 vb
- 下一篇: [Domino]Java访问Domino