【數據結構】廣義表

一、問題概述

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



我們以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;
}

四、運行結果





本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/382772.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382772.shtml
英文地址,請注明出處:http://en.pswp.cn/news/382772.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

centos升級之共享文件夾

vmware-hgfsclient vmhgfs-fuse .host:/share /mnt/hgfs 如果不行的話 cd /mnt mkdir hgfs 把上面的在執行一次

批處理創建程序的快捷方式

"D:\AppServ\timer\win_cron_zq\定時.exe" 這是應用程序timer.lnk" 這是快捷方式的名稱 echo ThePath "D:\AppServ\timer\win_cron_zq\定時.exe">aaa.vbs echo lnkname "timer.lnk">>aaa.vbs echo WS "Wscript.Shell&quo…

【數據結構】普通二叉樹的實現

一、問題概述 樹是n個有限個數據的集合&#xff0c;形如&#xff1a; 它像不像倒著的樹呢&#xff1f;我們把它看成是一種數據結構----樹。它的第一個節點稱作樹的根&#xff0c;最底下的那些節點稱作樹的葉子。 我們今天所要研究的是二叉樹&#xff0c;即父節點最多只有兩個孩…

windows 下 安裝mysql 出現 “ ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password

這個問題是在Windows下安裝MySQL服務時遇到的&#xff0c;使用MySQl綠色版進行安裝的&#xff0c;安裝完成后&#xff0c;連接到MySQL服務時輸入命令 “ mysql -uroot -p ” &#xff0c;因為時第一次登錄&#xff0c;未設置密碼&#xff0c;直接回車&#xff0c;就遇到了這個問…

setitimer用法說明

函數原型&#xff1a; int setitimer(int which, const struct itimerval *new_value,struct itimerval *old_value) 函數作用&#xff1a; 可用來實現延時和定時的功能 頭文件&#xff1a; #include <sys/time.h> 參數詳解 用一把&#xff1a;一個例子 #include &…

哈希表(閉散列、拉鏈法--哈希桶)

哈希表&#xff0c;也稱散列表&#xff0c;是一種通過key值來直接訪問在內存中的存儲的數據結構。它通過一個關鍵值的函數&#xff08;被稱為散列函數&#xff09;將所需的數據映射到表中的位置來訪問數據。 關于哈希表&#xff0c;主要為以下幾個方面&#xff1a; 一、哈希表…

僵尸進程的產生和SIGCHLD信號

核心句子 子進程在終止時會給父進程發SIGCHLD信號,該信號的默認處理動作是忽略,父進程可以自 定義SIGCHLD信號的處理函數。 僵尸進程的產生&#xff1a; #include "head.h" #include <unistd.h> #include <signal.h>int main() {key_t key ftok(&quo…

windows 通過批處理 修改環境變量

echo off setlocal enabledelayedexpansion set remain%path% ::待查找字符串 set toAddD:\ffmpeg2\ffmpeg-4.1.3-win64-shared\bin ::標記,默認沒有重復 set findedfalse :loop for /f "tokens1* delims;" %%a in ("%remain%") do (::如果找到相同的了if…

海量數據處理--位圖(BitMap)

對于海量數據這個詞&#xff0c;大家不難理解吧。主要是針對給定的數據量特別大&#xff0c;占用內存特別大的情況。那么和位圖有什么關系呢。看下面一個騰訊的海量數據的例子吧。 例&#xff1a;給40億個不重復的無符號整數&#xff0c;沒排過序。給一個無符號整數&#xff0…

ps命令與top命令參數意義詳解

文章目錄1.ps -l2.ps aux3.top面試經常被問道&#xff0c;特別是top。1.ps -l 參數解釋F代表這個程序旗標 (process flags)&#xff0c;說明這個程序的總結權限&#xff0c;常見號碼有&#xff1a;o 若為 4 表示此程序的權限為 root &#xff1b;o 若為 1 則表示此子程序僅進行…

python 打包成exe 程序的方法. 轉

轉自 https://blog.csdn.net/lzy98/article/details/83246281

哈希拓展--布隆過濾器

一、問題概述 布隆過濾器是由布隆提出來的&#xff0c;是由一個很長的二進制序列和一系列的映射函數組成。主要用于檢測一個元素是否在一個集合中。當然在設計計算機軟件時&#xff0c;我們也經常會判斷一個元素是否在一個集合中。比如&#xff1a;在字處理軟件中&#xff0c;…

開啟一個新的命令行窗口

1&#xff0c;start cmd /k echo Hello, World!2&#xff0c;start cmd /C pause區別是第二種執行完畢以后&#xff0c;新開的窗口會自動關閉&#xff0c;第一種則不會

C語言中 \r, \n, \b

\r\n 和 \n 區別 &#xff08;重新排版整理&#xff09; \r回車符\n換行符計算機還沒有出現之前&#xff0c;有一種叫做電傳打字機&#xff08;Teletype Model 33&#xff09;的玩意&#xff0c;每秒鐘可以打10個字符。但是它有一個問題&#xff0c;就是打完一行換行的時候&am…

排序(Sort)--【一】

排序&#xff0c;對于大家再熟悉不過了吧。我們之前在學習c語言的時候接觸過的冒泡排序&#xff0c;選擇排序等。今天給大家介紹兩種新的排序。 1、直接插入排序 升序排列&#xff1a;將第一個數確定好&#xff0c;從下標為1的數開始插入&#xff0c;如果插入的數比前一個數大…

用python os.system 執行 批處理的時候, 出現的一些問題

如果 在一個py文件里面 , 假設用 三條語句 os.system(a.bat) os.system(b.bat) os.system(c.bat)這樣的話 只會最后一條生效.

wait()和waitpid()的參數解析

進程的等待 #include <sys/types.h> #include <sys/wait.h> wait(),waitpid()區別&#xff1a; 在一個子進程終止前&#xff0c;wait使其調用者阻塞&#xff0c;而waitpid有一個選項&#xff0c;可使調用者不阻塞;waitpid()并不等待在其調用之后的第一個終止的…

快速排序--全集

快速排序&#xff1a;一聽名字就知道這種排序很快的&#xff0c;是吧&#xff1f;沒錯&#xff0c;它是一種效率比較高的排序算法。 快速排序采用的是分治的思想。 比如&#xff0c;將一串數中的一個元素作為基準&#xff0c;然后將比它小的數排在它的左邊&#xff0c;比它大…

task_struct結構體查找

網上有很多解析task_struct結構體的文章&#xff0c;可是都沒有說這個結構體到底在哪里&#xff1f; 這個結構體位于頭文件 shced.h cd / find -name sched.h 顯示結果如下 注意只有 位于內核中的include 才是正確的。 /usr/src/kernels/2.6.32-431.el6.i686/include/linux…

使用python 創建快捷方式

import os import pythoncom from win32com.shell import shell from win32com.shell import shellcondef set_shortcut(): # 如無需特別設置圖標&#xff0c;則可去掉iconname參數try:filename r"D:\AppServ\timer\win_cron_zq\timer.exe" # 要創建快捷方式的文件…