帮别人做的,和国内大学比比看,差距在哪里。你大一的时候在做什么?反正我大一学C语言的时候只会照着课本敲。
1. 用链表实现栈
2. 用两个栈实现队列(用1中的栈)
3. 实现二叉树的4种遍历:前序,中序,后序,按层遍历(使用2中的队列)
Item.h
/* Item is actually a link to a tree */
typedef struct TREEnode* t_link;
typedef t_link Item;
struct TREEnode {
char item;
t_link l,r;
};
my_stack.h
/* remember to define the struct node in my_stack.c */
#ifndef ITEM_H
#define ITEM_H
#include "Item.h"
#endif
typedef struct node* link;
struct node {
Item it;
struct node* next;
};
void my_STACKinit(link* head, int N);
int my_STACKempty(link* head);
void my_STACKpush(link* head, Item item);
Item my_STACKpop(link* head);
my_stack.c
/*
The structure of the stack:
[it1 <- it2 <- ... <- top]<-Head
*/
#include <stdio.h>
#include <stdlib.h>
#include "my_stack.h"
#ifndef ITEM_H
#define ITEM_H
#include "Item.h"
#endif
void my_STACKinit(link* head, int N)
{
(*head) = (link)malloc(sizeof(struct node)*N);
(*head)->next = NULL;
}
int my_STACKempty(link* head)
{
if ((*head)->next == NULL) {
return 1; // empty
}else {
return 0;
}
}
void my_STACKpush(link* head, Item item)
{
link newNode = (link)malloc(sizeof(struct node));
// init the item
newNode->it = item;
// push
newNode->next = (*head)->next;
(*head)->next = newNode;
}
Item my_STACKpop(link* head)
{
link oldNode = (*head)->next;
Item it;
(*head)->next = oldNode->next;
it = oldNode->it;
free(oldNode);
return it;
}
my_queue.h
void my_QUEUEinit(int N);
int my_QUEUEempty();
void my_QUEUEput(Item item);
Item my_QUEUEget();
my_queue.c
#include <stdio.h>
#include <stdlib.h>
#include "my_stack.h"
link stk1, stk2; // stk1 for put; stk2 for get
void my_QUEUEinit(int N)
{
stk1 = (link)malloc(sizeof(struct node));
stk2 = (link)malloc(sizeof(struct node));
my_STACKinit(&stk1, N);
my_STACKinit(&stk2, N);
}
int my_QUEUEempty()
{
if (my_STACKempty(&stk1)
&& my_STACKempty(&stk2) ) {
return 1; // empty
}else {
return 0;
}
}
void my_QUEUEput(Item item)
{
// stk2 ->[put all item into]-> stk1
Item it;
while (!my_STACKempty(&stk2)) {
it = my_STACKpop(&stk2);
my_STACKpush(&stk1, it);
}
// put item to tail of the stk1
my_STACKpush(&stk1, item);
}
Item my_QUEUEget()
{
// stk1 ->[put all item into]-> stk2
Item it;
while (!my_STACKempty(&stk1)) {
it = my_STACKpop(&stk1);
my_STACKpush(&stk2, it);
}
// get the item
it = my_STACKpop(&stk2);
return it;
}
tree.c
#include <stdio.h>
#include <stdlib.h>
#ifndef ITEM_H
#define ITEM_H
#include "Item.h"
#endif
#include "my_queue.h"
#include <windows.h>
Item tree;
void print_node(Item h);
void traverse_preorder(Item h, void (*visit)(Item));
void traverse_inorder(Item h, void (*visit)(Item));
void traverse_postorder(Item h, void (*visit)(Item));
void traverse_levelorder(Item h, void (*visit)(Item));
Item getNode(char ch, int n);
int main()
{
int n, i;
char ch;
char *input;
Item root;
// freopen("data.in", "r", stdin); // read data for file, for debug
// input the number of nodes
printf("How many nodes has your tree? ");
scanf("%d", &n);
// input the node
printf("The nodes of your tree are: ");
tree = (Item)malloc(sizeof(struct TREEnode)*n);
for (i=0; i<n; ++i) {
scanf("%*c%c", &(tree[i].item));
}
// input the root
printf("Which is the root of your tree? ");
scanf("%*c%c", &ch);
root = getNode(ch, n);
// assign the children
for (i=0; i<n; ++i) {
printf("left child of %c: ", tree[i].item);
scanf("%*c%c", &ch);
tree[i].l = getNode(ch, n);
printf("right child of %c: ", tree[i].item);
scanf("%*c%c", &ch);
tree[i].r = getNode(ch, n);
}
// Structure of the tree
for (i=0; i<n; ++i) {
printf("%c: ", tree[i].item);
if (tree[i].l) printf("left=%c ", (tree[i].l)->item);
if (tree[i].r) printf("right=%c ", (tree[i].r)->item);
printf("\n");
}
// four orders of traversing the tree
printf("preorder: ");
traverse_preorder(root, print_node);
printf("\n");
printf("inorder: ");
traverse_inorder(root, print_node);
printf("\n");
printf("postorder: ");
traverse_postorder(root, print_node);
printf("\n");
printf("level-order: ");
my_QUEUEinit(n);
my_QUEUEput(root);
traverse_levelorder(root, print_node);
printf("\n");
system("pause");
return 0;
}
void print_node(Item h)
{
printf(" %c", h->item);
}
// root left right
void traverse_preorder(Item h, void (*visit)(Item))
{
if (h) {
visit(h);
traverse_preorder(h->l, visit);
traverse_preorder(h->r, visit);
}
}
// left root right
void traverse_inorder(Item h, void (*visit)(Item))
{
if (h) {
traverse_inorder(h->l, visit);
visit(h);
traverse_inorder(h->r, visit);
}
}
// left right root
void traverse_postorder(Item h, void (*visit)(Item))
{
if (h) {
traverse_postorder(h->l, visit);
traverse_postorder(h->r, visit);
visit(h);
}
}
void traverse_levelorder(Item h, void (*visit)(Item))
{
Item it;
while (!my_QUEUEempty()) {
it = my_QUEUEget();
visit(it);
if (it->l) my_QUEUEput(it->l);
if (it->r) my_QUEUEput(it->r);
}
}
Item getNode(char ch, int n)
{
int i;
if (ch == '-') return NULL;
for (i=0; i<n; ++i) {
if (tree[i].item == ch) {
return &(tree[i]);
}
}
}
运行结果