using namespace std;
void createsqueue();
void insertname();
void insertheight();
void del();
void push();
void display();
struct nodename
{
string name;
float height;
struct nodename *next;
};
nodename *front;
nodename *rear;
void createstack()
{
front = NULL;
rear = NULL;
}
void insertname()
{
while (true)
{
char dec;
nodename *temp;
temp = new nodename;
std::cout << "ENTER YOUR NAME : ";
std::cin >> temp -> name;
std::cout << "ENTER YOUR HEIGHT : ";
std::cin >> temp -> height;
std::cout <<'n';
temp -> next = NULL;
if(rear == NULL)
{
rear = temp;
front = temp;
}
else
{
rear -> next = temp;
rear = temp;
}
std::cout << "ADD ANOTHER DATA? (Y/N) : ";
std::cin >> dec;
std::cout <<'n';
if (dec == 'n' || dec == 'N')
{
break;
}
}
}
void del()
{
if(front != NULL)
{
nodename *temp = front;
cout << "The deleted element is: " << temp -> name << endl;
front = front -> next;
delete temp;
}
else
{
cout << "Queue List is empty!n";
}
}
void display()
{
nodename *temp = front;
while(temp != NULL)
{
std::cout << "--------------------------" << 'n';
std::cout <<"NAME : "<< temp -> name << endl;
std::cout << showpoint << fixed << setprecision(0);
std::cout <<"HEIGHT : " << temp -> height << endl;
std::cout << showpoint << fixed << setprecision(2);
temp = temp -> next;
std::cout << "--------------------------" << 'n';
}
}
int main()
{
int operation;
createstack();
do
{
std::cout << "tMAIN MENU" << endl;
std::cout << "1 - ENTER NAME : "
<< "n2 - DELETE PREV DATA : "
<< "n3 - DISPLAY DATA : "
<< "n0 - End Program"
<< "nEnter your operation: ";
cin >> operation;
switch (operation)
{
case 1: insertname();
break;
case 2: del();
break;
case 3: display();
break;
case 0: cout << "Program End";
break;
default:
cout << "Wrong option. Please insert a new operation: ";
}
}
while(operation ! = 0);
return 0;
}
这是代码。 该程序运行良好,我遇到的唯一问题是按降序显示带有高度的名称,以便首先显示最高人的信息 我已经尝试了多种方法,但似乎没有任何效果,这可能是我的编码经验,因为我是新手,请原谅我刚刚开始的任何错误
修改用于插入新名称的代码,以便无论何时添加节点,都可以在插入链表之前找到它的正确位置。
void insertname(){
cin>>h;
nodename* ctr=front;
while(ctr-> height >h){
ctr=ctr->next;
}
nodename* temp =new nodename;
temp->next=ctr->next
ctr->next=temp
temp->height=h; //similarly for name
}
如果添加的节点是第一个节点或您实现的任何其他节点,只需找到它,并且上述节点将按降序添加节点,这些节点将根据需要显示。因此,您可以相应地修改它。