100分,一道C++,想写的来吧,再追分

来源:百度知道 编辑:UC知道 时间:2024/05/31 05:08:32
考虑一个字符的双链表,头指针指向第一个字符。所有链表内插入的字符按照升序插入。
1 写一个程序,创建一个双链表,头指针已经指向第一个字符。
2 写一个“addNode"函数,在链表内恰当的位置插入节点,保证字符按照升序排列。按顺序测试输入 D G B C
E M R J H X A Y
3 写一个“cutnode”函数,根据输入删除一个节点,测试存在和不存在的字符。
4 写一个“outputlist”函数,在插入和删除之后输出更新过的双链表。
5 写一个“countlist”返回值返回到双链表内数据的个数。
6 创建上面函数的菜单。

写了很久,太辛苦了,追点分吧。
#include <iostream>
using namespace std;

class DoublyList;

class ListNode
{
friend class DoublyList;

private:
ListNode(char ch): data(ch), prev(NULL), next(NULL) {}
char data;
ListNode *prev;
ListNode *next;
};

class DoublyList
{
public:
DoublyList(char ch);
~DoublyList();

int countlist() const;
bool addNode(char ch);
bool cutnode(char ch);
void Clear();
void outputlist() const;

private:
ListNode *head;
int length;
};

DoublyList::DoublyList(char ch)
{
head = new ListNode(ch);
length = 1;
}

DoublyList::~DoublyList()
{
Clear();
head = NULL;
}

void DoublyList::Clear()
{
ListNode *cursor = head, *temp;

while (cursor)
{
temp = cursor;
cursor = cursor->next;
d