网站开发税目编码,ssh架构jsp网站开发,做淘宝联盟必须要有网站吗,企业门户网站特征【0】表ADT1#xff09;intro#xff1a;我们把 形如 A1, A2, A3, ..., An 的结构称为表#xff1b;2#xff09;表的实现#xff1a; 数组#xff08;循环数组#xff09; 或 链表 或 双链表 或 循环链表实现#xff1b;3#xff09;表的插入#xff0c;删除操作可以…【0】表ADT1intro我们把 形如 A1, A2, A3, ..., An 的结构称为表2表的实现 数组循环数组 或 链表 或 双链表 或 循环链表实现3表的插入删除操作可以在任意位置上进行【1】栈基于表1intro栈是限制插入和删除只能在一个位置上进行的的表2栈的实现 数组实现 或 链表实现3栈的应用应用荔枝1检验代码的平衡符号每一个 括号圆括号中括号花括号 都要一一对应Attentionstack.h如下#include stdio.h
#include malloc.h#define ElementType char
#define ERROR(str) printf(str)struct Stack;
typedef struct Stack *Stack;struct Stack
{int size;ElementType* head;int top;
};int isFull(Stack s);
int isEmpty(Stack s);
Stack initStack(int size);
void push(Stack s, ElementType c);
void pop(Stack s, ElementType *e);int isFull(Stack s)
{return s-size s-top ? 1 : 0;
}int isEmpty(Stack s)
{return 0 s-top ? 1 : 0;
}Stack initStack(int size)
{Stack s (Stack)malloc(sizeof(struct Stack));if(sNULL){ERROR(error: failed initStack() for there is no spare space.\n);}else {s-size size;s-top 0;s-head (ElementType*)malloc(sizeof(ElementType) * size);if(s-headNULL){ERROR(error: failed initStack() for there is no spare space.\n);return NULL;}}return s;
}void push(Stack s, ElementType c)
{if(!isFull(s)){s-head[s-top] c;} else{printf(%s, failed pushing for stack is full.);}
}void pop(Stack s, ElementType *e)
{if(!isEmpty(s)){*e s-head[--s-top];}else{*e ;printf(%s, failed poping for stack is empty.);}
}
/prepre code_snippet_id1797122 snippet_file_nameblog_20160731_3_2765700 namecode classcpp#include stack.h// check whether the grammar defined in given file is correct or not.
int checkFile(Stack s)
{FILE *fp;ElementType c;ElementType popc;fp fopen(D:\\classical_books\\datastructure_alg\\source_code\\chapter3\\review_for_job\\p52_check_balanced_char\\temp.txt, r);// only test for round bracket (), bracket [], brace{}.// do you know the meanings of open brace { and close brace }.while((cgetc(fp)) ! EOF){if(c ( || c [ || c {){push(s, c);}else if(c ) || c ] || c }){pop(s, popc);if(c) popc! (){return 0;}else if(c] popc! [){return 0;}else if(c} popc! {){return 0;}}}return 1;
}int main()
{Stack s;int result; s initStack(1000);if(sNULL){return -1;}result checkFile(s);printf(%d \n, result);return 0;
}应用荔枝2计算后缀表达式的值step1将中缀表达式转为 后缀表达式step2然后求 后缀表达式的值 Attentioninfix2postfix.h 见应用荔枝3#include infix2postfix.h void computeExpr(Stack s, ElementType *postfix, int length)
{int i 0;char c;ElementType num1, num2, result;for(;ilength;i){c postfix[i];if(isOperator(c) -1)// c is an operand.{push(s, c-48);}else if(isOperator(c) ! -1) // c is an operator.{pop(s, num1);pop(s, num2); switch(c){case : result num1num2;break;case -: result num1-num2;break;case *: result num1*num2;break;case /: result num1/num2;break;}push(s, result);}}pop(s, result); printf(final computing result is %d \n, result);
}int main()
{Stack s; ElementType output[255];int length;s initStack(1000);if(sNULL){return -1;}length infix2postfix(s, output); //switch infix into postfix.printChatArray(output, length);// compute postfix expr.free(s);sinitStack(1000);computeExpr(s, output, length);return 0;
}应用荔枝3中缀表达式 中缀转后缀表达式 stack.h 同上 infix2postfix.h 如下#include stack.h
#include math.hvoid printChatArray(ElementType* array, int size);
int isOperator(ElementType c);
int checkPriority(int p1, int p2);
int infix2postfix(Stack s, ElementType *output);// just print array.
void printChatArray(ElementType* array, int size)
{int i0;for(;isize; i){putchar(array[i]);}printf(\n\n);
}// check whether the char is operator or not.
// if true returns priority with array index ,otherwise return -1.
int isOperator(ElementType c)
{ int i 0;char priorities[] {(, , ,-, ,*,/};int size 7;for(; isize; i){if(cpriorities[i]){return i;}}return -1;
} // 0 means p1.priority p2.priority
// 1
// -1
int checkPriority(int p1, int p2)
{if(p1-p21 || p1-p2-1 || p1-p20){return 0;}else if(p1-p21){return 1;}else if(p1-p2 -1){return -1;}
}// transmit infix into postfix.
// attention for operands not being pushed into stack.
int infix2postfix(Stack s, ElementType *output)
{ ElementType c, popc, topc; int i 0;int p1, p2; printf(%s, input the expr: );while((cgetchar()) ! \n){if(c() // when the char is ( or ){push(s, c);}else if(c)){while(!isEmpty(s) (topcgetTop(s))!(){pop(s, popc);output[i] popc; }if(topc(){pop(s,popc);}} // when the char is ( or ) over.else if(isOperator(c) -1) // c is an operand.{output[i] c;}else if((p1isOperator(c)) ! -1) // c is an operator.{if(isEmpty(s)) // if the stack is empty.{push(s, c);}else // if the stack is not empty.{ while(!isEmpty(s)){topc getTop(s);// after that, check priority between c and topc.p2 isOperator(topc);if(checkPriority(p1,p2) ! 1) // p1.priority p2.priority, then pop operand under p2 into output array.{pop(s, popc);output[i] popc;}else{ break;}} push(s, c);} }}while(!isEmpty(s)) // pop surplus elements into output array.{pop(s, popc);output[i] popc; } return i;
}#include infix2postfix.h int main()
{Stack s; ElementType output[255];int length;s initStack(1000);if(sNULL){return -1;}length infix2postfix(s, output); //switch infix into postfix.printChatArray(output, length);return 0;
}【2】队列基于表1intro队列是插入在一端而删除在另一端的表2队列的实现数组实现 或 循环数组队列实现3循环队列代码如下#include stdio.h
#include malloc.h#define ElementType int
#define Error(str) printf(\nerror: %s, str)struct Queue;
typedef struct Queue* Queue;// 循环队列的数据结构.
struct Queue
{int capacity;int front;int rear;int size;ElementType* array;
};Queue initQueue(int capacity);
int isFull(Queue queue);
void enQueue(Queue queue, ElementType e);
int isEmpty(Queue queue);
ElementType deQueue(Queue queue);
void printQueue(Queue queue);// init queue wit capacity.
Queue initQueue(int capacity)
{// allocate memory for queue.Queue queue (Queue)malloc(sizeof(struct Queue)); if(queueNULL){Error(failed initQueue() for out of space.);return NULL; } queue-capacity capacity;queue-front 0;queue-rear 0;queue-size 0;// allocate memory for queue-array.queue-array (ElementType*)malloc(capacity * sizeof(ElementType));if(queue-array NULL){Error(failed initQueue() for out of space.);return NULL;} return queue;
}// judge whether the queue is full or not.
int isFull(Queue queue)
{return queue-size queue-capacity ? 1 : 0;
}// 进队列满时不进.
void enQueue(Queue queue, ElementType e)
{ if(isFull(queue)){Error(failed enQueue() for the queue is full.);return ;} queue-array[queue-rear % queue-capacity] e;queue-size;
}// judge whether the queue is empty or not.
int isEmpty(Queue queue)
{return queue-size 0 ? 1 : 0;
}// 出队列空时不出.
ElementType deQueue(Queue queue)
{int temp;if(isEmpty(queue)){Error(failed deQueue() for the queue is empty.);return -1;}temp queue-array[queue-front % queue-capacity];queue-size--;return temp;
}// 打印队列
void printQueue(Queue queue)
{int i, index;ElementType* array queue-array;printf(\n);for(i0; iqueue-size; i){index (queue-front i) % queue-capacity;printf(%d , array[index]);}printf(\n);
}// 打印队列所在数组
void printArray(Queue queue)
{int i;printf(\nelements in queue-array from index 0 are as follows: );for(i0; iqueue-size; i){printf(%d , queue-array[i]);}printf(\n);
}#include queue.hvoid main()
{ElementType array[] {3, 4, 6, 1, 2, 0, 10, 8, 9};Queue queue;int capacity6;int i;queueinitQueue(capacity); // 初始化队列if(queue NULL){return ;}printf(\nlet {3, 4, 6, 1, 2, 0, 10, 8, 9} enter queue.\n);for(i0; i9; i){enQueue(queue, array[i]); // 让元素进队列}printf(\n\nthe elements in queue are as follows: );printQueue(queue);// 让元素出队列deQueue(queue);deQueue(queue);deQueue(queue);enQueue(queue, array[6]);enQueue(queue, array[7]);enQueue(queue, array[8]);printf(\n\nafter 3 dequeue operations and enQueue({10,8,9}) ,the elements in queue are as follows: );printQueue(queue);printArray(queue);
}