c/c++ 链表功能的实现 [和弦]

今天关注的主题是链表的相关操作,这里使用链表(单向)主要解决动态存储数据体列表的问题。
直接贴代码先:

se.h


#ifndef LIBTEST_SE_H
#define LIBTEST_SE_H
#include 
<stdio.h>

class seTest{
public:
    
struct Elist{
        
int eid;
        Elist 
*next;
    };
};

#endif;

 

main.cpp

  1/*
  2功能描述: c/c++ 链表的实现测试
  3创建时间: 2009-04-16
  4
  5*/

  6
  7#include "stdafx.h"
  8#include "iostream" 
  9#include "se.h"
 10using namespace std;
 11
 12void InsertEndList(seTest::Elist *head, seTest::Elist *elist);
 13seTest::Elist* InsertSortList(seTest::Elist *head, seTest::Elist *elist);
 14seTest::Elist* DeleteFromList(seTest::Elist *head, int id);
 15void Print(seTest::Elist *head);
 16
 17void main()
 18{    
 19    seTest::Elist *elist;
 20    elist=(seTest::Elist*)malloc(sizeof(seTest::Elist));  
 21    elist->eid=2;
 22    elist->next =NULL;
 23
 24    seTest::Elist *oneptr_1;
 25    oneptr_1=(seTest::Elist*)malloc(sizeof(seTest::Elist));  
 26    oneptr_1->eid=1;
 27    oneptr_1->next=NULL;
 28    //InsertEndList(elist, oneptr_1);                //将节点插到链表尾部
 29    elist = InsertSortList(elist, oneptr_1);        //按顺序插入节点
 30
 31    seTest::Elist *oneptr_2;
 32    oneptr_2=(seTest::Elist*)malloc(sizeof(seTest::Elist));  
 33    oneptr_2->eid=3;
 34    oneptr_2->next=NULL;
 35    //InsertEndList(elist, oneptr_2);                //将节点插到链表尾部
 36    elist = InsertSortList(elist, oneptr_2);        //按顺序插入节点
 37    
 38    //通过ID删除链表中元素
 39    elist = DeleteFromList(elist,2);
 40
 41    //输出链表数据
 42    Print(elist);
 43    cin.get();
 44}

 45
 46//将数据插入链表中(直接插入到链表尾部)
 47void InsertEndList(seTest::Elist *head, seTest::Elist *elist)
 48{
 49    seTest::Elist *p;
 50    if(head==NULL)
 51    {
 52        head = elist;
 53    }

 54    else
 55    {
 56        p = head;
 57        while(true)
 58        {
 59            p->eid;
 60            if(p->next==NULL)
 61            {
 62                p->next = elist;
 63                p = p->next;
 64                break;
 65            }

 66            else
 67            {
 68                p = p->next;
 69            }

 70        }

 71    }

 72}

 73
 74//将数据插入链表中(排序链表)
 75seTest::Elist* InsertSortList(seTest::Elist *head, seTest::Elist *elist)
 76{
 77    seTest::Elist *p,*fp; //*p:当前节点指针  *fp:上一个节点指针
 78    if(head==NULL)
 79    {
 80        head = elist;
 81    }

 82    else
 83    {
 84        //待插入节点值比头节点值小,直接插入到链表前面
 85        if(elist->eid < head->eid)
 86        {
 87            elist->next = head;
 88            head = elist;
 89        }

 90        else
 91        {
 92            fp = head;
 93            p = head->next;
 94            while(true)
 95            {
 96                if(p==NULL)
 97                {
 98                    fp->next = elist;
 99                    break;
100                }

101                else
102                {
103                    //如果链表当前节点的值大于或等于要插入节点的值
104                    if(p->eid>=elist->eid)
105                    {
106                        fp->next = elist;
107                        elist->next = p;
108                        break;
109                    }

110                    fp = p;
111                    p = p->next;
112                }

113            }

114        }

115    }

116    return head;
117}

118
119//根据ID值在链表中删除对应的节点
120seTest::Elist* DeleteFromList(seTest::Elist *head, int id)
121{
122    seTest::Elist *p,*fp; //*p:当前节点指针  *fp:上一个节点指针
123    if(head==NULL)
124    {
125        cout<<"链表为空"<<endl;
126    }

127    else
128    {
129        fp = head;
130        p = head->next;
131        //如果ID值等于头节点值,则删除头结点
132        if(head->eid == id)
133        {
134            head = head->next;
135            //释放内存
136            free(fp);
137        }

138        else
139        {
140            while(true)
141            {
142                if(p!=NULL && p->eid==id)
143                {
144                    fp->next = p->next;
145                    free(p);
146                    break;
147                }

148                else
149                {
150                    if(p==NULL)
151                    {
152                        cout<<"对不起,链表没有该元素"<<endl;
153                        break;
154                    }

155                    fp = p;
156                    p = p->next;
157                }

158            }

159        }

160    }

161    return head;
162}

163
164//输出链表数据
165void Print(seTest::Elist *head)
166{
167    seTest::Elist *p;
168    p = head;
169    while(p!=NULL)
170    {
171        cout<<p->eid<<endl;
172        if(p->next!=NULL)
173        {
174            p = p->next;
175        }

176        else
177            break;
178    }

179}

 

以上代码主要用来学习链表测试,实际使用中我们可能还是需要写的更细致点,过滤更严格点。

 

posted on 2009-04-16 16:26  优柔的和弦  阅读(919)  评论(0编辑  收藏  举报

导航