C++實現順序表和單鏈表

創建文件命名爲sqlist.h

#ifndef SQLIST_H
#define SQLIST_H

typedef int ElemType;

class SqList
{
public:
    SqList();                             // 無參構造函數,建立空的順序表
    SqList(ElemType array[], int len);    // 有參構造函數,構建len個數據元素的順序表

    ~SqList();                            // 析構函數

    ElemType Get(int index);              // 按位置查找
    int Find(ElemType x);                 // 按值查找
    bool Insert(int index, ElemType x);   // 插入操作
    bool Delete(int index);               // 刪除操作
    void PrintList();                     // 打印順序表

private:
    ElemType *elem;                       // 存儲空間基址
    int length;                           // 當前長度
    int maxLen;                           // 存儲空間的大小,根據第一次構造確定
};

/*
 * 單鏈表
 * 採用尾插法
 */

struct Node
{
    ElemType data;
    Node *next;
};

class LinkList
{
public:
    LinkList();                             // 無參構造函數,建立空的單鏈表
    LinkList(ElemType array[], int len);    // 有參構造函數,構建含有len個Node節點的鏈表

    ~LinkList();        
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章