我正在制作一个已排序的链表,用户输入一个数字,然后将数字插入到排序位置。我目前在排序功能方面遇到了问题。
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;
}
答案 0 :(得分:0)
我对您的代码有关逻辑步骤和/或命名的担忧。我不能只编辑你的代码来修复它,所以如果你同意我,我会根据这种理解重写你的代码:
head
指向第一个节点,tail
指向最后一个节点。head
和tail
指向新节点。此外,我的其他更改包括:
一个。对于课程stack
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;
}
这有帮助吗?