带表头结点的循环链表
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
struct stack_data
{
int stack[MAX];
int top;
};
enum return_val{PUSH_OK = 100, PUSH_NO, POP_OK, POP_NO, FULL_OK,FULL_NO, EMPTY_OK, EMPTY_NO};
typedef struct stack_data Stack;
void create_stack(Stack ** stack)
{
*stack = (Stack *)malloc(sizeof(Stack));
if(*stack == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
int is_full(Stack * stack)
{
if (stack->top == MAX - 1)
{
return FULL_OK;
}
return FULL_NO;
}
int push_stack(Stack * stack, int num)
{
if (is_full(stack) == FULL_OK)
{
printf("stack is full!\n");
return PUSH_NO;
}
else
{
stack->top++;
stack->stack[stack->top] = num;
return PUSH_OK;
}
}
void init_stack(Stack * stack)
{
stack->top = -1;
}
int pop_stack(Stack * stack)
{
if (is_empty(stack) == EMPTY_OK)
{
printf("stack is empty!\n");
return POP_NO;
}
else
{
return stack->stack[stack->top--];
}
}
int is_empty(Stack *stack)
{
if (stack->top == -1)
{
return EMPTY_OK;
}
else
{
return EMPTY_NO;
}
}
int main()
{
int i, num;
Stack * stack;
create_stack(&stack);
init_stack(stack);
for (i = 0; i < 10; i++)
{
num = push_stack(stack,i + 1);
if (num == PUSH_OK)
{
printf("push ok\n");
}
else
{
printf("push no\n");
break;
}
}
for (i = 0; i < 10; i++)
{
if (pop_stack(stack) == POP_NO)
{
printf("pop failure!\n");
break;
}
else
{
printf("num = %d\n",stack->stack[i]);
}
}
return 0;
}
栈的相关操作
#include <stdio.h>
#include <stdlib.h>
struct head_node
{
int num;
struct head_node * next;
};
typedef struct head_node Hnode;
typedef struct head_node * Hlink;
void is_malloc_ok(Hlink node)
{
if (node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void create_node(Hlink * node)
{
*node = (Hlink)malloc(sizeof(Hnode));
is_malloc_ok(*node);
}
void create_link(Hlink * head)
{
create_node(head);
(*head)->next = (*head);
}
void insert_node_head(Hlink head,Hlink new_node)
{
new_node->next = head->next;
head->next = new_node;
}
void display_link(Hlink head)
{
Hlink p;
if (head == NULL)
{
printf("link dont exit!\n");
return;
}
p = head->next;
if ((head->next) == head)
{
printf("link is empty!\n");
return;
}
while (p != head)
{
printf("%d\n",p->num);
p = p->next;
}
}
void insert_node_tail(Hlink head, Hlink new_node)
{
Hlink p;
p = head;
while (p->next != head)
{
p = p->next;
}
new_node->next = head;
p->next = new_node;
}
void insert_node_mid(Hlink head,Hlink new_node,int loc)
{
Hlink p, q;
p = q = head->next;
while (p != head && p->num != loc)
{
q = p;
p = p->next;
}
new_node->next = p;
q->next = new_node;
}
void free_link(Hlink head)
{
Hlink p;
p = head->next;
while(head->next != head)
{
head->next = p->next;
free(p);
p = head->next;
}
}
void release_link(Hlink *head)
{
free_link(*head);
free(*head);
*head = NULL;
}
int main()
{
Hlink head;
Hlink new_node;
int i,loc;
create_link(&head);
for(i = 0; i < 10; i++)
{
create_node(&new_node);
new_node->num = i + 1;
//insert_node_head(head,new_node);
insert_node_tail(head,new_node);
}
display_link(head);
printf("\n");
create_node(&new_node);
printf("Please input a number for new_node :\n");
scanf("%d",&new_node->num);
printf("Please input a number for insert location :\n");
scanf("%d",&loc);
insert_node_mid(head,new_node,loc);
display_link(head);
// free_link(head);
release_link(&head);
display_link(head);
return 0;
}
需要理解 关于栈的运用
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct operand
{
int data[MAX];
int top;
};
struct operator_ch
{
int top;
char data[MAX];
};
typedef struct operand OPND;
typedef struct operator_ch OPCH;
void init_OPND_stack(OPND *stack)
{
stack->top = -1;
}
void init_OPCH_stack(OPCH *stack)
{
stack->top = -1;
}
int is_empty_OPND(OPND *stack)
{
if(stack->top == -1)
{
return -1;
}
return 0;
}
int is_empty_OPCH(OPCH *stack)
{
if(stack->top == -1)
{
return -1;
}
return 0;
}
int get_OPND_top(OPND *stack)
{
if(is_empty_OPND(stack) == -1)
{
return -1;
}
return stack->data[stack->top];
}
char get_OPCH_top(OPCH *stack)
{
if(is_empty_OPCH(stack) == -1)
{
return -1;
}
return stack->data[stack->top];
}
void push_OPND(OPND *stack, int num)
{
stack->data[++(stack->top)] = num;
printf("push %d\n",stack->data[stack->top]);
}
void push_OPCH(OPCH *stack, char ch)
{
stack->data[++(stack->top)] = ch;
}
int pop_OPND(OPND *stack)
{
if(is_empty_OPND(stack) == -1)
{
return -1;
}
int num = stack->data[stack->top];
(stack->top)--;
return num;
}
char pop_OPCH(OPCH *stack)
{
if(is_empty_OPCH(stack) == -1)
{
return -1;
}
char ch = stack->data[stack->top];
(stack->top)--;
return ch;
}
int get_priority(char ch)
{
if(ch == '+' || ch == '-')
{
return 1;
}
if(ch == '*' || ch == '/')
{
return 2;
}
}
int compare_priority(char op_ch, char ch)
{
if(get_priority(op_ch) >= get_priority(ch))
{
return 1;
}
return -1;
}
int count(int a, int b, char ch)
{
int result;
switch(ch)
{
case '+':
{
result = a + b;
break;
}
case '-':
{
result = a - b;
break;
}
case '*':
{
result = a * b;
break;
}
case '/':
{
result = a / b;
break;
}
}
return result;
}
int main()
{
char ch;
char op_ch;
int a,b;
int opr_num;
int result_cal;
OPND * opnd_stack;
OPCH * opch_stack;
opnd_stack = (OPND *)malloc(sizeof(OPND));
opch_stack = (OPCH *)malloc(sizeof(OPCH));
init_OPND_stack(opnd_stack);
init_OPCH_stack(opch_stack);
while((ch = getchar()) != '\n')
{
if(ch >= '0' && ch <= '9')
{
// push_stack1();
opr_num = ch - '0';
push_OPND(opnd_stack,opr_num);
}
else
{
if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
//op_ch = pop_stack2();
do
{
op_ch = get_OPCH_top(opch_stack);
if(op_ch == -1)
{
push_OPCH(opch_stack,ch);
break;
}
else
{
if(compare_priority(op_ch,ch) < 0)
{
// push_stack2(ch);
push_OPCH(opch_stack,ch);
break;
}
else
{
a = pop_OPND(opnd_stack);
b = pop_OPND(opnd_stack);
op_ch = pop_OPCH(opch_stack);
if(a == -1 || b == -1 || op_ch == -1)
{
printf("stack is empty!\n");
}
// printf("%d%c%d",a,op_ch,b);
// printf("\n");
result_cal = count(b,a,op_ch);
push_OPND(opnd_stack,result_cal);
}
}
}while(compare_priority(op_ch,ch) > 0);
// push_OPCH(opch_stack,ch);
}
}
}
while(is_empty_OPND(opnd_stack) != -1 && is_empty_OPCH(opch_stack) != -1)
{
a = pop_OPND(opnd_stack);
b = pop_OPND(opnd_stack);
op_ch = pop_OPCH(opch_stack);
result_cal = count(b,a,op_ch);
push_OPND(opnd_stack,result_cal);
}
// printf("%d%c%d",a,op_ch,b);
// printf("\n");
printf("result is %d\n",result_cal);
free(opnd_stack);
free(opch_stack);
return 0;
}