链栈的结构体代码
创建链栈
typedef struct node
{
int data;
struct node*nextPtr;
} Stack,*LinkStack;
压入元素
void PushStack(LinkStack &top,int e)
{
LinkStack p=(LinkStack)malloc(sizeof(Stack));
if(p==NULL)
printf("There is an error.\n");
p->data=e;
p->nextPtr=top;
top=p;
}
弹出栈
int PopStack(LinkStack &top)
{
LinkStack p=NULL;
int e;
if(!top)
return 0;
e=top->data;
p=top;
top=top->nextPtr;
free(p);
return e;
}
链队列的结构体代码
typedef struct node_2
{
char data;
struct node_2* next;
}Node;
typedef struct Queue
{
Node* front;
Node* rear;
}Queue;
队列初始化
Queue* QueueInit()
{
Queue*q=(Queue*)malloc(sizeof(Queue));
q->front = NULL;
q->rear = NULL;
return q;
}
入队
void QueuePush(Queue* &q, char data)
{
Node * p = (Node *)malloc(sizeof(Node));
if(p == NULL)
printf("ERRO\n");
else
{
p->data = data;
p->next = NULL;
if(q->rear == NULL)
{
q->front = p;
q->rear = p;
}
else
{
q->rear->next = p;
q->rear= p;
}
}
}
出队
char QueuePop(Queue* &q)
{
Node * p = q->front;
char d;
if(q->front)
d=p->data;
q->front = q->front->next;
free(p);
return d;
}