选用一种存储结构建立栈、队列
编写一个程序,读入以升序排列的一系列整数,然后以降序打印出这些整数。一旦读到的整数不大于前面的数,则输入终止,然后将这些整数以降序打印输出。
求大神解答相关代码
不是要解答代码么?那你的代码在哪?
typedef struct Node
{
int nData;
Node *pNext;
}Node;
typedef struct Queue
{
Node *pHead;
Node *pTail;
}Queue;
void Init(Queue *pQueue)
{
pQueue->pHead = NULL;
pQueue->pTail = NULL;
}
void InsertData(Queue *pQueue,int nData)
{
Node *pNew = NULL;
pNew = (Node *)malloc(sizeof(Queue));
if (pNew == NULL)
{
cout <<"分配空间出错!\n";
return;
}
pNew->nData = nData;
pNew->pNext = NULL;
if (pQueue->pHead == NULL)
{
pQueue->pHead = pNew;
pQueue->pTail = pNew;
}
else
{
pQueue->pTail->pNext = pNew;
pQueue->pTail = pNew;
}
}
int OutData(Queue *pQueue)
{
int nTemp;
Node *pNew = NULL;
pNew = (Node *)malloc(sizeof(Queue));
if (pQueue->pHead == NULL)
{
return 0;
}
nTemp = pQueue->pHead->nData;
pNew = pQueue->pHead;
pQueue->pHead = pQueue->pHead->pNext;
if (pQueue->pHead == NULL)
{
pQueue->pTail = NULL;
}
free(pNew);
return nTemp;
}
void Clear(Queue *pQueue)
{
Node *pNew = pQueue->pHead;
while (pNew != NULL)
{
pQueue->pHead = pQueue->pHead->pNext;
free(pNew);
pNew = pQueue->pHead;
}
pQueue->pTail = NULL;
}
int main()
{
Queue nQueue;
int nData,nTemp = 0;
Init(&nQueue);
cout <<"输入升序的整数队列,初始值是0.\n";
while (1)
{
cin>>nData;
if (nData < nTemp)
{
break;
}
InsertData(&nQueue,nData);
nTemp = nData;
}
while(1)
{
if (nQueue.pHead == NULL)
{
break;
}
else
{
cout <<OutData(&nQueue)<<" ";
}
}
Clear(&nQueue);
return 0;
}
可以参考的自写代码。。_降序输出留给你自己写吧。。_
书上都有啊,你没有?