廣義表的C++實現(包含建構函式,拷貝建構函式,解構函式,獲取表頭,表尾,獲取直接後繼等操作)
阿新 • • 發佈:2019-01-09
資料結構是資料在系統中的物理儲存結構,自己編寫資料結構可以對某種資料結構的物理和邏輯結構有更深的瞭解。
一、廣義表作為一種資料結構有以下的特點:
1. 廣義表是遞迴的;
2. 廣義表的元素可以使廣義表,也可以是值;
3. 廣義表是有序的;
4. 廣義表是有長度有深度的。
二、廣義表的建立:
廣義表節點的儲存結構表示為:
圖 2-1
utype:
typedef enum{HEAD,VALUE,CHILDLIST}UTYPE;
info:
union
{
//HADE 引用計數,預設0,被引用為1
int ref;
//VALUE 存放值
int value;
;//CHILDLIST 存放子表地址
GenListNode *hlink
}info;
三、廣義表完整程式碼:
GenList.h
#ifndef _GLIST_H #define _GLIST_H #include <iostream> #include <assert.h> using namespace std; typedef enum{HEAD,VALUE,CHILDLIST}UTYPE; class GenList; class GenListNode { friend GenList; public: GenListNode(); GenListNode(UTYPE ut, int val); ~GenListNode() {} private: UTYPE utype; //HEAD VALUE CHILDLIST union { int ref; //HADE 引用計數,預設0,被引用為1 int value; //VALUE 存放值 GenListNode *hlink; //CHILDLIST 存放子表地址 }info; GenListNode *tlink; //尾指標 }; class GenList { public: GenList(); GenList(const GenList &GL); ~GenList(); public: void CreateGenList(char *str); //建立 void Head(GenListNode &GN)const; //求第一個元素 GenListNode* First()const; //返回表頭指標 GenListNode* Tail()const; //返回表尾指標 GenListNode* Next(GenListNode *p)const;//求直接後繼 int Length()const; //求廣義表長度 int Depth()const; //求廣義表深度 private: void free(GenListNode *p); GenListNode* Copy(GenListNode *p); int Length(GenListNode *s)const; int Depth(GenListNode *s)const; void CreateList(GenListNode *&ls, char *str); void server(char *sub, char *hsub); //拆分字串為頭和尾 GenListNode *first; }; #endif
GenList.cpp
#include"GList.h" GenListNode::GenListNode() :utype(HEAD), tlink(NULL) { info.ref = 0; } GenListNode::GenListNode(UTYPE ut, int val) :tlink(NULL) { utype = ut; if (utype == HEAD) { info.ref = val; } else if (utype == VALUE) { info.value = val; } } GenList::GenList() { first = new GenListNode; } void GenList:: CreateGenList(char *str) { CreateList(first, str); } void GenList::CreateList(GenListNode *&ls, char *str) { //對字串去前後括號 int n = strlen(str); str[n - 1] = '\0'; strcpy_s(str, n - 1, str + 1); if ((strlen(str) == 0) || (strcmp(str, "()") == 0)) { ls->tlink = NULL; } char *sub = str; char *hsub = new char[n - 2 + 1]; GenListNode *p = ls; if (p == NULL) { ls = new GenListNode(HEAD, 1); p = ls; } while (strlen(sub) != 0) { p = p->tlink = new GenListNode; //橫向 server(sub, hsub); //將字串拆分為表頭和表尾 if (hsub[0] == '(') //CHILDLIST { p->utype = CHILDLIST; CreateList(p->info.hlink, hsub); //縱向 } else { p->utype = VALUE; p->info.value = atoi(hsub); } } } void GenList::server(char *sub, char *hsub) { int n = strlen(sub); int i = 0; char ch = sub[0]; int k = 0; //括號匹配計數 while (i < n && (ch != ',' || k != 0)) { if (ch == '(')k++; else if (ch == ')')k--; i++; ch = sub[i]; //獲取下一個字元 } if (i < n) { sub[i] = '\0'; strcpy_s(hsub, i + 1, sub); strcpy_s(sub, n - i, sub + i + 1); } else if (k != 0) { return; } else { strcpy_s(hsub, n + 1, sub); sub[0] = '\0'; } } void GenList::Head(GenListNode &GN)const { if (first->tlink != NULL) { GN.utype = first->tlink->utype; GN.info = first->tlink->info; } } GenListNode* GenList::First()const { return first->tlink; } GenListNode* GenList::Tail()const { return first->tlink->tlink; } GenListNode* GenList::Next(GenListNode *p)const { return p->tlink; } int GenList::Length()const { return Length(first->tlink); } int GenList::Depth()const { return Depth(first->tlink); } int GenList::Length(GenListNode *s)const { if (s == NULL) return 0; else return 1 + Length(s->tlink); } int GenList::Depth(GenListNode *p)const { int max = 0; int de; while ((p != NULL) && (p->utype != CHILDLIST)) { p = p->tlink; } if (p == NULL) { return 1; } else { max = Depth(p->info.hlink) + 1; de = Depth(p->tlink); return max > de ? max : max = de; } } GenList::GenList(const GenList &GL) { first = Copy(GL.first); } GenListNode* GenList::Copy(GenListNode *p) { GenListNode *q = NULL; if (p != NULL) { q = new GenListNode; q->utype = p->utype; switch (p->utype) { case HEAD: q->info.ref = p->info.ref; break; case VALUE: q->info.value = p->info.value; break; case CHILDLIST: q->info.hlink = Copy(p->info.hlink); break; default: break; } q->tlink = Copy(p->tlink); } return q; } GenList::~GenList() { free(first); } void GenList::free(GenListNode *p) { if (p == NULL) { return; } switch (p->utype) { case HEAD: free(p->tlink); break; case VALUE: free(p->tlink); break; case CHILDLIST: free(p->tlink); free(p->info.hlink); break; default: break; } delete p; }
Test.cpp
#include "GList.h"
int main(int argc, char *argv[])
{
char ge[] = "((3,4),(5),(6),((7)))";
GenList gen;
gen.CreateGenList(ge);
int x=gen.Depth();
int y=gen.Length();
GenList gen1(gen);
return 0;
}
圖 4-1 除錯結果