栈与队列的实现
栈的实现
函数的声明与头文件的引用
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int stackdatatype;
typedef struct Stack
{
stackdatatype* a;
int size;
int capacity;
}SK;
void IsFull(SK* head);
void Init(SK* head);
void Print(SK* head);
void Push(SK* head,stackdatatype x);
void Pop(SK* head);
int Size(SK* head);
void Destory(SK* head);
函数的实现
#define _CRT_SECURE_NO_WARNINGS
#include"stack.h"
void IsFull(SK* head)
{
assert(head);
if (head->size == head->capacity)
{
stackdatatype* newhead = (stackdatatype*)realloc(head->a,sizeof(stackdatatype)*10);
if (newhead == NULL)
{
perror("realloc failed");
exit(-1);
}
head->capacity += 10;
head->a = newhead;
}
}
void Init(SK* head)
{
assert(head);
head->a= (stackdatatype*)malloc((sizeof(stackdatatype) * 4));
if (head->a== NULL)
{
perror("malloc failed");
exit(-1);
}
head->size = 0;
head->capacity = 4;
}
void Print(SK* head)
{
for (int i = 0; i < head->size; i++)
{
printf("%d ", head->a[i]);
}
printf("\n");
}
void Push(SK* head,stackdatatype x)
{
IsFull(head);
head->a[head->size] = x;
head->size++;
}
void Pop(SK* head)
{
assert(head);
assert(head->size);
head->size--;
}
int Size(SK* head)
{
assert(head);
return head->size;
}
bool SKEmpty(SK* head)
{
assert(head);
return head->size == 0;
}
void Destory(SK* head)
{
head->size = 0;
head->capacity = 0;
free(head->a);
head->a = NULL;
}
队列的实现
函数声明与头文件的引用
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int Queuedatatype;
typedef struct QueueNode
{
Queuedatatype x;
struct QueueNode* next;
}QN;
//typedef struct headtail
//{
// QN* head;
// QN* tail;
//}HT;
void QueueInit(QN** head);
void QueueDestroy(QN* head);
void QueuePrint(QN* head);
QN* CreateNode(Queuedatatype x);
void QueuePush(QN** head, Queuedatatype x);
void QueuePop(QN** head);
函数实现
#define _CRT_SECURE_NO_WARNINGS
#include"queue.h"
void QueueInit(QN** head)
{
*head = NULL;
}
void QueueDestroy(QN* head)
{
assert(head);
QN* cur = head;
while (cur->next != NULL)
{
QN* freenode = cur;
cur = cur->next;
free(freenode);
freenode = NULL;
}
}
QN* CreateNode(Queuedatatype x)
{
QN* newnode = (QN*)malloc(sizeof(QN));
if (newnode==NULL)
{
perror("malloc failed");
exit(-1);
}
newnode->next = NULL;
newnode->x = x;
return newnode;
}
void QueuePrint(QN* head)
{
assert(head);
QN* cur = head;
while (cur!= NULL)
{
printf("%d", cur->x);
cur = cur->next;
}
printf("\n");
}
void QueuePush(QN** head, Queuedatatype x)
{
QN* cur = *head;
QN* newnode = CreateNode(x);
if (*head == NULL)
*head = newnode;
else
{
while (cur->next!= NULL)
{
cur = cur->next;
}
cur->next = newnode;
}
}
void QueuePop(QN** head)
{
assert(*head);
if ((*head)->next == NULL)
{
free(*head);
*head = NULL;
}
else
{
QN* cur = (*head)->next;
free(*head);
*head = cur;
}
}
栈和队列测试
#define _CRT_SECURE_NO_WARNINGS
#include"stack.h"
#include"queue.h"
void test()
{
SK x;
Init(&x);
Push(&x, 1);
Push(&x, 2);
Push(&x, 3);
Push(&x, 4);
Push(&x, 5);
Print(&x);
Pop(&x);
Print(&x);
Destory(&x);
}
void test2()
{
QN* qn;
QueueInit(&qn);
QueuePush(&qn, 1);
QueuePush(&qn, 2);
QueuePush(&qn, 3);
QueuePush(&qn, 4);
QueuePush(&qn, 5);
QueuePrint(qn);
QueuePop(&qn);
QueuePop(&qn);
QueuePop(&qn);
QueuePop(&qn);
QueuePrint(qn);
QueueDestroy(qn);
}
int main()
{
test();
test2();
}
oj
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
每个右括号都有一个对应的相同类型的左括号。
方法:判断s,是左括号就入栈,是右括号就弹出栈顶元素比较(s一开始就是右括号先判断栈内是否有元素,没有就返回false),不匹配返回false,直到字符串结束,再判断栈内元素是否全部出栈,有未出栈的则说明有不匹配的括号,返回false
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void STPush(ST* ps, STDataType x)
{
assert(ps);
// 11:40
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void STPop(ST* ps)
{
assert(ps);
//
assert(ps->top > 0);
--ps->top;
}
STDataType STTop(ST* ps)
{
assert(ps);
//
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
bool isValid(char * s){
ST st;
STInit(&st);
char topval;
while(*s)
{
if(*s=='(' || *s=='{'||*s=='[')
{
STPush(&st,*s);
}
else
{
if(STEmpty(&st))
{
STDestroy(&st);
return false;
}
else
{
topval=STTop(&st);
STPop(&st);
if(*s==')'&&topval!='('
|| *s=='}'&&topval!='{'
|| *s==']'&&topval!='[')
{
STDestroy(&st);
return false;
}
}
}
*s++;
}
bool ret =STEmpty(&st);
STDestroy(&st);
return ret;
}