線性表的基本操作

weixin_34236869發表於2019-01-27

1、順序表的基本操作

using namespace std;
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define True 1
#define False 0
#define Ok 1
#define Error 0
#define Infeasible -1
#define Overflow -2
typedef int Status;
typedef int ElemType;


#define ListInitSize 100 //線性表儲存空間的初始分配量
#define ListIncrement 10//線性表儲存空間的分配增量


//動態申請儲存空間的順序表
typedef struct{//順序表定義
    ElemType *elem; //指示動態分配陣列的指標
    int listsize; //當前分配的儲存容量
    int length;//長度
}DynList;

Status DynInitList(DynList &L)//順序表的初始化
{
    L.elem = (ElemType*)malloc(ListInitSize*sizeof(ElemType));
    if(!L.elem)
        exit(Overflow);
    L.length = 0;
    L.listsize = ListInitSize;
    cout<<"初始化"<<endl;
    return Ok;
}

//線上性表L中第i個位置之前插入新的元素e
Status ListInsert_Sq(DynList &L,int i, ElemType e){
    //第一步:判斷資料是否合法
    if(i<1 || i> L.length +1)
        return Error;
    //第二步:判斷當前儲存空間已滿,增加分配
    if(L.length >= L.listsize)
    {
        ElemType *newbase;
        newbase = (ElemType *)realloc(L.elem,
            (L.listsize + ListIncrement)*sizeof(ElemType));
        if(!newbase)
            exit(Overflow);//終結程式
        L.elem = newbase; //新基址
        L.listsize += ListIncrement;//增加儲存容量
    }
    //插入元素:
    //1、先找到插入的位置
    //2、將所有的元素向後移動一個單元
    //3、插入元素,表長增1
    int *q,*p;
    q = &(L.elem[i-1]);
    for(p = &(L.elem[L.length-1]);p>=q;--p)
        *(p+1) = *p;
    *q = e;
    ++L.length;
    return Ok;
}

//順序表的輸出
Status ListOutput_Sq(DynList L)
{
    int len = L.length;
    for(int i=0;i<L.length;i++)
    {
        cout<<L.elem[i]<<"\t";
    }
}


//順序表的查詢(按值查詢)
Status ListLocate_Sq(DynList L,ElemType e)
{
    int loc=0;
    for(int i=0;i<L.length;i++){
        if(L.elem[i] == e)
            loc = i+1;
    }
    if(loc==0){
        cout<<"沒有這個值"<<endl;
    }
    else
        cout<<e<<"是第"<<loc<<"個值"<<endl;
    return loc;
}

//順序表的刪除
Status ListDelete_Sq(DynList &L,int e)
{
    int loc=0;
    loc = ListLocate_Sq(L,e);
    for(int i=loc-1;i<L.length+1;i++){
        L.elem[i] = L.elem[i+1];
    }
    L.length--;
    return Ok;
}

typedef struct Node{//單連結串列的定義
    ElemType data;
    struct Node *next;
}Node,*LinkedList;

LinkedList LinkedInit()//單連結串列的初始化
{
    Node *L;
    L = (Node*)malloc(sizeof(Node));
    if(L==NULL)
    {
        cout<<"記憶體分配失敗"<<endl;
    }
    else{
        cout<<"記憶體分配成功"<<endl;
    }
    L->next =NULL;
    return L;
}


int main()
{
    DynList DynL;
    DynInitList(DynL);
    ListInsert_Sq(DynL,1,1);
    ListInsert_Sq(DynL,1,61);
    ListInsert_Sq(DynL,1,84);
    ListInsert_Sq(DynL,1,52);
    ListInsert_Sq(DynL,1,11);
    ListInsert_Sq(DynL,4,48);
    ListOutput_Sq(DynL);
    ListLocate_Sq(DynL,61);
    ListDelete_Sq(DynL,61);
    ListOutput_Sq(DynL);
    //LinkedInit();
    return 0;
}

結果顯示:


15271323-3b9ae8d9b2a45066.png
image.png

補充:


15271323-0dd90d61051d07d0.png
image.png

2、連結串列


typedef struct Node{//單連結串列的定義
    ElemType data;
    struct Node *next;
}Node,*LinkedList;

LinkedList LinkedInit()//單連結串列的初始化
{
    Node *L;
    L = (Node*)malloc(sizeof(Node));
    if(L==NULL)
    {
        cout<<"記憶體分配失敗"<<endl;
    }
    else{
        cout<<"記憶體分配成功"<<endl;
    }
    L->next =NULL;
    return L;
}

int LinkLength(LinkedList L)//求連結串列的長度
{
    int len = 0;
    while(L->next)
    {
        len++;
    }
    return len;
}

Status LinkInsert(LinkedList &L,int i,ElemType e)//單連結串列的插入
{
    /*
    int length = LinkedLength(L);
    if(i>length)
        exit(Overflow);
    */
    Node *pre,*pi;
    pre = L;
    int num = 0;

    while(pre && num < i-1)
    {
        pre = pre->next;
        num++;
    }
    pi = (Node*)malloc(sizeof(Node));
    pi->data = e;
    pi->next = pre->next;
    pre->next = pi;
    return Ok;
}

Status LinkedLocate(LinkedList L,ElemType e)//按值查詢
{
    Node *p;
    p = L->next;
    int num=1;
    while(p&&p->data!=e)
    {
        num++;
        p = p->next;
    }
    cout<<e<<"是第"<<num<<"個元素"<<endl;
    return num;
}

Status LinkedDelete(LinkedList L,ElemType e)//連結串列的刪除
{
    int num = LinkedLocate(L,e);
    Node *pre,*pi;
    pre = L;
    int j=0;
    while(pre->next&&j<num-1)
    {
        pre = pre->next;
        j++;
    }
    pi = pre->next;
    pre->next = pi->next;
    free(pi);
    return Ok;
}

Status LinkedOutput(LinkedList L)//連結串列的輸出
{
    Node *p;
    p = L->next;
    while(p)
    {
        cout<<p->data<<"\t";
        p = p->next;
    }
    return Ok;
}

int main()
{
    LinkedList LinkL;
    LinkL = LinkedInit();
    int length = LinkLength(LinkL);
    cout<<"這個連結串列的長度為:"<<length<<endl;
    LinkInsert(LinkL,0,65);//&是傳輸,函式里加,呼叫的時候不加
    LinkInsert(LinkL,0,6);
    LinkInsert(LinkL,0,5);
    LinkInsert(LinkL,0,56);
    LinkedOutput(LinkL);
    LinkedDelete(LinkL,6);
    LinkedOutput(LinkL);
    return 0;
}

結果顯示:


15271323-0aeffe7f4bb17b2f.png
image.png

相關文章