#define maxSize 100
栈的结构体定义
顺序栈的结构体定义
typedef struct
{
int data[maxSize];
int top;
} SqStactk;
链栈结点的结构体定义
typedef struct LNode
{
int data;
struct LNode *next;
} LNode;
队列的结构体定义
顺序队列的结构体定义
typedef struct
{
int data[maxSize];
int front;
int rear;
} SqQueue;
链队的结构体定义
链队结点的结构体定义
typedef struct QNode
{
int data;
struct QNode *next;
} QNode;
链队的结构体定义
typedef struct
{
QNode *front;
QNode *rear;
} LiQueue;
顺序栈
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct
{
int data[maxSize];
int top;
} SqStactk;
void initStack(SqStactk &st)
{
st.top = -1;
}
int isEmpty(SqStactk st)
{
if (st.top == -1)
return 1;
else
return 0;
}
int push(SqStactk &st, int x)
{
if (st.top == maxSize - 1)
return 0;
++(st.top);
st.data[st.top] = x;
return 1;
}
int pop(SqStactk &st, int &x)
{
if (st.top == -1)
return 0;
x = st.data[st.top];
--(st.top);
return 1;
}
int main()
{
SqStactk st;
int x;
initStack(st);
cout << isEmpty(st) << endl;
push(st, 2);
pop(st, x);
cout << x;
return 0;
}
int stack[maxSize];
int top = -1;
stack[++top] = x;
x=stack[top--];
链栈
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct LNode
{
int data;
struct LNode *next;
} LNode;
void initStack(LNode *&lst)
{
lst = (LNode *)malloc(sizeof(LNode));
lst->next = NULL;
}
int isEempty(LNode *lst)
{
if (lst->next == NULL)
return 1;
else
return 0;
}
void push(LNode *lst, int x)
{
LNode *p;
p = (LNode *)malloc(sizeof(LNode));
p->next = NULL;
p->data = x;
p->next = lst->next;
lst->next = p;
}
int pop(LNode *lst, int &x)
{
LNode *p;
if (lst->next == NULL)
return 0;
p = lst->next;
x = p->data;
lst->next = p->next;
free(p);
return 1;
}
int main()
{
LNode *lst;
int x;
initStack(lst);
cout << isEempty(lst) << endl;
push(lst, 2);
pop(lst, x);
cout << x;
return 0;
}
栈的应用
顺序栈的应用
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
int match(char exp[], int n)
{
char stack[maxSize];
int top = -1;
int i;
for (i = 0; i < n; i++)
{
if (exp[i] == '(')
stack[++top] = '(';
if (exp[i] == ')')
{
if (top == -1)
return 0;
else
--top;
}
}
if (top == -1)
return 1;
else
return 0;
}
int main()
{
char exp[] = "()()()()";
cout << match(exp, 8);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
int op(int a, char Op, int b)
{
if (Op == '+') return a + b;
if (Op == '-') return a - b;
if (Op == '*') return a * b;
if (Op == '/')
{
if (b == 0)
{
cout << "ERROR" << endl;
return 0;
}
else
return a / b;
}
return 1;
}
int com(char exp[])
{
int i, a, b, c;
int stack[maxSize];
int top = -1;
char Op;
for (i = 0; exp[i] != '\0'; ++i)
{
if (exp[i] >= '0' && exp[i] <= '9')
stack[++top] = exp[i] - '0';
else
{
Op = exp[i];
b = stack[top--];
a = stack[top--];
c = op(a, Op, b);
stack[++top] = c;
}
}
return stack[top];
}
int main()
{
char exp[] = "1234*++5/";
cout << com(exp);
return 0;
}
链栈的应用
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct LNode
{
int data;
struct LNode *next;
} LNode;
void initStack(LNode *&lst)
{
lst = NULL;
}
int isEempty(LNode *lst)
{
if (lst == NULL)
return 1;
else
return 0;
}
void push(LNode *&lst, int x)
{
LNode *p;
p = (LNode *)malloc(sizeof(LNode));
p->next = NULL;
p->data = x;
p->next = lst;
lst = p;
}
int pop(LNode *&lst, int &x)
{
LNode *p;
if (lst == NULL)
return 0;
p = lst;
x = p->data;
lst = p->next;
free(p);
return 1;
}
int main()
{
LNode *lst;
int x;
initStack(lst);
cout << isEempty(lst) << endl;
push(lst, 2);
pop(lst, x);
cout << x;
return 0;
}
顺序队(循环队列)
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct
{
int data[maxSize];
int front;
int rear;
} SqQueue;
void initQueue(SqQueue &qu)
{
qu.front = qu.rear = 0;
}
int isQueueEmpty(SqQueue qu)
{
if (qu.front == qu.rear)
return 1;
else
return 0;
}
int enQueue(SqQueue &qu, int x)
{
if ((qu.rear + 1) % maxSize == qu.front)
return 0;
qu.rear = (qu.rear + 1) % maxSize;
qu.data[qu.rear] = x;
return 1;
}
int deQueue(SqQueue &qu, int &x)
{
if (qu.front == qu.rear)
return 0;
qu.front = (qu.front + 1) % maxSize;
x = qu.data[qu.front];
return 1;
}
int main()
{
SqQueue qu;
int x;
initQueue(qu);
cout << isQueueEmpty(qu) << endl;
enQueue(qu, 2);
deQueue(qu, x);
cout << x;
return 0;
}
链队
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct QNode
{
int data;
struct QNode *next;
} QNode;
typedef struct
{
QNode *front;
QNode *rear;
} LiQueue;
void initQueue(LiQueue *&lqu)
{
lqu = (LiQueue *)malloc(sizeof(LiQueue));
lqu->front = lqu->rear = NULL;
}
int isQueueEmpty(LiQueue *lqu)
{
if (lqu->rear == NULL || lqu->front == NULL)
return 1;
else
return 0;
}
void enQueue(LiQueue *lqu, int x)
{
QNode *p;
p = (QNode *)malloc(sizeof(QNode));
p->data = x;
p->next = NULL;
if (lqu->rear == NULL)
lqu->front = lqu->rear = p;
else
{
lqu->rear->next = p;
lqu->rear = p;
}
}
int deQueue(LiQueue *lqu, int &x)
{
QNode *p;
if (lqu->rear == NULL)
return 0;
else
p = lqu->front;
if (lqu->front == lqu->rear)
lqu->front = lqu->rear = NULL;
else
lqu->front = lqu->front->next;
x = p->data;
free(p);
return 1;
}
int main()
{
LiQueue *lqu;
int x;
initQueue(lqu);
cout << isQueueEmpty(lqu) << endl;
enQueue(lqu, 5);
deQueue(lqu, x);
cout << x;
return 0;
}
共享栈和双端队列