在c++; 我试图用C++中的双链表来构造括号匹配程序。 当我单独尝试Dlink类时,我确认我的Dlink类确实有效。然而,当我在我的括号类中创建一个Dlink对象时,这个角色似乎不在双链接列表中。我找不到我把代码弄乱的地方
代码如下:在c++; 我试图用C++中的双链表来构造括号匹配程序。 当我单独尝试Dlink类时,我确认我的Dlink类确实有效。然而,当我在我的括号类中创建一个Dlink对象时,这个角色似乎不在双链接列表中。我找不到我把代码弄乱的地方,c++,doubly-linked-list,parentheses,C++,Doubly Linked List,Parentheses,代码如下: #include "stdafx.h" #include <iostream> #include <cstdlib> using namespace std; class DNode { private: char data; DNode *next; DNode *prev; friend class Dlink; }; class Dlink { private: public: Dlink(); ~
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
class DNode {
private:
char data;
DNode *next;
DNode *prev;
friend class Dlink;
};
class Dlink {
private:
public:
Dlink();
~Dlink();
bool empty() const;
void addfront(char);
void removefront();
void addback(char);
void removeback();
void add(DNode*, const char);
void remove(DNode *);
const char& front() const;
const char& back() const;
int size();
void print();
int n;
private:
DNode * head;
DNode *tail;
};
Dlink::Dlink() {
head = new DNode;
tail = new DNode;
head->next = tail;
tail->prev = head;
n = 0;
}
Dlink::~Dlink() {
while (!empty()) removefront();
delete head;
delete tail;
}
void Dlink::add(DNode *v, const char e) {
DNode *u = new DNode;
u->data = e;
u->next = v;
u->prev = v->prev;
v->prev->next = u;
v->prev = u;
n++;
}
void Dlink::addfront(const char e) {
add(head->next, e);
}
void Dlink::addback(const char e) {
add(tail, e);
}
void Dlink::remove(DNode *v) {
DNode *u = v->prev;
DNode *w = v->next;
u->next = w;
w->prev = u;
delete v;
n--;
}
void Dlink::removefront() {
remove(head->next);
}
void Dlink::removeback() {
remove(tail->prev);
}
bool Dlink::empty() const {
return (head->next == tail);
}
const char& Dlink::front() const {
return head->next->data;
}
const char& Dlink::back() const {
return tail->prev->data;
}
int Dlink::size() {
return n;
}
void Dlink::print() {
cout << front() << endl;
}
class parenthesis {
private:
Dlink d;
public:
parenthesis(char[], int);
~parenthesis();
char str[20];
int i;
int flag;
int j = 0;
bool domatch();
bool isopen(char);
bool isclose(char);
int type(char);
char a;
};
parenthesis::parenthesis(char str[], int size) {
str = str;
i = size;
}
parenthesis::~parenthesis() {}
bool parenthesis::isopen(char a) {
a = a;
if (a == '(' || a == '{' || a == '[') return true;
else return false;
}
bool parenthesis::isclose(char a) {
a = a;
if (a == ')' ||a == '}' || a == ']') return true;
else return false;
}
int parenthesis::type(char a) {
a = a;
if (a == ')' || a == '(') return 1;
else if (a == '}' || a == '{') return 2;
else if (a == ']' || a == '[') return 3;
}
bool parenthesis::domatch() {
if (isclose(str[j])) {
return false;
}
else if (isopen(str[j])) {
d.addfront(str[j]);
flag = 1;
j++;
}
if (flag == 1) {
for (j; j < i; j++)
{
if (isopen(str[j])) {
d.addfront(str[j]);
}
else if (isclose(str[j])) {
if (type(d.front()) == type(str[j])) d.removefront();
else {
return false;
}
}
}
if (!d.empty()) return false;
else return true;
}
}
int main() {
char str[20];
cout << "enter string" << endl;
cin >> str;
parenthesis p1 = parenthesis(str, strlen(str));
return p1.domatch();
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
类DNode{
私人:
字符数据;
DNode*下一步;
DNode*prev;
好友类链接;
};
类数据链路{
私人:
公众:
Dlink();
~Dlink();
bool empty()常量;
void addfront(char);
void removefront();
无效加回(char);
void removeback();
无效添加(DNode*,常量字符);
消除空洞(DNode*);
常量char&front()常量;
常量char&back()常量;
int size();
作废打印();
int n;
私人:
DNode*头;
DNode*尾;
};
Dlink::Dlink(){
head=新的DNode;
tail=新的DNode;
头部->下一个=尾部;
尾部->上一个=头部;
n=0;
}
Dlink::~Dlink(){
而(!empty())removefront();
删除标题;
删除尾部;
}
void Dlink::add(数据节点*v,常量字符e){
DNode*u=新的DNode;
u->data=e;
u->next=v;
u->prev=v->prev;
v->prev->next=u;
v->prev=u;
n++;
}
void Dlink::addfront(常量字符e){
添加(头部->下一步,e);
}
void Dlink::addback(const char e){
加(尾,e);
}
void Dlink::remove(DNode*v){
DNode*u=v->prev;
DNode*w=v->next;
u->next=w;
w->prev=u;
删除第五条;
n--;
}
void Dlink::removefront(){
移除(头部->下一步);
}
void Dlink::removeback(){
移除(尾部->上一个);
}
bool Dlink::empty()常量{
返回(头部->下一步==尾部);
}
常量char&Dlink::front()常量{
返回磁头->下一步->数据;
}
常量char&Dlink::back()常量{
返回尾部->上一个->数据;
}
int-Dlink::size(){
返回n;
}
void Dlink::print(){
您的DLink类可能会遇到漏洞。它对节点链负责
parenthesis p1 = parenthesis(str, strlen(str));
此初始化不起作用,因为DLink没有正确的复制构造函数..当括号中的和DLink
的临时实例被销毁后,指针将悬空
简单的解决方法是在DLink中使用智能指针,这将遵循“全部”或“无”的规则,而智能指针只会在列表不再使用时销毁列表。或者,如果assigment不允许使用库类,则创建复制或移动构造函数
仅用于辅助,您完全可以避免使用时态对象,但您应该意识到这一缺陷。我正在尝试使用双链表制作一个括号匹配程序,这很不寻常。您通常使用堆栈或类似的数据结构来解决此问题,而不是使用链表。此外,使用堆栈的解决方案使用的代码远远少于至少你已经写了。我同意使用堆栈会容易得多,但是作业说我需要使用双链接列表。如果你需要双链接列表,你可以直接使用。不需要自己写。如果我做括号*p1=新括号(str,strlen(str))怎么办它还违反第三条规则吗?对不起,我真的不知道第三条规则@윤준한 我给出了链接。是的,这是一种方法,即使它没有删除底层类的问题。另一种方法是括号p{str,strlen(str)};
我刚刚删除了括号类并生成了一个parantise函数,现在它可以工作了。但我仍然不明白为什么它不能工作@윤준한 因为您没有以可以复制的方式编写DLink
。