且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C语言尾插法链表

更新时间:2022-09-15 12:32:26

算法示意图:
C语言尾插法链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using namespace std;
 
struct DATA
{
    int id;
    char name[20];
};
 
struct info
{
    DATA data;
    info * pNext;
};
 
//头节点
info * g_Head = NULL;
 
void CreateList()
{
    //创建头节点
    info * pHead   = new info;
    if(pHead)
    {
        pHead->data.id  = NULL;
        memset(pHead->data.name,0,10);
 
        pHead->pNext = NULL;
         
        g_Head = pHead;
    }
}
 
void Add(int id,char * str)
{
     info * pData   = new info;
     pData->data.id = id;
     strcpy(pData->data.name,str);
     pData->pNext = NULL;
 
 
     info * p = g_Head,*p1;
     while(p)
     {
        p1 = p;
        p = p->pNext;
     }
 
     p1->pNext = pData;
 
}
 
void print()
{
     info * p = g_Head;
     while(p)
     {
         cout << p->data.id << " " << p->data.name << endl;
         p = p->pNext;
     }
}
 
int main(int argc, char* argv[])
{
    CreateList();
    Add(1,"李大");
    Add(2,"王五");
    Add(3,"罗博特");
 
    print();
 
    getchar();
    return 0;
}

C语言尾插法链表















本文转自Chinayu201451CTO博客,原文链接:http://blog.51cto.com/9233403/1968176 ,如需转载请自行联系原作者