1. 程式人生 > >使用棧的鏈式表示,實現括號匹配檢測。

使用棧的鏈式表示,實現括號匹配檢測。

清華出版《資料結構》3.2.2

棧的鏈式表示.h
#include <stdlib.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Node
{
	int data;
	struct Node *pNext;
} NODE, *PNODE;

typedef struct Stack
{
	PNODE pTop;
	PNODE pBottom;
} STACK, *PSTACK;

PSTACK creat_stack()
{
	PSTACK pStack = (PSTACK)malloc(sizeof(STACK));
	pStack->pBottom = (PNODE)malloc(sizeof(NODE));
	if(pStack->pBottom == NULL || pStack == NULL)
	{
		printf("malloc failed");
		exit(-1);
	}
	pStack->pTop = pStack->pBottom;
	return pStack;
}

bool is_empty(PSTACK pStack)
{
	bool result = false;
	if(pStack->pBottom == pStack->pTop)
	{
		result = true;
	}
	else
	{
		result = false;
	}
	return result;
}

int push(PSTACK pStack, int a)
{
	PNODE pNode = (PNODE)malloc(sizeof(NODE));
	if(pNode == NULL)
	{
		exit(-1);
	}
	pNode->data = a;
	pNode->pNext = pStack->pTop;
	pStack->pTop =pNode;
	return 1;
}

bool pop(PSTACK pStack, int* pData)
{
	if(is_empty(pStack))
	{
		return false;
	}
	else
	{
		PNODE p = pStack->pTop;
		*pData = p->data;
		pStack->pTop = p->pNext;
		free(p);
		p = NULL;
		return true;
	}
}

void traverse_stack(PSTACK pStack)
{
	PNODE current = pStack->pTop;
	printf("Now data int the stack are:\n");
	while(current != pStack->pBottom)
	{
		printf("%d ", current->data);
		current = current-> pNext;
	}
	printf("\n");
}

void clear_stack(PSTACK pStack)
{
	//isEmpty()
	PNODE p = NULL;
	while(pStack->pTop != pStack->pBottom)
	{
		p = pStack->pTop;
		pStack->pTop = p -> pNext;
		free(p);
		p = NULL;
	}
}
main.c
#include<stdlib.h>
#include<stdio.h>
#include"棧的鏈式表示.h"

enum bracket{left_little, right_little, left_middle, right_middle};

int main()
{
	PSTACK pStack = creat_stack();
	printf("請輸入一串括號,用於檢驗是否合法(按回車鍵結束):\n");
	char input = '\0';
	int temp = 0;
	do
	{
		scanf("%c", &input);
		switch(input)
		{
            case '(':
                push(pStack, left_little);
                break;
            case '[':
                push(pStack, left_middle);
                break;
            case ')':
                if(pStack->pTop->data == left_little)
                {
                    pop(pStack, &temp);
                }
                else if(pStack->pTop->data == left_middle)
                {
                    printf("不合法!");
                    return 0;
                }
                break;
            case ']':
                 if(pStack->pTop->data == left_middle)
                {
                    pop(pStack, &temp);
                }
                else if(pStack->pTop->data == left_little)
                {
                    printf("不合法!");
                    return 0;
                }
                break;
            default:
                 if(input != '\n')
                {
                     printf("請不要輸入()[]以外的其它字元");
                     return 0;
                }
                break;
		}

	}
	while(input != '\n');
    if(is_empty(pStack))
    {
        printf("合法!");
    }
    else
    {
        printf("不合法!");
    }
}