1, LinkList.h
#ifndef _linklist_h_
#define _linklist_h_
typedef void linklist;
typedef struct _TAG_LINKLISTNODE Linklistnode;
struct _tag_linklistnode
{
Linklistnode* Next;
};
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
2, Linklist.c
#include <stdio.h>
#include <malloc.h>
#include "LinkList.h"
typedef struct _TAG_LINKLIST
{
Linklistnode header;
int length;
} tlinklist;
linklist* linklist_create ()//O (1)
{
tlinklist* ret = (tlinklist*) malloc (sizeof (tlinklist));
if (ret! = NULL)
{
ret->length = 0;
Ret->header.next = NULL;
}
return ret;
}
void Linklist_destroy (linklist* list)//O (1)
{
Free (list);
}
void Linklist_clear (linklist* list)//O (1)
{
tlinklist* sList = (tlinklist*) list;
if (sList! = NULL)
{
slist->length = 0;
Slist->header.next = NULL;
}
}
int linklist_length (linklist* list)//O (1)
{
tlinklist* sList = (tlinklist*) list;
int ret =-1;
if (sList! = NULL)
{
RET = slist->length;
}
return ret;
}
int Linklist_insert (linklist* list, linklistnode* node, int pos)//O (n)
{
tlinklist* sList = (tlinklist*) list;
int ret = (sList! = null) && (pos >= 0) && (node! = NULL);
int i = 0;
if (ret)
{
linklistnode* current = (linklistnode*) sList;
for (i=0; (I<pos) && (current->next! = NULL); i++)
{
Current = current->next;
}
Node->next = current->next;
Current->next = node;
slist->length++;
}
return ret;
}
linklistnode* linklist_get (linklist* list, int pos)//O (n)
{
tlinklist* sList = (tlinklist*) list;
linklistnode* ret = NULL;
int i = 0;
if ((sList! = NULL) && (0 <= Pos) && (POS < slist->length))
{
linklistnode* current = (linklistnode*) sList;
for (i=0; i<pos; i++)
{
Current = current->next;
}
RET = current->next;
}
return ret;
}
linklistnode* linklist_delete (linklist* list, int pos)//O (n)
{
tlinklist* sList = (tlinklist*) list;
linklistnode* ret = NULL;
int i = 0;
if ((sList! = NULL) && (0 <= Pos) && (POS < slist->length))
{
linklistnode* current = (linklistnode*) sList;
for (i=0; i<pos; i++)
{
Current = current->next;
}
RET = current->next;
Current->next = ret->next;
slist->length--;
}
return ret;
}
3, LinkStack.h
#ifndef _linkstack_h_
#define _linkstack_h_
typedef void Linkstack;
Linkstack* linkstack_create (); Creating stacks
void Linkstack_destroy (linkstack* stack); Destroying stacks
void Linkstack_clear (linkstack* stack); Empty stack
int Linkstack_push (linkstack* stack, void* item); Into the stack
void* Linkstack_pop (linkstack* stack); Out of the stack
void* linkstack_top (linkstack* stack); Get top of stack element
int linkstack_size (linkstack* stack); Get stack size
#endif
4, LINKSTACK.C
#include <malloc.h>
#include "LinkStack.h"
#include "LinkList.h"
typedef struct _TAG_LINKSTACKNODE
{
Linklistnode header;
void* item;
} Tlinkstacknode;
linkstack* linkstack_create ()//Create Stack
{
return Linklist_create ();
}
void Linkstack_destroy (linkstack* stack)//Destroy Stack
{
Linkstack_clear (stack);
Linklist_destroy (stack);
}
void Linkstack_clear (linkstack* stack)//emptying stack
{
while (linkstack_size (stack) > 0)//Determine if there are elements in the stack, and if so, perform a stack action
{
Linkstack_pop (stack);
}
}
int Linkstack_push (linkstack* stack, void* Item)//into the stack, item is the address of the incoming stack element
{
tlinkstacknode* node = (tlinkstacknode*) malloc (sizeof (Tlinkstacknode)); Dynamic memory allocation
int ret = (node! = NULL) && (item! = NULL);
if (ret)//judgment stack and the element pressed into the stack is legal
{
Node->item = Item;
ret = Linklist_insert (Stack, (linklistnode*) node, 0); Press the queue head
}
if (!ret)
{
Free (node);
}
return ret;
}
void* Linkstack_pop (linkstack* stack)//out Stack
{
tlinkstacknode* node = (tlinkstacknode*) linklist_delete (stack, 0);
void* ret = NULL;
if (node! = NULL)
{
RET = node->item;
Free (node);
}
return ret;
}
void* linkstack_top (linkstack* stack)//get stack top element
{
tlinkstacknode* node = (tlinkstacknode*) linklist_get (stack, 0);
void* ret = NULL;
if (node! = NULL)
{
RET = node->item;
}
return ret;
}
int linkstack_size (linkstack* stack)//Get stack size
{
return linklist_length (stack);
}
5, MAIN.C
#include <stdio.h>
#include <stdlib.h>
#include "LinkStack.h"
/* Run this program using the console Pauser or add your own getch, System ("pause") or input loop */
int Isleft (char c)//Find left sign, jump out when found
{
int ret = 0;
Switch (c)
{
Case ' < ':
Case ' (':
Case ' [':
Case ' {':
Case ' \ ':
Case ' \ ':
ret = 1;
Break
Default
ret = 0;
Break
}
return ret;
}
int Isright (char c)//search for right symbol number, jump out when found
{
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)//to match the number of symbols to the number of symbols
{
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]! = ' + ')//start into scan loop and exit when Terminator is encountered
{
if (Isleft (Code[i]))
{
Linkstack_push (Stack, (void*) (code + i)); Performs a stack operation when getting to the left symbol
}
if (Isright (Code[i]))//Get to signed, enter
{
char* C = (char*) linkstack_pop (stack); Out of the stack
if ((c = = NULL) | |!match (*C, code[i])//When C is empty and the match is unsuccessful, an error
{
printf ("%c does not match!\n", code[i]);
ret = 0;
Break
}
}
i++;
}
if (linkstack_size (stack) = = 0) && (code[i] = =))//If the element in the stack is 0 and the end character is scanned, the match is successful.
{
printf ("succeed!\n");
ret = 1;
}
Else
{
printf ("Invalid code!\n"); Input error
ret = 0;
}
Linkstack_destroy (stack); Destroy Stack table
return ret;
}
int main (int argc, char *argv[])
{
Const char* Code = "#include <stdio.h> int main () {int a[5][5]; int (*p) [4]; p = a[0]; printf (\ "%d\\n\", &p[3][3]-&a[3][3]); return 0; }";
Scanner (code);
return 0;
}
Seven, stack to achieve C language symbol matching