队列是现实生活中经常用到的一种数据结构,因为它遵循“先到先得”的规律,这种认知符合人类公平公正的思想观,比如排队打饭,银行业务挂号,医院挂号.....
1.入队.
你想想啊,排队打饭肯定是先排先打,后排等着最后,所以,肯定是先进先出,新结点加到队列的最后。
2.出队.
肯定是出头啊,所以每次都是把队列的第一个数据结点出队列。
#include<iostream>
#include<malloc.h>using namespace std;
struct Lines{
int order;
char data;
Lines* next;
};
void Menu();
Lines* newNode();
bool is_Empty(Lines* head);
void enter(Lines* head);
void out(Lines* head);
void Show(Lines* head);
int main(int argc,char** argv){
Lines* head = newNode();
int select;
while(true){
Menu();
cin >> select;
switch(select){
case 1:
enter(head);
break;
case 2:
out(head);
break;
case 3:
Show(head);
break;
case 0:
exit(1);
break;
default:
cout << "输入无效,请重新输入" << endl;
break;
}
}
}
void Menu(){
system("cls");
cout << "1.入列" << endl;
cout << "2.出列" << endl;
cout << "3.遍厉列" << endl;
cout << "0.退出" << endl;
cout << endl << "请输入功能代叫进行下一步操作" << endl;
}
void Show(Lines* head){
system("cls");
while(head->next != NULL){
cout << head->next->order << " " << head->next->data << endl;
head = head->next;
}
system("pause");
}
Lines* newNode(){
Lines* node = NULL;
node = (Lines*)malloc(sizeof(Lines));
node->next = NULL;
node->data = '0';
node->order = 0;
return node;
}
void enter(Lines* head){
system("cls");
Lines* node = newNode();
cout << "请输入您的编号" << endl;
cin >> node->order;
cout << "请输入您的等级" << endl;
cin >> node->data;
while(head->next != NULL){
head = head->next;
}
head->next = node;
cout << "进入队列成功" << endl;
system("pause");
}
void out(Lines* head){
system("cls");
Lines* node = newNode();
if(!is_Empty(head)){
node->next = head->next;
head->next = head->next->next;
free(node->next);
free(node);
cout << "出队列成功" << endl;
}else{
cout << "队列内不存在任何元素" << endl;
}
system("pause");
}
bool is_Empty(Lines* head){
if(head->next == NULL){
return true;
}else{
return false;
}
}