数据结构(C语言)

一、回顾C语言

1.对一维数组中的元素逆序排列

#include <stdio.h>
 
void begin() {
	int i, Array[10];
	for(i = 0; i <= 9; i++) {
		Array[i] = i + 1;
	    printf("a[%d]=%d\n", i, Array[i]);
        }  
	}
 
void after() {
	int i, array[10];
	for(i = 9; i >= 0; i--) {
		array[i] = i + 1;
		printf("a[%d]=%d\n", i, array[i]);
        }
    }
 
int main(void) {
	printf("The original array is:\n");
	begin();
    printf("The after array is:\n");
    after();
    return 0;	
}

2.输出数组元素的地址和值

#include <stdio.h>
 
int main(void) {
	int a[5];
	int b, i;
    for(i = 0; i<= 5; i++){
    	a[i] = i;
    	printf("the address of a[%d]:%x,", i, &a[5]);
    	printf("the value of a[%d]:%d\n", i, a[i]);
	} 
}

3.设计师生通用表

教师数据有姓名、年龄、职业和教研室;学生数据有姓名、年龄、专业和班级,依次输入人员数据,以表格形式输出。

#include <stdio.h>
 
#define N 10
 
struct student {
    char name[10];/*姓名*/
    int age;    /*年龄*/
    char job;   /*用s或t表示学生或教师*/
    
    union {
        int class;        /*班级*/
        char office[10];  /*教研室*/
    } union1;
} stu[N];
 
int main(void) {
    int i;
    int n;
    printf("请输入人员数(<10):\n");
    scanf("%d",&n);
    for(i = 0; i<n; i++) { 
        printf("请输入第%d人员的信息:名字,年龄和职业:\n", i+1);
        scanf("%s %d %c", &stu[i].name, &stu[i].age, &stu[i].job);
        if(stu[i].job == 's') {
            printf("请输入班级:\n");
            scanf("%d", &stu[i].union1.class);
        }
        else {
            printf("请输入教研室:\n");
            scanf("%s", &stu[i].union1.office);
        }
    }
    return 0;
}

二、栈

1.源代码(栈)

#include <stdio.h>
#include <stdlib.h>
 
#define STACK_SIZE 10  /*初始存储空间*/
 
typedef  int elemType; /*定义元素类型*/
typedef struct{
    elemType *base;
    elemType *top;
    int stacksize;     /*已分配存储空间*/
} SqStack;
 
void InitStack(SqStack *S);   /*构造空栈*/
int push(SqStack *S, elemType e); /*入栈*/
int Pop(SqStack *S, elemType *e);  /*出栈*/
int CreateStack(SqStack *S);     /*创建栈*/
void PrintStack(SqStack *S);   /*输出栈中元素*/
 
void InitStack(SqStack *S) {
    S->base = (elemType *) malloc (STACK_SIZE * sizeof (elemType));
    S->top = S->base;
    S->stacksize = STACK_SIZE;
}
 
int Push(SqStack *S, elemType e) {
    if (S->top - S->base == S->stacksize) {
    	S->base = (elemType*) malloc (STACK_SIZE * sizeof (elemType));
    if(!S->base) {
		return 0;	
		S->top = S->base + S->stacksize;
    	S->stacksize = S->stacksize + STACK_SIZE;
		}
	}
	*S->top = e;
	S->top++;
}
 
int Pop(SqStack *S, elemType *e) {
   if(S->top == S->base) {
	return 0;
   }
   else {
	--S->top;
   *e = *S->top;
   }
}
 
int CreateStack(SqStack *S) {
    int e;
    InitStack(S);
    printf("Push:\n");
    while(scanf("%d", &e)) {
    	Push(S,e);
	}
}
 
void PrintStack(SqStack *S) {
    elemType e;
    while(Pop(S, &e)){
    	printf("%d ", e);
	}
}
 
void main(void) {
    SqStack ss;
    CreateStack(&ss);
    printf("Pop:\n");
    PrintStack(&ss);
}

2.运行结果

三、循环队列

1.循环队列基本操作

初始化队列、判断队列空、入队、出队、取队头元素、显示队列元素。

2.源代码(队列)

#include <stdio.h>
#include <stdlib.h>
 
#define MAXSIZE 10 //最大长度 
#define OK 1
#define ERROR 0
 
typedef char elemType;
typedef struct{
	elemType *base; //初始化存储空间 
	int  front; //头指针 
	int rear; //尾指针 
} SeqQueue; 
 
//初始化队列 
int initQueue(SeqQueue *Q) {
	Q->base = (elemType *) malloc (sizeof(char) * MAXSIZE);
	if (Q->base == 0) {
		return ERROR;
	}
	else{
	Q->front = Q->rear = 0;
	return OK;	
	}
}
 
//判断队列空 
int emptyQueue(SeqQueue *Q) {
	if (Q->front == Q->rear) {
		return OK;
	}
	else 
	return ERROR;
}
 
//入队 
int inQueue(SeqQueue *Q, elemType e) {
	if ((Q->rear + 1) % MAXSIZE == Q->front) {
		return ERROR;
	}
	else{
	Q->base[Q->rear] = e;
	Q->rear = (Q->rear + 1) % MAXSIZE;
	return OK; 
	}
}
 
//出队 
int deQueue(SeqQueue *Q, elemType *e) {
	if (Q->front == Q->rear) {
		return ERROR;
	}
	else{
	*e = Q->base[Q->front];
	Q->front  = (Q->front + 1) % MAXSIZE;
	return OK;
	}
}

四、二叉树

1.二叉树递归遍历

//先序遍历 
void PreOrder(BinTree root)
{
    if( root != NULL) { //非空二叉树 
    	printf("%c", root->data); //访问根节点 
    	PreOrder(root->LChild); //递归遍历左子树 
    	PreOrder(root->RChild); //递归遍历右子树 
	}
}
 
//中序遍历 
void InOrder(BinTree root)
{
	if( root != NULL) {
		InOrder(root->LChild);
    	printf("%c", root->data);
    	InOrder(root->RChild);
	}
}
 
//后序遍历 
void PostOrder(BinTree root)
{
	if( root != NULL) {
		PostOrder(root->LChild);
		PostOrder(root->RChild);
    	printf("%c", root->data);
	}
}

2.二叉树遍历应用

//统计结点总数 
int Count(BinTree T)
{
	if(T == NULL) { 
		return 0;	
	}
	else {
		return( Count(T->LChild) + Count(T->RChild) + 1 );	
	}
}
 
//统计叶子结点总数 
int LeafCount(BinTree T)
{
	if(T == NULL){
		return 0;	
	}
	else if(T->LChild == NULL && T->RChild == NULL){
		return 1;
	}
	else {
	return( LeafCount(T->LChild) + LeafCount(T->RChild) );
	}	
}

五、图的遍历

深度优先搜索遍历

#include <stdio.h>
 
#define N 20
#define TRUE 1
#define FALSE 0
 
int visited[N];
 
typedef struct { 
  int vexnum, arcnum; //顶点个数、边数 
  char vexs[N]; //存放顶点的数组 
  int arcs[N][N]; //存放边的二维数组 
} graph; //图的邻接矩阵
 
void createGraph(graph *g); //建立无向图的邻接矩阵 
void dfs(int i, graph *g);  //从第i个顶点进行深度优先搜索 
void tdfs(graph *g); // 深度优先搜索整个图            
void init_visit(); //初始化访问标识数组        
 
void createGraph(graph *g) {
  int i, j;
  char v;
  g->vexnum  = 0;
  g->arcnum  = 0;
  i = 0;
  printf("输入顶点序列(以#结束):\n");
  while ((v = getchar()) != '#'){
    g->vexs[i] = v;
    i++;
  }
  g->vexnum = i;                 
  for (i = 0; i < g->vexnum; i++) {  
  for (j = 0; j< g->vexnum ; j++) {
  g->arcs[i][j] = 0;
	}
  }
  printf("输入边的信息:\n");
  scanf("%d,%d", &i, &j); 
  while (i != -1) {
   g->arcs[i][j] = 1; 
   g->arcs[j][i] = 1;
   scanf("%d, %d", &i, &j);
  }
}
 
void dfs(int i, graph *g) 
{
  int j;
  printf("%c", g->vexs[i]);
  visited[i] = TRUE;
  for (j = 0; j < g->vexnum; j++) {
  if (g->arcs[i][j] == 1 && !visited[j])
  dfs(j, g);
  }
}
 
void tdfs(graph *g) 
{
  int i;
  printf("\n从顶点%C开始深度优先搜索序列:", g->vexs[0]);
  for (i = 0; i < g->vexnum ; i++) {
  if (visited[i] != TRUE)
  dfs(i, g);
  }  
}
 
void init_visit() 
{
  int i;
  for (i = 0; i < N; i++) {
    visited[i] = FALSE;
  }
}

六、查找

1.顺序查找

//顺序查找 
#include <stdio.h>
 
#define N 20
 
typedef struct {
	int elem[N];
	int length;
} STable;
 
int ST_search(STable st, int key) {
	int i;
	st.elem [0] = key;
	for (i = st.length; st.elem[i] != key; i--)
		;
	return i;
}

2.折半查找

//折半查找 
#include <stdio.h>
 
#define N 20
 
typedef struct {
	int elem[N];
	int length;
} STable;
 
int ZB_search(STable ST, int key) {
	int low, hight, mid;
	low = 1;
	hight = ST.length ;
	while ( low <= hight) {
		mid = (low + hight) / 2;
		if (ST.elem[mid] == mid) {
			return mid;
		} else if (ST.elem[mid] < key) {
			hight = mid - 1;
		} else {
			low = mid + 1;
		}
	}
	return -1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值