日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【数据结构】广义表

發布時間:2023/11/30 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【数据结构】广义表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、問題概述

廣義表是非線性的數據結構,是由若干個元素組合而成的,廣義表中可以有子表,類似這樣的:



我們以C=(a,b,(c,d))為例,將它定義為這樣的數據結構:


我們會給定字符串的形式,如:char * str = "(a,b,(c,d))";???? 然后將它轉化為如上的數據結構。


二、解決辦法

(1)將符號'('看作是頭節點,然后將是數值的部分鏈入當前位置的下一個位置;

(2)當遇到SUB時,就鏈入它的_sublink(連接子表的指針),此時可用遞歸(子表相當于是表的子問題);

(3)直到str為空時,廣義表的數據結構創建完成。


三、實現代碼

//GeneralizedList.h

#pragma once #include<iostream> using namespace std; #include<assert.h>enum Type {HEAD,VALUE,SUB, };struct GeneralizeListNode {Type _type;GeneralizeListNode* _next;union{char _value;GeneralizeListNode* _sublink;};GeneralizeListNode(Type type,char value = 0):_next(NULL),_type(type),_value(value){if(type == VALUE){_value = value;}if(type == SUB){_sublink = NULL;}} };class GeneralizeList {typedef GeneralizeListNode Node; public:GeneralizeList():_head(NULL){}//構造函數GeneralizeList(const char* str){_head = Create(str);}//拷貝構造函數GeneralizeList(const GeneralizeList& g){_head = _Copy(g._head);}//賦值運算符重載(1)//GeneralizeList& operator=(const GeneralizeList& g)//{// if(this != &g)// {// Node* tmp = _Copy(g._head);// delete _head;// _head = tmp;// }// return *this;//}//賦值運算符重載(2)GeneralizeList& operator=(const GeneralizeList& g){if(_head != g._head){GeneralizeList tmp(g);swap(_head,tmp._head);}return *this;}~GeneralizeList(){if(_head){_Destroy(_head);_head = NULL;}}void Print(){_Print(_head);cout<<endl;}public:Node* Create(const char*& str) //創建表{assert(*str == '(');Node* head = new Node(HEAD);++str;Node* tail = head;while(*str){if((*str >= '0' && *str <= '9')||(*str >= 'a' && *str <= 'z')||(*str >= 'A' && *str <= 'Z')){tail->_next = new Node(VALUE,*str);tail = tail->_next;++str;}else if(*str == '('){tail->_next = new Node(SUB);tail = tail->_next;tail->_sublink = Create(str);++str;}else if(*str == ')'){++str;return head;}else{++str;}}return head;}void _Print(Node* head){assert(head);Node* cur = head;while(cur){if(cur->_type == HEAD){cout<<"(";}else if(cur->_type == VALUE){cout<<cur->_value;if(cur->_next != NULL){cout<<",";}}else if(cur->_type == SUB){_Print(cur->_sublink);if(cur->_next != NULL){cout<<",";}}else{assert(false);}cur = cur->_next;}cout<<")";}Node* _Copy(Node* head){Node* cur = head;Node* newHead = new Node(HEAD);Node* newTail = newHead;while(cur){if(cur->_type == HEAD){cur = cur->_next;}else if(cur->_type == VALUE){newTail->_next = new Node(VALUE,cur->_value);newTail = newTail->_next;cur = cur->_next;}else if(cur->_type == SUB){newTail->_next = new Node(SUB);newTail = newTail->_next;newTail->_sublink = _Copy(cur->_sublink);cur = cur->_next;}else{assert(false);}}return newHead;}void _Destroy(Node* head) //銷毀表{Node* cur = head;while(cur){if(cur->_type == HEAD || cur->_type == VALUE){Node* del = cur;cur = cur->_next;delete del;}else if(cur->_type == SUB){Node* del = cur;cur = cur->_next;_Destroy(del->_sublink);delete del;}else{assert(false);}}}size_t Size(){return _Size(_head);}size_t _Size(Node* head) //元素個數{assert(head);size_t count = 0;Node* cur = head;while(cur){if(cur->_type == VALUE){++count;}else if(cur->_type == SUB){count += _Size(cur->_sublink);}cur = cur->_next;}return count;}size_t Depth(){return _Depth(_head);}size_t _Depth(Node* head) //表的深度{Node* cur = head;size_t MaxDepth = 1;while(cur){if(cur->_type == SUB){size_t Depth = _Depth(cur->_sublink);if(Depth+1 > MaxDepth){MaxDepth = Depth+1;}}cur = cur->_next;}return MaxDepth;} protected:Node* _head; };void FunTest() {char* str = "(a,b,(c,d),(e,(f),g))";GeneralizeList g1(str);g1.Print();GeneralizeList g2(g1);g2.Print();GeneralizeList g3;g3 = g1;g3.Print();cout<<g3.Size()<<endl;cout<<g3.Depth()<<endl; }
//GeneralizedList.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include"GeneralizedList.h" int main() {FunTest();return 0; }

四、運行結果





總結

以上是生活随笔為你收集整理的【数据结构】广义表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。