Sorting 双链表:按升序排序节点
我正在做一个使用双链表的程序。我已经获得了插入节点、删除节点、从节点检索值、显示列表、输出列表大小、告知列表是否为空以及删除所有节点的功能 我遇到的问题是按升序对值进行排序,并查找某个值所在的节点 我明天要去一个帮助实验室,我只是想在那之前看看是否有人能帮忙 我不知道如何处理sort()。 我的find()有些工作,但当我要求它查找值的节点时,它只返回我输入的值 编辑:deleteAll()现在可以正常工作了Sorting 双链表:按升序排序节点,sorting,doubly-linked-list,Sorting,Doubly Linked List,我正在做一个使用双链表的程序。我已经获得了插入节点、删除节点、从节点检索值、显示列表、输出列表大小、告知列表是否为空以及删除所有节点的功能 我遇到的问题是按升序对值进行排序,并查找某个值所在的节点 我明天要去一个帮助实验室,我只是想在那之前看看是否有人能帮忙 我不知道如何处理sort()。 我的find()有些工作,但当我要求它查找值的节点时,它只返回我输入的值 编辑:deleteAll()现在可以正常工作了 #include <iostream> #include <limi
#include <iostream>
#include <limits>
using namespace std;
struct node
{
int data; //data
struct node *next; //link to next node
struct node *back; //link to previous node
};
class LinkedList //class LinkedList
{
struct node *root;
struct node *end;
int size;
public:
LinkedList() : root(NULL), size(0){}
bool isEmpty() const { return (root == NULL); }
void create(); //cleate list
void insert(); //insertion
void remove(); //deletion
void display(); //display list
void sort(); //sort list
void find(); //find a values' node
void deleteAll(); //delete all values
void retreive(); //retrive node
void getSize(); //return the number of nodes
};
//Creating a new list
void LinkedList::create()
{
struct node *nxt_node;
struct node *prev_node;
int value;
int n;
int i;
root = nxt_node = NULL;
cout << "How many nodes will the list have?: ";
cin >> n;
cout << "Enter the numbers to create the list: ";
for(i = 1; i <= n; i++)
{
cin >> value;
nxt_node = new node;
nxt_node->data = value;
nxt_node->next = NULL;
nxt_node->back = NULL;
if(root == NULL)
{
root = nxt_node;
}
else
{
prev_node->next = nxt_node;
nxt_node->back = prev_node;
}
prev_node = nxt_node;
}
end = nxt_node;
size = n;
cout << endl;
cout << "The list has been created!" << endl;
cout << endl;
display();
}
//Displaying the list
void LinkedList::display()
{
if(isEmpty())
{
cout << "The list is empty!" << endl;
return;
}
else
{
struct node *tmp = root;
cout << "The list is: ";
cout << "root -> ";
while(tmp != NULL)
{
cout << tmp->data << " -> ";
tmp = tmp->next;
}
cout << "end";
cout << endl << endl;
}
}
//Instert a node anywhere
void LinkedList::insert()
{
int pos;
int dat;
struct node *ptr_b;
struct node *ptr_f;
struct node *tmp;
cout << "At what node do you want to insert the new data?: " << endl;
cout << "(If the node is not found the data will be added to the beginning)" << endl;
cout << "Node: ";
cin >> pos;
cout << endl;
cout << "Enter the data to be inserted: ";
cin >> dat;
ptr_b = root;
tmp = new node;
tmp->data = dat;
while(ptr_b->next != NULL && ptr_b->data != pos)
{
ptr_b = ptr_b->next;
}
if(ptr_b->next == NULL && ptr_b->data != pos) //insertion to front
{
root->back = tmp;
tmp->next = root;
tmp->back = NULL;
root = tmp;
}
else if(ptr_b->next == NULL && ptr_b->data == pos) //insertion at the end
{
ptr_b->next = tmp;
tmp->next = NULL;
tmp->back = ptr_b;
end = tmp;
}
else // insertion between two nodes
{
ptr_f = ptr_b->next;
tmp->next = ptr_b->next;
tmp->back = ptr_b;
ptr_b->next = tmp;
ptr_f->back = tmp;
}
size++;
cout << "The new data has been inserted!" << endl << endl;
display();
}
//remove any node
void LinkedList::remove()
{
int loc;
int dat;
struct node *ptr_b;
struct node *ptr_f;
struct node *tmp;
cout << "Enter the node to delete: ";
cin >> loc;
tmp = root;
if(loc < size && loc > 0) //return the value at loc
{
int ind = 0;
while(++ind < loc && tmp->next != NULL)
{
tmp = tmp->next;
}
}
if(tmp->next == NULL && tmp->data != loc) //Data not found
{
cout << "Node not found!" << endl << endl;
return;
}
else if(tmp->next == NULL && tmp->data == loc) //Delete from the end of the list
{
ptr_b = tmp->back;
dat = tmp->data;
ptr_b->next = NULL;
end = ptr_b;
}
else //Delete between two nodes or first node
{
dat = tmp->data;
if(root == tmp) //delete the first node
{
root = tmp->next;
ptr_f = root;
ptr_f->back = NULL;
}
else //deletion between two nodes
{
dat = tmp->data;
ptr_b = tmp->back;
ptr_b->next = tmp->next;
ptr_f = ptr_b->next;
ptr_f->back = ptr_b;
}
}
delete tmp;
size--;
cout << endl;
cout << "The node has been deleted!" << endl << endl;
display();
}
//retrieve a nodes data value
void LinkedList::retreive()
{
int loc;
//if the list is empty, say so
if(isEmpty())
{
cout << "The List is empty!";
return;
}
else
{
//If the list is not empty
cout << "What nodes' value do you want to retrieve?: ";
cin >> loc;
if(loc < size && loc > 0) //return the value at loc
{
struct node *tmp = root;
int ind = 0;
while(++ind < loc && tmp->next != NULL)
{
tmp = tmp->next;
}
cout << "The nodes value is: " << tmp->data << endl;
cout << endl;
}
else //if the node is out of range
{
cout << "Invailid location!" << endl;
}
}
}
//sort the list in acending order
void LinkedList::sort()
{
cout << "The list has been sorted!" << endl;
}
//delete all the values
void LinkedList::deleteAll()
{
struct node *tmp;
for(int i = 1; i < size; i++)
{
while(root != NULL)
{
tmp = root;
root = tmp->next;
delete tmp;
}
}
display();
}
//find a values' node
void LinkedList::find()
{
int value;
//if the list is empty, say so
if(isEmpty())
{
cout << "The List is empty!";
return;
}
else
{
//If the list is not empty
cout << "What values' node do you want to find?: ";
cin >> value;
struct node *tmp = root;
int ind = 0;
while(++ind < value && tmp->data != value)
{
tmp = tmp->next;
}
if(tmp->next == NULL && tmp->data != value) //Data not found
{
cout << "Value not found!" << endl << endl;
return;
}
else
{
cout << "The value is at node: " << tmp->data << endl;
cout << endl;
}
}
}
void LinkedList::getSize()
{
cout << "The list has " << size << " nodes" << endl << endl;
}
int main()
{
LinkedList list;
list.create(); //create a new list
int choice;
while(1)
{
cout << "What would you like to do? (1-7): " << endl;
cout << "1. Insert a number" << endl;
cout << "2. Delete a node" << endl;
cout << "3. Delete ALL values" << endl;
cout << "4. Retrieve a nodes' value" << endl;
cout << "5. Retrives a values' node" << endl;
cout << "6. Return the size of the list" << endl;
cout << "7. Exit the program" << endl;
cout << endl;
cout << "Enter your choice (1-7): ";
cin >> choice;
cout << endl;
switch(choice)
{
case 1: //insertion
list.insert();
break;
case 2: //deletion
list.remove();
break;
case 3: //delete all the values
list.deleteAll();
break;
case 4: //retrieve a nodes' value
list.retreive();
break;
case 5: //retrieve a values' node
list.find();
break;
case 6: //returns the lists size
list.getSize();
break;
case 7: //Exit the program
exit(0);
break;
default:
cout << "Please enter a vaild choice (1-7)!" << endl;
break;
}
}
return 0;
}
#包括
#包括
使用名称空间std;
结构节点
{
int data;//数据
struct node*next;//链接到下一个节点
struct node*back;//链接到上一个节点
};
类链接列表//类链接列表
{
结构节点*根;
结构节点*结束;
整数大小;
公众:
LinkedList():根(NULL),大小(0){}
bool isEmpty()常量{return(root==NULL);}
void create();//清除列表
void insert();//插入
void remove();//删除
void display();//显示列表
void sort();//排序列表
void find();//查找值节点
void deleteAll();//删除所有值
void retrieve();//检索节点
void getSize();//返回节点数
};
//创建新列表
void LinkedList::create()
{
结构节点*nxt_节点;
结构节点*上一个节点;
int值;
int n;
int i;
root=nxt\u node=NULL;
cout>n;
库特值;
nxt_节点=新节点;
nxt_节点->数据=值;
nxt_node->next=NULL;
nxt_节点->返回=空;
if(root==NULL)
{
根=nxt_节点;
}
其他的
{
上一个节点->下一个=nxt\u节点;
nxt\u节点->返回=上一个节点;
}
prev_节点=nxt_节点;
}
end=nxt_节点;
尺寸=n;
cout-next=tmp;
tmp->next=NULL;
tmp->back=ptr_b;
end=tmp;
}
else//在两个节点之间插入
{
ptr_f=ptr_b->next;
tmp->next=ptr_b->next;
tmp->back=ptr_b;
ptr_b->next=tmp;
ptr_f->back=tmp;
}
大小++;
下一个空)
{
tmp=tmp->next;
}
}
if(tmp->next==NULL&&tmp->data!=loc)//未找到数据
{
后退;
dat=tmp->数据;
ptr_b->next=NULL;
结束=ptr_b;
}
else//在两个节点或第一个节点之间删除
{
dat=tmp->数据;
if(root==tmp)//删除第一个节点
{
root=tmp->next;
ptr_f=根;
ptr\u f->back=NULL;
}
else//在两个节点之间删除
{
dat=tmp->数据;
ptr_b=tmp->back;
ptr_b->next=tmp->next;
ptr_f=ptr_b->next;
ptr\u f->back=ptr\u b;
}
}
删除tmp;
大小--;
cout对于delete,所有For循环都需要从0开始,而不是从1开始(或者转到size+1)。考虑一个大小为1的列表,您的代码将不会执行,这就是为什么链接列表中只剩下1个节点的原因。您的代码也需要重新思考。而不是for循环或者while循环
struct node *tmp;
while (root != NULL)
{
tmp = root;
root = root->next;
root->back = NULL; //Is this even necessary? You're deleting ALL nodes.
delete tmp;
}
display();
至于find函数,您已经将其设置为返回void(nothing)。它正在打印节点值,因为您告诉它,但您没有返回节点。您需要考虑find应该返回什么(节点或null)
我建议您使用快速排序对列表进行排序。因为这是一种特殊情况,所以您必须调整快速排序以按节点->值排序,并且在排序时,您必须确保在交换值时节点指向正确的上一个和下一个节点。当您遇到更多错误时,请继续发布您的代码,并确保您告诉我们您尝试了什么以及什么不起作用,我们很乐意为您指出正确的方向。对于删除,所有For循环都需要从0开始,而不是从1开始(或转到大小+1)。考虑一个大小为1的列表,您的代码将不会执行,这就是为什么链接列表中只剩下1个节点的原因。您的代码也需要重新思考。而不是for循环或者while循环
struct node *tmp;
while (root != NULL)
{
tmp = root;
root = root->next;
root->back = NULL; //Is this even necessary? You're deleting ALL nodes.
delete tmp;
}
display();
至于find函数,您已经将其设置为返回void(nothing)。它正在打印节点值,因为您告诉它,但您没有返回节点。您需要考虑find应该返回什么(节点或null)
我建议您使用快速排序对列表进行排序。因为这是一种特殊情况,所以您必须调整快速排序以按节点->值排序,并且在排序时,您必须确保在交换值时节点指向正确的上一个和下一个节点。当您遇到更多错误时,请继续发布您的代码,并确保您告诉我们您尝试了什么以及什么不起作用,我们很乐意为您指出正确的方向。删除的代码在哪里?删除的代码在哪里?我已尝试在deleteAll()中设置for循环从1开始,转到大小+1,然后是,会抛出一个错误并停止工作。请参阅上面更新的代码。你需要重新思考你的数据以及它的结构。谢谢你对deleteAll()的建议,我对它做了一些改动,现在它工作得很好。向上投票@Blake,你不需要在while循环中使用这个循环。据我所知,对链表进行排序的最快方法是合并排序,因为它是O(logn),并且有一个非常好的常数乘数。Quicksort的乘数要高得多。我已尝试将deleteAll()中的for循环设置为从1开始,然后变为大小+1,但随后is抛出错误并停止工作。请参阅上面我更新的代码。你需要重新思考你的数据以及它的结构。谢谢你对deleteAll()的建议,我对它做了一些改动,现在它工作得很好。向上投票@Blake,你不需要在while循环中使用这个循环。据我所知,对链表进行排序的最快方法是合并排序,因为它是O(logn),并且有一个非常好的常数乘数。快速排序的乘数要高得多。