標籤:return 多鏈棧 進棧 出棧
多棧運算的演算法思想:將多個鏈棧的棧頂指標放在一個一維指標數組中來統一管理,從而實現同時管理和使用多個棧。
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/80/04/wKioL1c0jzjBUGYkAAAjsxAXniU553.png" title="圖片1.png" alt="wKioL1c0jzjBUGYkAAAjsxAXniU553.png" />
多鏈棧
實現代碼如下:
#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define M 10
typedef struct node
{
int data;
struct node *next;
}LinkStackNode, *LinkStack;
LinkStack top[M];
//第i號棧進棧操作
int Pushi(LinkStack top[M], int i,int x)//將元素x進入第i號鏈棧
{
LinkStackNode *temp;
temp = (LinkStackNode *)malloc(sizeof(LinkStackNode));
if (temp==NULL)//申請空間失敗
{
return FALSE;
}
temp->data= x;
temp->next = top[i]->next;
top[i]->next = temp;//修改當前棧頂指標
return TRUE;
}
//第i號棧出棧操作
int Pop(LinkStack top[M], int i,int *x)//將第i號棧的棧頂元素彈出,放到x所指的儲存空間中
{
LinkStackNode *temp;
temp = top[i]->next;
if (temp == NULL)//第i號棧為空白棧
{
return FALSE;
}
top[i]->next = temp->next;
*x=temp->data ;
free(temp);//釋放儲存空間
return TRUE;
}
本文出自 “岩梟” 部落格,請務必保留此出處http://yaoyaolx.blog.51cto.com/10732111/1772840
多棧運算