码迷,mamicode.com
首页 > 编程语言 > 详细

1、链表之增、删、查实现(C语言)

时间:2019-01-23 01:30:59      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:string   节点   new   ISE   HERE   info   地址   next   enter   

一、功能描述:

可以创建节点并添加到链表中、查看链表中的所有节点、并可以删除特定的节点

 

二、代码实现

1、主函数main主要实现的是从后台键入不同的字符,执行对应的函数来实现特定的操作代码如下:

int main(char argc, char *argv[])
{
    char c;
    while (1)
    {    
        c = getchar();                //键入一个字符
//        printf("c = %c\n",c);

        printf("Usage:\n");
        printf("<l> List all the notes\n");
        printf("<a> Add a node to chains\n");
        printf("<d> Delete a node from chains\n");
        printf("<q> quit\n");
        printf("Enter the choise: \n");
        switch (c)
        {
            case l :
            {
                list_all_notes();    //显示链表中所有的节点
                break;
            }
            case a :
            {
                add_a_node();    //链表中添加一个节点
                break;
            }            
            case d :
            {
                delete_a_node();    //删除链表中的一个节点
                break;
            }
            case q :                //退出死循环
            {
                return 0;
                break;
            }
            defaut :
            {        
                break;
            }            
        }
    }
    return 0;
}

 

2、add_a_node的主要作用创建一个节点,并初始化(节点分配内存、初始化节点name),最后调用add_note_to_chains函数把该节点添加到链表中,代码如下:

 1 void add_a_node()
 2 {
 3     char *str;                //用来存放节点名字
 4     char name[128];        //临时变量,键盘设置新节点的名字
 5     PT_Node ptNew;        //指针变量,指向新的节点
 6 
 7     printf("Please enter name of node to be added : ");
 8     scanf("%s", name);        //给新添加的节点取一个名字
 9 
10     str = malloc(strlen(name) + 1);    //为节点的名字分配空间
11     strcpy(str, name);
12 
13     ptNew = malloc(sizeof(T_Node));    //为新节点分配内存
14 
15     //初始化节点中的元素
16     ptNew ->name = str;            //节点中的name指向str
17     ptNew ->pre = NULL;            
18     ptNew ->next = NULL;
19 
20     add_note_to_chains(ptNew);    //把该节点添加到链表中
21 }

 

3、add_note_to_chains主要实现的是把节点挂载到链表中,代码如下:

static void add_note_to_chains(PT_Node ptNew)
{
    PT_Node ptCur;
    if (g_ptNodeHead == NULL)
    {
        g_ptNodeHead = ptNew;//如果链表中没有一个节点,则头结点指向该节点
    }
    else
    {
        ptCur = g_ptNodeHead;
        while(ptCur ->next)    //遍历找到当前节点的next元素为空
        {
            ptCur = ptCur ->next;    
        }
        
        ptCur->next = ptNew;    //把新的节点挂载到g_ptNodeHead链表中
        ptNew ->pre = ptCur;
        
    }
}

 

4、delete_a_node函数的主要功能是通过键入的name,索引到链表中对应的节点地址,最后调用delete_node函数删除(卸载)链表中对应的节点,代码如下:

static void delete_a_node()
{
    PT_Node ptFindName;
    char name[128];

    printf("Please enter the name of node to be deleted :");
    scanf("%s", name);

    ptFindName = get_name(name);    //通过名字获得对应节点地址
    if (ptFindName == NULL)
    {
        printf("Don‘t have this node\n");
        return;
    }
        
    delete_node(ptFindName);        //删除链表中对应的节点
}

 

5、get_name函数的作用是通过节点name在链表中索引到其对应的节点地址,代码如下:

PT_Node get_name(char *name)
{
    PT_Node ptCar;
    if (g_ptNodeHead == NULL)
    {
        return NULL;
    }
    else
    {
        ptCar = g_ptNodeHead;
        do{
            if (strcmp (ptCar->name, name) == 0)
            {
                    printf("find \‘%s\‘ from chain\n",name);
                    return ptCar;
            }
            else
            {
                ptCar = ptCar->next;
            }
        }while (ptCar);

        printf("don‘t find \‘%s\‘ from chain\n",name);
        return NULL;
    }
}

 

6、delete_node函数的主要作用是卸载链表中对应的节点,卸载细节如下图,其中ptCar为当前要被卸载的节点、ptPre为前一个节点、ptNext为后一个节点

技术分享图片

对应代码如下:

static void delete_node(PT_Node ptDel)
{
    PT_Node ptCar;
    PT_Node ptPre;
    PT_Node ptNext;
    if (g_ptNodeHead == ptDel)
    {
        g_ptNodeHead = ptDel->next;
//        return;
    }
    else{
        ptCar = g_ptNodeHead ->next;
//        printf("1111111111111\n");
        while(ptCar)
        {
            if (ptCar == ptDel)
            {
                //从链表中删除
                ptPre    = ptCar ->pre;
                ptNext  = ptCar ->next;
                ptPre->next = ptNext;
//                printf("22222222\n");
                if(ptNext)
                {
                    ptNext ->pre = ptPre;
                }
                break;
            }
            else
            {
                ptCar = ptCar->next;
//                printf("333333\n");
            }
        }        
    }
    free(ptDel->name);    //释放节点名字占用的内存
    free(ptDel);            //释放节点占用的内存
}

 

7、list_all_notes显示链表中的所用节点,代码如下:

static void list_all_notes()
{
    PT_Node ptCar;
    ptCar = g_ptNodeHead;
    int i = 0;
    if (g_ptNodeHead == NULL)
    {
        printf("\n");
        printf("There is no node in chain list!\n");
        printf("\n");
    }
    while(ptCar)
    {
        printf("%02d : %s\n", i++, ptCar ->name);
        ptCar = ptCar ->next;
    }
}

 

8、完整代码实现

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 //定义一个节点
  6 typedef struct T_NODE{
  7     char *name;
  8     struct T_NODE *pre;
  9     struct T_NODE *next;
 10 }T_Node, *PT_Node;
 11 
 12 static PT_Node g_ptNodeHead;    //链表头
 13 
 14 static void add_note_to_chains(PT_Node ptNew)
 15 {
 16     PT_Node ptCur;
 17     if (g_ptNodeHead == NULL)
 18     {
 19         g_ptNodeHead = ptNew;//如果链表中没有一个节点,则头结点指向该节点
 20     }
 21     else
 22     {
 23         ptCur = g_ptNodeHead;
 24         while(ptCur ->next)    //遍历找到当前节点的next元素为空
 25         {
 26             ptCur = ptCur ->next;    
 27         }
 28         
 29         ptCur->next = ptNew;    //把新的节点挂载到g_ptNodeHead链表中
 30         ptNew ->pre = ptCur;
 31         
 32     }
 33 }
 34 
 35 void add_a_node()
 36 {
 37     char *str;                //用来存放节点名字
 38     char name[128];        //临时变量,键盘设置新节点的名字
 39     PT_Node ptNew;        //指针变量,指向新的节点
 40 
 41     printf("Please enter name of node to be added : ");
 42     scanf("%s", name);        //给新添加的节点取一个名字
 43 
 44     str = malloc(strlen(name) + 1);    //为节点的名字分配空间
 45     strcpy(str, name);
 46 
 47     ptNew = malloc(sizeof(T_Node));    //为新节点分配内存
 48 
 49     //初始化节点中的元素
 50     ptNew ->name = str;            //节点中的name指向str
 51     ptNew ->pre = NULL;            
 52     ptNew ->next = NULL;
 53 
 54     add_note_to_chains(ptNew);    //把该节点添加到链表中
 55 }
 56 
 57 
 58 PT_Node get_name(char *name)
 59 {
 60     PT_Node ptCar;
 61     if (g_ptNodeHead == NULL)
 62     {
 63         return NULL;
 64     }
 65     else
 66     {
 67         ptCar = g_ptNodeHead;
 68         do{
 69             if (strcmp (ptCar->name, name) == 0)
 70             {
 71                     printf("find \‘%s\‘ from chain\n",name);
 72                     return ptCar;
 73             }
 74             else
 75             {
 76                 ptCar = ptCar->next;
 77             }
 78         }while (ptCar);
 79 
 80         printf("don‘t find \‘%s\‘ from chain\n",name);
 81         return NULL;
 82 
 83     }
 84 
 85 }
 86 
 87 static void delete_node(PT_Node ptDel)
 88 {
 89     PT_Node ptCar;
 90     PT_Node ptPre;
 91     PT_Node ptNext;
 92     if (g_ptNodeHead == ptDel)
 93     {
 94         g_ptNodeHead = ptDel->next;
 95 //        return;
 96     }
 97     else{
 98         ptCar = g_ptNodeHead ->next;
 99 //        printf("1111111111111\n");
100         while(ptCar)
101         {
102             if (ptCar == ptDel)
103             {
104                 //从链表中删除
105                 ptPre    = ptCar ->pre;
106                 ptNext  = ptCar ->next;
107                 ptPre->next = ptNext;
108 //                printf("22222222\n");
109                 if(ptNext)
110                 {
111                     ptNext ->pre = ptPre;
112                 }
113                 break;
114             }
115             else
116             {
117                 ptCar = ptCar->next;
118 //                printf("333333\n");
119             }
120         }        
121     }
122     free(ptDel->name);    //释放节点名字占用的内存
123     free(ptDel);            //释放节点占用的内存
124 }
125 
126 static void delete_a_node()
127 {
128     PT_Node ptFindName;
129     char name[128];
130 
131     printf("Please enter the name of node to be deleted :");
132     scanf("%s", name);
133 
134     ptFindName = get_name(name);    //通过名字获得对应节点地址
135     if (ptFindName == NULL)
136     {
137         printf("Don‘t have this node\n");
138         return;
139     }
140         
141     delete_node(ptFindName);        //删除链表中对应的节点
142 }
143 
144 static void list_all_notes()
145 {
146     PT_Node ptCar;
147     ptCar = g_ptNodeHead;
148     int i = 0;
149     if (g_ptNodeHead == NULL)
150     {
151         printf("\n");
152         printf("There is no node in chain list!\n");
153         printf("\n");
154     }
155     while(ptCar)
156     {
157         printf("%02d : %s\n", i++, ptCar ->name);
158         ptCar = ptCar ->next;
159     }
160 }
161 
162 int main(char argc, char *argv[])
163 {
164     char c;
165     while (1)
166     {    
167         c = getchar();                    //键入一个字符
168 //        printf("c = %c\n",c);
169 
170         printf("Usage:\n");
171         printf("<l> List all the notes\n");
172         printf("<a> Add a node to chains\n");
173         printf("<d> Delete a node from chains\n");
174         printf("<q> quit\n");
175         printf("Enter the choise: \n");
176         switch (c)
177         {
178             case l :
179             {
180                 list_all_notes();        //显示链表中所有的节点
181                 break;
182             }
183             case a :
184             {
185                 add_a_node();        //链表中添加一个节点
186                 break;
187             }            
188             case d :
189             {
190                 delete_a_node();        //删除链表中的一个节点
191                 break;
192             }
193             case q :                    //退出死循环
194             {
195                 return 0;
196                 break;
197             }
198             defaut :
199             {        
200 
201                 break;
202             }            
203         }
204     }
205     return 0;
206 }

 

1、链表之增、删、查实现(C语言)

标签:string   节点   new   ISE   HERE   info   地址   next   enter   

原文地址:https://www.cnblogs.com/sbtblogs/p/10306692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!