list的基本操做實現

 

有關list的相關實現,主函數沒有寫不少,每一個部分目前沒發現有問題;ios

#include <iostream>
#include <stdio.h>

using namespace std;

typedef int ElemType;
typedef int Status;
#define OK    1
#define ERROR 0

struct Node
{
    ElemType Data;
    Node* Prior;
    Node* Next;
} ;

struct List
{
    int length;
    Node* head;
    Node* tail;
    List(int l=0, Node* h=0, Node* t=0):length(l),head(h),tail(t) {}
} ;

void Pop_Front(List &L);///pop_front() 刪除第一個元素
void Pop_Front(List &L);///pop_front() 刪除第一個元素
void Push_back(List &L, int x);///push_back() 在list的末尾添加一個元素
void Push_front(List &L, int x);///push_front() 在list的頭部添加一個元素
bool Empty(List L);///empty() 若是list是空的則返回true
int Back(List L);///back() 返回最後一個元素
int Front(List L);///front() 返回第一個元素
void Clear(List &L); ///clear() 刪除全部元素
void Erase(List &L, int x);///erase() 刪除一個元素

int main()
{
    List L;

    Push_back(L, 1);
    Push_front(L, 2);
    Push_back(L, 3);/// 2 1 3
    //Push_back(L, 3);/// 2 1 3
    //Push_front(L, 4);/// 2 1 3

    int ans = Back(L);
    printf("%d\n", ans);

    int ans1 = Front(L);
    printf("%d\n", ans1);

    Erase(L, 3);

    //Pop_back(L);
    //Pop_back(L);

    //Clear(L);

    ans = Back(L);
    printf("%d\n", ans);

    return 0;
}
bool Empty(List L)///empty() 若是list是空的則返回true
{
    return (L.length == 0);
}

int Back(List L)///back() 返回最後一個元素
{
    if(Empty(L))
    {
        printf("鏈表爲空\n");
        return -1;
    }
    return L.tail->Data;
}
int Front(List L)///front() 返回第一個元素
{
    if( Empty(L) )
    {
        printf("鏈表爲空\n");
        return -1;
    }
    return L.head->Data;
}
void Clear(List &L) ///clear() 刪除全部元素
{
    while(!Empty(L))
    {
        Node* s = L.head;
        L.head = L.head->Next;
        L.length --;
        delete s;
    }
}

void Pop_back(List &L)///pop_back() 刪除最後一個元素
{
    if(Empty(L))
        return;
    Node* s = L.tail;

    L.tail = s->Prior;
    L.tail->Next = NULL;

    L.length --;

    delete s;
}

void Pop_Front(List &L)///pop_front() 刪除第一個元素
{
    if(Empty(L))
        return;
    Node* s = L.head;

    L.head = s->Next;
    L.head->Next = NULL;

    L.length --;

    delete s;
}

void Erase(List &L, int x)///erase() 刪除一個元素
{
    Node* p = L.head;

    while(p)
    {
        //printf("%d\n", p->Data);
        if(p->Data == x)
        {
            if(p->Prior == NULL)
                Pop_Front(L);
            else if(p->Next == NULL)
                Pop_back(L);
            else
            {
                Node* s = p;
                p->Prior->Next = p->Next;
                p->Next->Prior = p->Prior;
                delete s;
            }
        }
        p = p->Next;
    }

}
void Push_back(List &L, int x)///push_back() 在list的末尾添加一個元素
{
    Node* s = new Node;
    s->Data = x;

    if(Empty(L))
    {
        L.head = s;
        L.tail = s;
    }
    else
    {
        L.tail->Next = s;
        s->Prior = L.tail;
        L.tail = s;
    }
    L.length ++;
}
void Push_front(List &L, int x)///push_front() 在list的頭部添加一個元素
{
    Node* s = new Node;

    s->Data = x;
    if(Empty(L))
    {
        L.head = s;
        L.tail = s;
    }
    else
    {
        s->Next = L.head;
        L.head->Prior = s;
        L.head = s;
    }
    L.length ++;
}
View Code
相關文章
相關標籤/搜索