都经过编译的
链表的
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define N 10
- typedef struct node *link;
- struct node
- {
- int item;
- link next;
- };
- link NODE(int item,link next)
- {
- link t = (link)malloc(sizeof *t);
- t->item = item;
- t->next = next;
- return t;
- }
- int main()
- {
- int i;
- link head = NULL;
- link t = NULL;
- srand(time(NULL));
- for(i=0;i<N;i++)
- head = NODE(rand()%100,head);
- for(t=head;t!=NULL;t=t->next)
- printf ("%3d",t->item);
- printf("/n");
- return 0;
- }
非空表头链表
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define N 10
- typedef struct node *link;
- struct node
- {
- int item;
- link next;
- };
- link NODE(int item,link next)
- {
- link t = (link)malloc(sizeof *t);
- t->item = item;
- t->next = next;
- return t;
- }
- link insert_node(link head,int item)
- {
- link x,y;
- for(y=head,x=head;y;x=y,y=y->next)
- if(item<=y->item) break;
- if(x==y) head = NODE(item,head); //判断是不是表头
- x->next = NODE(item,y);
- }
- int main()
- {
- int i;
- link head = NULL;
- link t = NULL;
- srand(time(NULL));
- for(i=0;i<N;i++)
- head = insert_node(head,read()%100);
- for(t=head;t!=NULL;t=t->next)
- printf ("%3d",t->item);
- printf("/n");
- return 0;
- }
逆波兰表达式
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int *S;
- int top;
- void init_stack(int max_stack) //初始化站
- {
- S = (int*)malloc(max_stack*sizeof(int));
- top = 0;
- }
- void push(int item) //压栈
- {
- S[top++] = item;
- }
- int pop() //出栈
- {
- return S[--top];
- }
- int is_empty() //判断栈空
- {
- return top == 0;
- }
- int posteval(char *a)
- {
- int i,n = strlen(a);
- init_stack(n);
- for(i=0;i<n;i++){
- if(a[i] == '+') push(pop()+pop());
- if(a[i] == '*') push(pop()*pop());
- if(a[i]>='0' && a[i]<='9')
- push(a[i] - '0' );
- }
- return pop();
- }
- int main()
- {
- printf("%d/n",posteval("1 2 + 3 4 + *"));
- return 0 ;
- }
BST 树
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define N 8
- typedef struct node *link;
- struct node
- {
- int item;
- link l,r;
- };
- link NODE(int item)
- {
- link t = (link)malloc(sizeof *t);
- t->item = item;
- t->l = NULL;
- t->r = NULL;
- return t;
- }
- int search(link t,int key)
- {
- if(t==NULL) return 0;
- if(key < t->item) return search(t->l,key);
- if(key > t->item) return search(t->r,key);
- return 1;
- }
- link insert_node(link t,int item)
- {
- if(t==NULL)
- return NODE(item);
- if(item <= t->item)
- t->l = insert_node(t->l,item);
- else if(item > t->item)
- t->r = insert_node(t->r,item);
- return t;
- }
- void pre_ord(link t)
- {
- if(t==NULL) return ;
- printf("%3d",t->item);
- pre_ord(t->l);
- pre_ord(t->r);
- }
- void in_ord(link t)
- {
- if(t==NULL) return ;
- in_ord(t->l);
- printf("%3d",t->item);
- in_ord(t->r);
- }
- void lost_ord(link t)
- {
- if(t==NULL) return ;
- lost_ord(t->l);
- lost_ord(t->r);
- printf("%3d",t->item);
- }
- int main()
- {
- link root = NULL;
- srand(time(NULL));
- int i;
- for(i=0;i<N;i++)
- {
- root = insert_node(root,rand()%100);
- }
- pre_ord(root);
- printf("/n");
- in_ord(root);
- printf("/n");
- lost_ord(root);
- printf("/n");
- return 0;
- }