+ -
当前位置:首页 → 问答吧 → 求一个用C++写的单链表类的代码 能实现删除 插入 查找等操作

求一个用C++写的单链表类的代码 能实现删除 插入 查找等操作

时间:2011-12-05

来源:互联网

RT

作者: yw52109   发布时间: 2011-12-05

lz看看:
#include <iostream>
#define SIZE 5
using namespace std;

typedef int ElemType,Status;

typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;

void CreateList_L(LinkList &L,int n); //创建链表函数。
void display(LinkList L); //输出函数。
Status ListInsert_L(LinkList &L,int i,ElemType e); //插入函数。
Status ListDelete_L(LinkList &L,int i,ElemType &e); //删除函数。

void main()
{
int e=3,i=4;
  LinkList L;
  CreateList_L(L,SIZE);
  display(L);
  ListInsert_L(L,i,e);
  display(L);
  ListDelete_L(L,i,e);
  display(L);
}

void CreateList_L(LinkList &L,int n)
{
LinkList p;
L=(LinkList)malloc(sizeof(LNode));
L->next =NULL;
cout<<"Input five elements:";
for(int i=n;i>0;--i) //逆位序创建代表头节点的单链线性表L。
{
p=(LinkList)malloc(sizeof(LNode));
cin>>p->data;
p->next=L->next;
L->next=p;
}
}

void display(LinkList L)
{
if(!L) exit(0);
LinkList p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

Status ListInsert_L(LinkList &L,int i,ElemType e)
{
LinkList p=L,s;
int j=0;
while(p && j<i-1)
{
p=p->next;
++j;
}
if(!p||j>i-1) 
return 0;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return 0;
}

Status ListDelete_L(LinkList &L,int i,ElemType &e) //与插入函数不同,删除的元素参数要用引用或指针。
{
  LinkList p=L,q;
int j=0;
while(p->next && j<i-1)
{
p=p->next ;
++j;
}
if(!(p->next)||j>i-1)  
return 0;
q=p->next;
p->next=q->next;
e=q->data;
free(q);
return 0;
}

作者: y1509713911   发布时间: 2011-12-05

C/C++ code
using namespace std;

template<class Elem> class Link
{
    public:
        Elem element;
        Link* next;
        Link(const Elem &elemval, Link* nextval = NULL)
            { element = elemval; next = nextval; }
        Link(Link* nextval = NULL) { next = nextval; }
};

template<class Elem> class LList
{
    public:
        LList(int size) { init(); }
        ~LList() { removeall(); }
        void rever();
        void clear() { removeall(); init(); }
        bool insert(const Elem &);
        bool append(const Elem &);
        bool remove(Elem &);
        void setStart()
            { fence = head; rightcnt += leftcnt; leftcnt = 0; }
        void setEnd()
            { fence = tail; leftcnt += rightcnt; rightcnt = 0; }
        void prev();
        void next();
        int leftlength() const { return leftcnt; }
        int rightlength() const { return rightcnt; }
        bool setPos(int);
        bool getValue(Elem &) const;
        bool find(Elem &);
        void print() const;
    private:
        Link<Elem>* head;
        Link<Elem>* tail;
        Link<Elem>* fence;
        int leftcnt;
        int rightcnt;
        void init()
        {
            fence = tail = head = new Link<Elem>;
            leftcnt = rightcnt = 0;
        }
        void removeall()
        {
            while (head != NULL)
            {
                fence = head;
                head = head->next;
                delete fence;
            }
        }
};

template<class Elem>
void LList<Elem> :: rever()
{
    if ((leftcnt + rightcnt) == 0)
        return;
    Link<Elem> *newTail, *newTailNext, *headNext;
    newTail = head->next;
    while (newTail->next != NULL)
    {
        newTailNext = newTail->next;
        newTail->next = newTailNext->next;
        headNext = head->next;
        head->next = newTailNext;
        newTailNext->next = headNext;
    }
    tail = newTail;
}

template<class Elem>
bool LList<Elem> :: insert(const Elem &item)
{
    fence->next = new Link<Elem>(item, fence->next);
    if (tail == fence) 
        tail = fence->next;
    rightcnt++;
    return true;
}

template<class Elem>
bool LList<Elem> :: append(const Elem &item)
{
    tail = tail->next = new Link<Elem>(item, NULL);
    rightcnt++;
    return true;
}

template<class Elem>
bool LList<Elem> :: remove(Elem &it)
{
    if (fence->next == NULL) 
        return false;
    it = fence->next->element;
    Link<Elem>* ltemp = fence->next;
    fence->next = ltemp->next;
    if (tail == ltemp) 
        tail = fence;
    delete ltemp;
    rightcnt--;
    return true;
}

template<class Elem>
void LList<Elem> :: next()
{
    if (fence != tail)
        { fence = fence->next; rightcnt--; leftcnt++; }
}

template<class Elem>
void LList<Elem> :: prev()
{
    Link<Elem>* temp = head;
    if (fence = temp) 
        return;
    while (temp->next != fence) 
        temp = temp->next;
    fence = temp;
    leftcnt++;
    rightcnt++;
}

template<class Elem>
bool LList<Elem> :: setPos(int pos)
{
    if ((pos < 0) || (pos > leftcnt + rightcnt)) 
        return false;
    fence = head;
    for (int i = 0; i < pos; i++) 
        fence = fence->next;
    return true;
}

template<class Elem>
bool LList<Elem> :: getValue(Elem &it) const
{
    if (rightcnt == 0) 
        return false;
    it = fence->next->element;
    return true;
}

template<class Elem>
bool LList<Elem> :: find(Elem &it)
{
    Elem item;
    for (setStart(); getValue(item); next())
        if (it == item) return true;
    return false;
}

template<class Elem>
void LList<Elem> :: print() const
{
    Link<Elem>* temp = head;
    cout << "当前线性表为:< ";
    while (temp != fence)
    {
        cout << temp->next->element << " ";
        temp = temp->next;
    }
    cout << "| ";
    while (temp != tail)
    {
        cout << temp->next->element << " ";
        temp = temp->next;
    }
    cout << ">       '|'为栅栏标记\n";
}


一些你没说的函数我懒得删了 VS2010可以编译通过

作者: Johnkey_Chen   发布时间: 2011-12-05

http://download.csdn.net/detail/beefliu/2340886

作者: beefliu   发布时间: 2011-12-05