如何插入一个分类的单链接列表中
How to insert into a sorted singly linked list?
我正在制作一个排序的链接列表,其中用户输入一个数字,并且该数字插入到排序位置。我目前在排序功能方面有问题。
class Node
{
public:
int data;
class Node *next;
Node(int info, Node *ptr = 0)
{
data = info;
next = ptr;
}
};
class Node *head = NULL;
class stack
{
private:
Node *temp;
public:
stack()
{
temp = 0;
}
bool isEmpty()
{
return temp == NULL;
}
我决定使用插入功能(它称为Sort,但实际上它只是将一个节点插入链接列表中),这就是为什么我有当前指针和以前的指针。我想我的问题是我走正确的道路吗?我是新手,所以我只想一些指导来解决这个问题。任何技巧都将不胜感激。
void sort(int data)
{
class Node *born = new Node(data);
Node* current = head;
Node* previous = NULL;
//the list is empty case
if (isEmpty())
temp = born;
else
{
while (current != NULL)
{
if (current->data >= temp->data)
{
born->next = current;
temp = born;
break;
}
else
{
previous = current;
current = temp->next;
}
}
if (current == head)
{
born->next = head;
head = born;
}
else
{
born->next = current;
previous->next = born;
}
/*else
{
born->next = temp;
temp = born;
}*/
}
}
void print()
{
cout<<"stack from the top"<<endl;
for(Node *top = temp; top != 0; top=top->next)
cout << top->data << " ";
cout << endl;
/*while(temp != NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
*/
}
int pop()
{
if (isEmpty())
return -999;
int intReturn = temp->data;
Node *top;
top = temp;
temp = temp->next;
delete top;
return intReturn;
}
};
int main(void)
{
int num=0;
stack boss;
while(num!=-1)
{
cout<<"Please Enter a Number"<<endl;
cin >> num;
if (num == -1)
boss.print();
else
boss.sort(num);
}
cout<<endl<<endl;
system("PAUSE");
return 0;
}
我对您对事物的逻辑步骤和/或事物的命名有担忧。我不能只是编辑您的代码来修复它,因此,如果您同意我的意见,我已经根据此理解重写您的代码:
- a
head
指向第一个节点,而tail
指向最后一个节点。 - 检查列表是否为空,然后
head
,tail
指向新节点。 - 否则,检查是否要将新节点放在开始时,然后进行
- 否则,检查一下是否适合中间,然后进行
- 否则,将其放在最后一个地方
此外,我的其他更改包括:
a。对于类stack
- 我将其重命名为linkedlist
- 我将两个成员变量
head
和tail
参考第一个和最后一个节点
b。对于sort()
方法
- 我将其重命名为
insert
- 我明确地清楚地写了每个重要情况(空列表,第一个节点,中间节点,最后一个节点)
我还删除了using namespace std
并明确编写了std::
,因为这是一个更好的做法。这里和那里也有一些小变化。
我的最终代码:
class Node
{
public:
int data;
class Node* next;
Node(int info, Node* ptr = 0)
{
data = info;
next = ptr;
}
};
class LinkedList
{
private:
Node* head;
Node* tail;
public:
LinkedList()
{
head = 0;
tail = 0;
}
bool isEmpty()
{
return head == NULL;
}
void insert(int data)
{
Node* newNode = new Node(data);
if (isEmpty())
{
head = newNode;
tail = newNode;
}
// if node to be placed at the top
else if (data > head->data)
{
newNode->next = head;
head = newNode;
}
else
{
Node* temp = head;
Node* previousTemp = head;
while (temp != NULL)
{
// if node to be placed in the middle
if (temp->data < data)
{
previousTemp->next = newNode;
newNode->next = temp;
break;
}
previousTemp = temp;
temp = temp->next;
}
// if node to be placed at the end
if (temp == NULL)
{
tail->next = newNode;
tail = tail->next;
}
}
}
void print()
{
std::cout << "Stack from the top" << std::endl;
Node* temp = head;
while(temp != NULL)
{
std::cout << temp->data << " ";
temp = temp->next;
}
}
int pop()
{
if (isEmpty())
{
return -999;
}
int intReturn = head->data;
Node *top;
top = head;
head = head->next;
delete top;
return intReturn;
}
};
int main(void)
{
int num = 0;
LinkedList linkedList;
while (num != -1)
{
std::cout << "Please enter a number: ";
std::cin >> num;
if (num == -1)
{
linkedList.print();
}
else
{
linkedList.insert(num);
}
}
std::cout << std::endl << std::endl;
system("PAUSE");
return 0;
}
这有帮助吗?
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- CMake-按正确顺序将项目与C运行时对象文件链接
- 从链接列表c++中删除一个项目
- 有根的二进制搜索树.保留与其父级的链接
- 读取文件的最后一行并输入到链接列表时出错
- 静态数据成员的问题-修复链接错误会导致编译器错误
- node-gyp 在 macOS 上未正确链接库
- 基于boost的程序的静态链接——zlib问题
- 无法链接 CMake 中的本地库
- 内联函数中具有内部链接的全局变量
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 使用gcc从静态链接的文件中查找可选符号
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 在链接链接静态(GSOAP)库的共享库时,为什么会得到一个未定义的符号
- 未定义的参考,但链接链接
- 链接链接器/加载器错误时"undefined reference to ..."与拉斯皮康库链接时
- 将Typedef语句放入类链接链接列表中的C 错误
- 对嵌套结构单链接和双链接进行排序
- 如何制作一个函数,只需更改链接即可交换单链表中的两个相邻元素