定义一个新节点,然后将新节点赋值给队列的头。
出队Node * Delete(Node *S)
{
if (S->front != S->rear)
{
S->front = S->front->next;
return S;
}
else
{
cout << "队列为空";
return 0;
}
}
即将队列尾指向队列尾下一个:即 S->front = S->front->next;
显示队列头void Show(Node *S)
{
cout << "队头元素为:";
cout << S->front->next->data << endl;
}
因为链表的头指向的是空,所以显示出头的下一个元素,即
S->front->next->data
第一种代码实现#include<iostream>
using namespace std;
#define MaxSize 20
struct Node
{
int data; //数据域
Node *front; //尾
Node *rear; //头
Node *next; //下一个
};
Node *Init()
{
Node *S = new Node;
S->front = S->rear=new Node;
S->front->next = NULL;
return S;
}
Node *Enter(Node *S)
{
Node *P = new Node;
cout << "输入将要入队列的值:";
cin>>P->data;
P->next = NULL;
S->rear->next = P;
S->rear = P;
return S;
}
Node * Delete(Node *S)
{
if (S->front != S->rear)
{
S->front = S->front->next;
return S;
}
else
{
cout << "队列为空";
return 0;
}
}
void Show(Node *S)
{
cout << "队头元素为:";
cout << S->front->next->data << endl;
}
int main()
{
Node *S = Init();
S=Enter(S);
S=Enter(S);
S=Enter(S);
Show(S);
S=Delete(S);
Show(S);
system("pause");
}
#include<iostream>
using namespace std;
#define MaxSize 20
struct Node
{
int data; //数据域
Node *next; //下一个
};
struct List
{
Node *front; //尾
Node *rear; //头
};
List *Init()
{
List *S = new List;
S->front = S->rear=new Node;
S->front->next = NULL;
return S;
}
List *Enter(List *S)
{
Node *P = new Node;
cout << "输入将要入队列的值:";
cin>>P->data;
P->next = NULL;
S->rear->next = P;
S->rear = P;
return S;
}
List* Delete(List *S)
{
if (S->front != S->rear)
{
S->front = S->front->next;
return S;
}
else
{
cout << "队列为空";
return 0;
}
}
void Show(List *S)
{
cout << "队头元素为:";
cout << S->front->next->data << endl;
}
int main()
{
List *S = Init();
S=Enter(S);
S=Enter(S);
S=Enter(S);
Show(S);
S=Delete(S);
Show(S);
system("pause");
}