####一 、栈的特点:
四个字总结: 先进后出
####二、栈的应用 :
(1)符号匹配问题:
给出算法,判断括号是否成对匹配,若是,则返回 :成功,若否,则返回匹配失败;
(2)算法思路:
2.1,从第一个字符开始扫描,当遇到普通字符时,则忽略;
2.2,当遇到左符号时,压入栈中
2.3,当遇到右符号时从栈中弹出栈顶符号,并进行匹配
匹配成功:继续读入下一个字符;
匹配失败:立即停止,并报错;
2.4,结束后:
成功:所有字符扫描完毕,且栈为空
失败:匹配失败或所有字符扫描完毕,但栈非空;
(3)实现算法:
//linklist.h
#ifndef _MYLINKLIST_H_
#define _MYLINKLIST_H_
typedef void LinkList;
/*
typedef struct _tag_LinkListNode LinkListNode;
struct _tag_LinkListNode
{
LinkListNode* next;
};
*/
typedef struct _tag_LinkListNode
{
struct _tag_LinkListNode* next;
}LinkListNode;
LinkList* LinkList_Create();
void LinkList_Destroy(LinkList* list);
void LinkList_Clear(LinkList* list);
int LinkList_Length(LinkList* list);
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);
LinkListNode* LinkList_Get(LinkList* list, int pos);
LinkListNode* LinkList_Delete(LinkList* list, int pos);
#endif
//linklist.c
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "linklist.h"
typedef struct _tag_LinkList
{
LinkListNode header;
int length;
}TLinkList;
LinkList* LinkList_Create()
{
TLinkList *tList = (TLinkList *)malloc(sizeof(TLinkList));
if (tList == NULL)
{
return NULL;
}
tList->header.next = NULL;
tList->length = 0;
return tList;
}
void LinkList_Destroy(LinkList* list)
{
if (list != NULL)
{
free(list);
}
return ;
}
void LinkList_Clear(LinkList* list)
{
TLinkList *tList = list;
if (tList == NULL)
{
return ;
}
tList->length = 0;
tList->header.next = NULL;
return ;
}
int LinkList_Length(LinkList* list)
{
TLinkList *tList = list;
if (tList == NULL)
{
return -1;
}
return tList->length;
}
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos)
{
int i = 0;
TLinkList *tList = (TLinkList *)list;
LinkListNode *current = NULL;
if (tList == NULL || node == NULL || pos<0)
{
return -1;
}
current = &tList->header;
//current = (LinkListNode *)list;
for (i=0; (i<pos)&¤t->next!=NULL; i++ )
{
current = current->next;
}
//新节点链接后续链表
node->next = current->next;
//前面链表链接node
current->next = node;
tList->length ++;
return 0;
}
LinkListNode* LinkList_Get(LinkList* list, int pos)
{
int i = 0;
TLinkList *tList = (TLinkList *)list;
LinkListNode *current = NULL;
LinkListNode *ret = NULL;
if (list==NULL || pos<0 || pos>=tList->length)
{
return NULL;
}
current = &tList->header;
for (i=0; i<pos; i++)
{
current = current->next;
}
ret = current->next;
return ret;
}
LinkListNode* LinkList_Delete(LinkList* list, int pos)
{
int i = 0;
TLinkList *tList = (TLinkList *)list;
LinkListNode *current = NULL;
LinkListNode *ret = NULL;
if (list==NULL || pos<0 || pos>=tList->length)
{
return NULL;
}
//没有初始化环境
current = &tList->header;
for (i=0; i<pos; i++)
{
current = current->next;
}
ret = current->next;
current->next = ret->next;
tList->length --;
return ret;
}
//liststack.h
#ifndef _MY_LINKSTACK_H_
#define _MY_LINKSTACK_H_
typedef void LinkStack;
LinkStack* LinkStack_Create();
void LinkStack_Destroy(LinkStack* stack);
void LinkStack_Clear(LinkStack* stack);
int LinkStack_Push(LinkStack* stack, void* item);
void* LinkStack_Pop(LinkStack* stack);
void* LinkStack_Top(LinkStack* stack);
int LinkStack_Size(LinkStack* stack);
#endif //_MY_LINKSTACK_H_
linkstack.c
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "linkstack.h"
#include "linklist.h"
typedef struct _tag_linkStackNode
{
LinkListNode node;
void *item;
}TlinkStackNode;
LinkStack* LinkStack_Create()
{
return LinkList_Create();;
}
void LinkStack_Destroy(LinkStack* stack)
{
return ;
}
void LinkStack_Clear(LinkStack* stack)
{
return ;
}
//向stack中push数据,相当于向linklist中插入数据
int LinkStack_Push(LinkStack* stack, void* item)
{
int ret = 0;
TlinkStackNode *t1 =(TlinkStackNode *) malloc(sizeof(TlinkStackNode));
t1->item = item;
ret = LinkList_Insert(stack, (LinkListNode*)t1, 0);
if (ret != 0)
{
free(t1); //注意内存泄露
}
return ret;
}
void* LinkStack_Pop(LinkStack* stack)
{
void *myItem = NULL;
TlinkStackNode *node = NULL;
if (stack == NULL)
{
return NULL;
}
node = (TlinkStackNode *)LinkList_Delete(stack, 0);
if (node == NULL)
{
return NULL;
}
myItem = node->item;
free(node); //注意释放内存不要忘记
return myItem;
}
void* LinkStack_Top(LinkStack* stack)
{
return NULL;
}
int LinkStack_Size(LinkStack* stack)
{
if (stack == NULL)
{
return -1;
}
return LinkList_Length(stack);
}
5,括号匹配实现代码:
//match.c
#include "stdio.h"
#include "stdlib.h"
#include "linkstack.h"
int isLeft(char c)
{
int ret = 0;
switch(c)
{
case '<':
case '(':
case '[':
case '{':
case '\'':
case '\"':
ret = 1;
break;
default:
ret = 0;
break;
}
return ret;
}
int isRight(char c)
{
int ret = 0;
switch(c)
{
case '>':
case ')':
case ']':
case '}':
case '\'':
case '\"':
ret = 1;
break;
default:
ret = 0;
break;
}
return ret;
}
int match(char left, char right)
{
int ret = 0;
switch(left)
{
case '<':
ret = (right == '>');
break;
case '(':
ret = (right == ')');
break;
case '[':
ret = (right == ']');
break;
case '{':
ret = (right == '}');
break;
case '\'':
ret = (right == '\'');
break;
case '\"':
ret = (right == '\"');
break;
default:
ret = 0;
break;
}
return ret;
}
int scanner(const char* code)
{
LinkStack* stack = LinkStack_Create();
int ret = 0;
int i = 0;
while( code[i] != '\0' )
{
if( isLeft(code[i]) )
{
LinkStack_Push(stack, (void*)(code + i));
}
if( isRight(code[i]) )
{
char* c = (char*)LinkStack_Pop(stack);
if( (c == NULL) || !match(*c, code[i]) )
{
printf("%c 括号不匹配!\n", code[i]);
ret = 0;
break;
}
}
i++;
}
if( (LinkStack_Size(stack) == 0) && (code[i] == '\0') )
{
printf("匹配成功!\n");
ret = 1;
}
else
{
printf("不匹配!\n");
ret = 0;
}
LinkStack_Destroy(stack);
return ret;
}
void main()
{
const char* code = "#include <stdio.h> int main() { int a[4][4]; int (*p)[4]; p = a[0]; return 0;";//显然,待测字符串里面{不匹配
scanner(code);
return ;
}
(4)实现结果:
若将上述测试代码改为:
const char* code = "#include <stdio.h> int main() { int a[4][4]; int (*p)[4]; p = a[0]; return 0;";//显然,待测字符串匹配;测试结果如下: