首页 > 编程语言 > 详细

C++操作链表

时间:2015-03-27 19:32:05      阅读:154      评论:0      收藏:0      [点我收藏+]
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 struct Node{
 6     int data;
 7     Node *next;
 8 };
 9 
10 Node* InitNodeList(){
11     Node *node=0;//定义一个空指针
12     return node;
13 }
14 
15 Node* addNode(Node *nodeList,Node *node)
16 {
17     if(nodeList == 0)
18     {
19         nodeList=node;
20     }else{
21         nodeList->next = node;
22     }
23     node->next=0;
24     
25     return nodeList;
26 }
27 
28 void printNodeList(Node *nodeList)
29 {
30     Node *p,*q;
31     q=p=nodeList;
32     while(q->next!=0)
33     {
34         cout << p->data << " ";
35         q=p;
36         p=p->next;
37     }
38     cout << endl;
39 }
40 
41 int main(void){
42     Node *node=InitNodeList();
43 
44     Node *a=new Node;
45     a->data = 1;
46     Node *b = new Node;
47     b->data = 2;
48     
49     node = addNode(node,a);
50     node = addNode(node,b);
51     
52     printNodeList(node);
53     return 0;
54 }

 

C++操作链表

原文:http://www.cnblogs.com/xxdfly/p/4372603.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!