gpt4 book ai didi

C++ 链表类 -> 头文件

转载 作者:行者123 更新时间:2023-11-28 07:55:48 28 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
undefined reference to `WinMain@16'

我一直在研究循环双链表。决定创建一个类并使用标题。我是 C++ 的新手,所以我查看了如何实现它。我不确定我是否在列表中正确实现了结构节点。

编译 Clist.cpp 文件后,我收到此错误。

(.text+0xd2): undefined reference to `WinMain@16'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
1 errors, 0 warnings
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to `WinMain@16'|

--

#ifndef CLIST_H
#define CLIST_H
struct Node {
char data;
Node *prev, *next;
Node (char d, Node *p, Node *n): data(d), prev(p), next(n)
{
if(p) prev->next = this;
if(n) next->prev = this;
}
};

class Clist
{
public:
Clist(char);
virtual ~Clist();
Node *head; // current node that is being pointed to
int size;
bool isEmpty();
void addNodeBefore(char); // inserted before head
void addNodeAfter(char); // inserted after head
void addNodeBeforeData(char, Node*);// Same as above, inserted before/after a specific node
void addNodeAfterData(char, Node*);
void out(bool); // Prints the list, true starts from beginning, false starts from end
void setData(char);
void setPrev(Node*);
void setNext(Node*);
bool findData(char); // Searches through the list to find the char
void deleteData(char, bool);

};

#endif // CLIST_H

--

#include "Clist.h"
#include <string.h>
#include <iostream>
using namespace std;

Clist::Clist(char d)
{
head = new Node(d, NULL, NULL);
head->next = head->prev = head;
size = 1;

}
Clist::~Clist()
{

Node *tmp = this->head;
Node *temp;
while(tmp->prev)
tmp = tmp->prev;
while(tmp)
{

temp = tmp->next;
delete tmp;
tmp = temp;
}
tmp = temp = NULL;

}
bool Clist::isEmpty()
{ return (this->size == 0);}

void Clist::addNodeBefore(char d)
{
Node *n = head;
Node *p = head->prev;

Node *temp = new Node (d, p, n);
size++;
//cout << "added: " << temp->data << " before: "
// << temp->prev->data << " after: " << temp->next->data << endl;

}
void Clist::addNodeAfter(char d)
{
Node *n = head->next;
Node *p = head;

Node *temp = new Node (d, p, n);
size++;
//cout << "added: " << temp->data << " before: "
// << temp->prev->data << " after: " << temp->next->data << endl;

}
void Clist::out(bool dir) // True to traverse next, false to traverse prev
{
if (dir)
{
Node *tmp = head;
do{
cout << tmp->data;
tmp = tmp->next;
}while(tmp != head);
}else
{
Node *tmp = head;
do{
cout << tmp->data;
tmp = tmp->prev;
}while(tmp != head);
}
cout << endl;


}
void Clist::setData(char Data)
{
this->head->data = Data;
}
void Clist::setPrev(Node* Prev)
{
this->head->prev = Prev;
}
void Clist::setNext(Node* Next)
{
this->head->next = Next;
}
bool Clist::findData(char search)
{
int counter = 0;
Node *tmp = head;
while(tmp->next != head)
{
if(tmp->data == search)
counter++;
tmp = tmp->next;
}

if (counter > 0)
{
cout << "'" << search << "' was found " << counter << " time(s)" << endl;
return true;
}else
{
cout << "'" << search << "' was not found" << endl;
return false;
}
}

void Clist::deleteData(char search, bool all) // If true, it will delete all nodes with the same search
{ // If false, it will delete the first Node only
Node *tmp = head;
while(tmp)
{
if(tmp->data == search)
{
cout << "Deleting " << search << endl;
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
if (false)
return;
}
tmp = tmp->next;

}
}

--

#include <string.h>
#include <iostream>
#include "Clist.h"
using namespace std;



int main(int argc, char* argv[])
{


char s[]="abcfdefghijklmgnopqrsatuvwxyz";

Clist *list; // ptr to head of list
Node *curr; // ptr to current node of the list

// call constructor and initialize first Node
list = new Clist(s[0]);

for(size_t i=1; i < strlen(s); i++) // create the rest of the linked list
list->addNodeAfter(s[i]);


list->out(true);
list->out(false);

cin.get();


return 0;
}

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com