#include <iostream>
#include <string>
using namespace std;
struct node
{
int num;
node*next;
};
bool isEmpty(node *head);
char menu();
void insertasfirstelement(node *&head, node *&last, int num);
void insert(node *&head, node *&last, int num);
void remove(node *&head, node *&last);
void showlist(node*c);
bool isEmpty(node*head)
{
if(head == NULL)
return true;
else
return false;
}
char menu()
{
char choice;
cout << "\n\nMenu:\n";
cout << "\n1. Add an item";
cout << "\n2. Remove an item";
cout << "\n3. Show the list";
cout << "\n4. Exit" <<endl;
cin >> choice;
return choice;
}
void insertasfirstelement(node *&head, node*&last, int num)
{
node * temp = new node;
temp ->num=num;
temp ->next=NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, int num)
{
if(isEmpty(head))
insertasfirstelement(head,last,num);
else
{
node *temp = new node;
temp ->num=num;
temp ->next=NULL;
last ->next= temp;
last = temp;
}
}
void remove(node *&head, node *&last)
{
if(isEmpty(head))
cout << "List is empty\n";
else if(head == last)
{
delete head;
head = NULL;
last = NULL;
}
else
{
node *temp = head;
head = head -> next;
delete temp;
}
}
void showlist(node*c)
{
if(isEmpty(c))
cout <<"The list is empty\n";
else
{
cout << "The values are: \n";
while(c !=NULL)
{
cout << c -> num << endl;
c = c -> next;
}
}
}
int main()
{
node *head=NULL;
node *last=NULL;
char choice;
int num;
do{
choice = menu();
switch(choice)
{
case '1': cout << "Please enter a number: ";
cin >> num;
insert(head, last, num);
break;
case '2': remove(head,last);
break;
case '3': showlist(head);``
break;
default: cout << "System exit\n";
}
} while(choice != '4');
}
所以我能够得到一份工作链表。但我无法弄清楚如何按顺序完成它。我也不知道怎么做,所以我可以删除我插入的数字。我试图了解链接列表如何更好地工作,所以如果你能提供一些帮助,我将不胜感激。
答案 0 :(得分:0)
您可以通过比较节点的数据字段来制作链接列表
尝试这种方式:
1 - 直到节点通过链表清空。
2 - 如果当前节点的数据小于下一个节点的数据,则交换这两个数据。
3 - 否则转到下一个节点
如果您想在不交换数据的情况下订购链表... 即改变下一个节点然后你也可以这样做..
删除节点
试试这种方式
1-要求用户输入他/她想要删除的节点的数据。
2-搜索具有该数据的节点。
3-如果找到该节点则删除该节点。
4-否则在剩余链表中找到节点。
注意:这就像怎么做...你必须构建逻辑,以便在链表中保持一致性:)
答案 1 :(得分:0)
以下两个函数都假设您希望链接列表按升序排序。
要将新节点插入链接列表对象,您可以尝试:
void insert(node *&head, node *&last, int num)
{
if(isEmpty(head))
insertasfirstelement(head,last,num);
else if(num > tail->num) //if new number to be inserted is greater than tail, insert at end and avoid needless iteration through list
{
node* temp = new node;
temp->num = num;
temp-next = NULL;
last->next = temp;
last = temp;
}
else
{
node *temp = new node;
temp ->num=num;
temp ->next=NULL;
node* ptr = head; //to lead iteration through linked list
node* prev = NULL; //to trail ptr
while(ptr && (ptr->num > temp->num)) //iterate through linked list while new node's value is less than ptr's value and ptr is not beyond end of list (assuming last->next == NULL)
{
prev = ptr;
ptr = ptr->next;
}
if(!prev) // if prev never iterated, the temp node is the new head node
{
head = temp;
head->next = ptr;
}
else if(prev == last) //if prev is tail, the temp node is the new tail node
{
prev->next = temp;
tail = temp;
}
else //otherwise, insert temp node in between prev and ptr
{
prev-next = temp;
temp->next = ptr;
}
}
}
要从链接列表对象中删除节点,请尝试:
void remove(node *&head, node *&last, int x) // added a int parameter to determine which node is to be removed
{
if(isEmpty(head))
cout << "List is empty\n";
else
{
node* ptr = head;
node* prev = NULL;
while(ptr && (ptr->num != x) //iterate through linked list while desired value to be deleted is not found and have not gone through entire list
{
prev = ptr;
prt = ptr->next;
}
if(!prev) //if prev never iterated, the head is the value to be deleted
{
node* garbage = head;
head = head->next;
delete garbage;
}
else if(ptr==tail) //if the value to be deleted is the tail's value, reassign tail to prev and delete ptr
{
tail = prev;
tail->next = NULL;
delete ptr;
}
else if(!ptr) //if ptr == NULL, value to be deleted was not in list
{
cout << "Value " << x << " could not be found in list.\n"; // or throw exception
}
else //reassign prev's next to ptr's next and delete ptr;
{
prev-next = ptr->next;
delete ptr;
}
}
}