顺序表练习1
#define _CRT_SECURE_NO_WARNINGS 1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define MAXSIZE 100
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
typedef int ElemType;
typedef struct
{
ElemType *elem ;
int length;
}Sqlist;
int init_Lnode(Sqlist &L,int ch[], int len)//初始化顺序表
{
L.elem = new ElemType[MAXSIZE];
if (!L.elem) exit(-1);
L.length = 0;
for (int i = 0; i < len; i++)
{
L.elem[i] = ch[i];
}
return OK;
}
int insert_Lnode(Sqlist& L, int c, int len)//插入
{
int pos = 0;
for (int i = 0; i < len; i++)
{
if (c > L.elem[i]) continue;
else if (c <= L.elem[i])
{
pos = i;
break;
}
}
for (int i = len; i >= pos; i--)
{
L.elem[i + 1] = L.elem[i];
}
L.elem[pos] = c;
return OK;
}
int main()
{
int ch[10] = { 0 };//输入顺序表的值
int len = 0;
while (1)
{
int num;
scanf("%d", &num);
ch[len] = num;
char tep = getchar();
len++;
if (tep == '\n')
{
break;
}
}
int c = 0;
scanf("%d", &c);
Sqlist L;
init_Lnode(L, ch, len);
insert_Lnode(L, c, len);
for (int i = 0; i < len + 1; i++)
{
printf("%d", L.elem[i]);
if (i < len)
{
printf(",");
}
else
{
printf("\n");
}
}
return 0;
}
链表练习— 删除结点
#define _CRT_SECURE_NO_WARNINGS 1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define MAXSIZE 100
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int DataType;
typedef struct node
{
DataType data;
struct node* next;
}Linknode, *Linklist;
void initLinklist(Linklist& head)//初始化链表
{
head = new Linknode;
head->next = NULL;
}
int LengthLinklist(Linklist& head)//计算链表长度
{
Linklist p = head->next;
int count = 0;
while (p != NULL)
{
count += 1;
p = p->next;
}
return count;
}
int FindLinklist(Linklist& L, DataType x)//找出对应元素节点
{
Linklist p = L;
int count = 0;
while (p->next != NULL)
{
p = p->next;
if (p->data == x)
{
return count;
}
count += 1;
}
return 0;
}
Linklist LocateLinklist(Linklist& L, int i)//定位第i个元素节点
{
if (i < 0) return NULL;
Linklist p = L->next;
int count = 0;
while (p != NULL)
{
if (count == i) return p;
p = p->next;
count += 1;
}
return NULL;
}
int addLinklist(Linklist& P, DataType x)
{
Linklist L = P;
if (L == NULL) return 0;
while (L->next != NULL)
{
L = L->next;
}
Linklist node = new Linknode;
if (node != NULL)
{
node->data = x;
node->next = L->next;
L->next = node;
return 1;
}
else return 0;
}
int RemoveLinklist(Linklist& L, int i, DataType& x)//删除指定节点,并返回删除元素
{
Linklist p = LocateLinklist(L, i - 1);
if (p == NULL|| p->next==NULL) return 0;
x = p->next->data;
Linklist s = p->next;
p->next = s->next;
free(s);
return 1;
}
int main()
{
Linklist head;
initLinklist(head);
int num = 0;
char ch = 0;
while (1)
{
scanf("%d", &num);
addLinklist(head, num);
ch = getchar();
if (ch == '\n')
{
break;
}
}
scanf("%d", &num);
int n = FindLinklist(head, num);
int key = RemoveLinklist(head, n - 1, num);
if (key == 0)
{
printf("该结点没有前驱结点\n");
}
else
{
int len = LengthLinklist(head);
Linklist node = head->next;
for (int i = 0; i < len; i++)
{
printf("%d", node->data);
if (i < len - 1) printf(" ");
else printf("\n");
node = node->next;
}
}
return 0;
}
链表求交集
#define _CRT_SECURE_NO_WARNINGS 1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define MAXSIZE 100
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int DataType;
typedef struct node
{
DataType data;
struct node* next;
}Linknode, * Linklist;
void initLinklist(Linklist& head)//初始化链表
{
head = new Linknode;
head->next = NULL;
}
void clearLinklist(Linklist& head)//清空链表
{
Linklist q;
while (head->next != NULL)
{
q = head->next;
head->next = q->next;
free(q);
}
}
int LengthLinklist(Linklist& head)//计算链表长度
{
Linklist p = head->next;
int count = 0;
while (p != NULL)
{
count += 1;
p = p->next;
}
return count;
}
Linklist FindLinklist(Linklist& L, DataType x)//找出对应元素节点
{
Linklist p = L->next;
int count = 0;
while (p != NULL)
{
count += 1;
p = p->next;
if (p->data == x)
{
return p;
}
}
return 0;
}
Linklist LocateLinklist(Linklist& L, int i)//定位第i个元素节点
{
if (i < 0) return NULL;
Linklist p = L->next;
int count = 0;
while (p != NULL)
{
if (count == i) return p;
p = p->next;
count += 1;
}
return NULL;
}
int InsertLinklist(Linklist& L, int i, DataType x)//指定位置插入元素
{
Linklist p = LocateLinklist(L, i - 1);
if (p == NULL) return 0;
Linklist s = new Linknode;
if (s != NULL)
{
s->data = x;
s->next = p->next;
p->next = s;
return 1;
}
else return 0;
}
int RemoveLinklist(Linklist& L, int i, DataType& x)//删除指定节点,并返回删除元素
{
Linklist p = LocateLinklist(L, i - 1);
if (p == NULL || p->next == NULL) return 0;
x = p->next->data;
Linklist s = p->next;
p->next = s->next;
free(s);
return 1;
}
int addLinklist(Linklist& P, DataType x)//将元素插入到链表末尾
{
Linklist L = P;
if (L == NULL) return 0;
while (L->next != NULL)
{
L = L->next;
}
Linklist node = new Linknode;
if (node != NULL)
{
node->data = x;
node->next = L->next;
L->next = node;
return 1;
}
else return 0;
}
void maskLinklist(Linklist& head_a, Linklist& head_b, Linklist& head_c)
{
Linklist pa = head_a->next;
Linklist pb = head_b->next;
Linklist pc = head_c;
while (pa != NULL && pb != NULL)
{
if (pa->data == pb->data)
{
addLinklist(head_c, pa->data);
pa = pa->next;
pb = pb->next;
}
else if (pa->data > pb->data)
{
pb = pb->next;
}
else if (pa->data < pb->data)
{
pa = pa->next;
}
}
}
int main()
{
Linklist head_a;
Linklist head_b;
Linklist head_c;
initLinklist(head_a);
initLinklist(head_b);
initLinklist(head_c);
int num;
int count = 0;
while (1)
{
scanf("%d", &num);
addLinklist(head_a, num);
char tep = getchar();
if (tep == '\n') break;
}
while (1)
{
scanf("%d", &num);
addLinklist(head_b, num);
char tep = getchar();
if (tep == '\n') break;
}
maskLinklist(head_a, head_b, head_c);
int len = LengthLinklist(head_c);
Linklist node = head_c->next;
if (head_c->next == NULL)
{
printf("没有交集\n");
}
else
{
for (int i = 0; i < len; i++)
{
printf("%d", node->data);
node = node->next;
printf(" ");
}
printf("\n");
}
return 0;
}
栈和队列练习—判断字符串是否 回文
#define _CRT_SECURE_NO_WARNINGS 1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define MAXSIZE 100
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef char SElemType;
typedef struct
{
SElemType* elem;
int maxsize;
int top;
}SeqStack;
int initStack(SeqStack& S)//栈的初始化
{
S.elem = new SElemType[MAXSIZE];
if (S.elem == NULL) return -1;
S.maxsize = MAXSIZE;
S.top = 0;
}
int pushStack(SeqStack& S, SElemType x)//进栈,栈顶指针等于最后一个元素,既为空时是-1
{
if (S.top == S.maxsize ) return 0;
S.elem[++S.top] = x;
return 1;
}
int popStack(SeqStack& S, SElemType& x)//出栈
{
if (S.top == 0) return 0;
x = S.elem[S.top];
S.top--;
return 1;
}
int gettopStack(SeqStack& S, SElemType& x)//获取栈顶值
{
if (S.top == 0) return 0;
x = S.elem[S.top];
return 1;
}
int emptyStack(SeqStack& S)//栈空为1
{
return S.top == 0;
}
int FullStack(SeqStack& S)//是否栈满
{
return S.top == S.maxsize;
}
int SizeStack(SeqStack& S)
{
return S.top;
}
int main()
{
SElemType ch[] = {0,0,0,0,0,0,0,0,0,0};
int len = 0;
while (1)
{
char arr = getchar();
if (arr == '\n') break;
ch[len] = arr;
len++;
}
SeqStack S;
initStack(S);
for (int i = 0; i < len; i++)
{
pushStack(S, ch[i]);
}
SElemType ch2[] = {0,0,0,0,0,0,0,0,0,0};
for (int i = 0; i < len; i++)
{
popStack(S, ch2[i]);
}
for (int i = 0; i < len; i++)
{
if (ch[i] != ch2[i])
{
printf("不是回文\n");
return 0;
}
}
printf("是回文\n");
return 0;
}
栈和队列练习—奇偶数输出
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int QDataType;
typedef struct Node//链式队列的节点
{
QDataType data;
struct Node* next;
}LinkNode;
typedef struct {
LinkNode* front, * rear;
}LinkQuene;
void initLinkQuene(LinkQuene Q)//初始化
{
Q.front = Q.rear = NULL;
}
int EnLinkQuene(LinkQuene *Q, QDataType x)//进队列
{
LinkNode* s = new LinkNode;
s->data = x;
s->next = NULL;
if (Q->rear == NULL) Q->front = Q->rear = s;
else
{
Q->rear->next = s;
Q->rear = s;
}
return 1;
}
int DeLinkQuene(LinkQuene *Q, QDataType x)//出队列
{
if (Q->front == NULL) return 0;
LinkNode* p = Q->front;
x = p->data;
Q->front = p->next;
free(p);
if (Q->front == NULL) Q->rear = NULL;
return x;
}
int EmptyLinkQuene(LinkQuene *Q)//判断队列是否为空,空返回1
{
return Q->front == NULL;
}
int main()
{
LinkQuene Q1, Q2;
LinkQuene *A = new LinkQuene, * B = new LinkQuene;
Q1.front = Q1.rear = NULL;
Q2.front = Q2.rear = NULL;
A = &Q1;
B = &Q2;
int n = 0;
int tep = 0;
scanf("%d", &n);
for (int i = 0;i < n; i++)
{
scanf("%d", &tep);
if (tep % 2 == 1)
{
EnLinkQuene(A, tep);
}
else if (tep % 2 == 0)
{
EnLinkQuene(B, tep);
}
}
while (!EmptyLinkQuene(A) && !EmptyLinkQuene(B))
{
int tep_a=0, tep_b=0;
tep_a = DeLinkQuene(A, tep_a);
tep_b = DeLinkQuene(B, tep_b);
printf("%d %d\n", tep_a, tep_b);
}
return 0;
}
栈和队列练习—括号是否配对
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef char SElemType;
typedef struct
{
SElemType* elem;
int maxSize, top;
}SeqStack;
void InitSeqStack(SeqStack& S)//栈的初始化
{
S.elem = new SElemType[MAXSIZE];
if (S.elem == NULL) exit(1);
S.maxSize = MAXSIZE;
S.top = -1;
}
int pushSeqStack(SeqStack& S, SElemType x)//进栈
{
if (S.top == S.maxSize - 1) return 0;
S.top++;
S.elem[S.top] = x;
return 1;
}
int PopSeqStack(SeqStack& S, SElemType& x)//退栈
{
if (S.top == -1) return 0;
x = S.elem[S.top];
S.top--;
return 1;
}
int GetSeqStack(SeqStack& S, SElemType& x)//获取栈顶元素
{
if (S.top == -1) return 0;
x = S.elem[S.top];
return 1;
}
int l_jug(char s)
{
char ch[3] = { '[','(', '{'};
for (int i = 0; i < 3; i++)
{
if (s == ch[i]) return 0;
}
return 1;
}
int r_jug(char s)
{
char ch[3] = { ']',')', '}' };
for (int i = 0; i < 3; i++)
{
if (s == ch[i]) return 0;
}
return 1;
}
int SeqStackEmpty(SeqStack& S)//判断空栈
{
return S.top == -1;
}
int main()
{
SeqStack S;
InitSeqStack(S);
while (1)
{
char s = 0;
scanf("%c", &s);
if (s == '\n') break;
if (r_jug(s)&&l_jug(s)) continue;
if (!l_jug(s)) pushSeqStack(S, s);
else if (!r_jug(s))
{
char tep = 0;
GetSeqStack(S, tep);
if (s == ')')
{
if (tep == '(') PopSeqStack(S, tep);
else
{
printf("括号不配对\n");
return 0;
}
}
else if (s == ']')
{
if (tep == '[') PopSeqStack(S, tep);
else
{
printf("括号不配对\n");
return 0;
}
}
else if (s == '}')
{
if (tep == '{') PopSeqStack(S, tep);
else
{
printf("括号不配对\n");
return 0;
}
}
}
}
if (SeqStackEmpty(S))
{
printf("括号配对\n");
return 0;
}
else printf("括号不配对\n");
return 0;
}
栈和队列练习—输出学生成绩
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int QElemType;
typedef struct//结构定义
{
QElemType elem[MAXSIZE];
int front;
int rear;
}CircQuene;
void initQuene(CircQuene& Q)//初始化
{//da
Q.front = Q.rear = 0;
}
int EnQuene(CircQuene& Q, QElemType x)//入队列
{
if ((Q.rear + 1) % MAXSIZE == Q.front) return 0;
Q.elem[Q.rear] = x;
Q.rear = (Q.rear + 1) % MAXSIZE;
return 1;
}
int DeQuene(CircQuene &Q, QElemType& x)//出队列
{
if (Q.rear == Q.front) return 0;
x = Q.elem[Q.front];
Q.front = (Q.front + 1) % MAXSIZE;
return 1;
}
int EmptyQuene(CircQuene& Q)//判断是否为空,空返回1
{
return Q.front == Q.rear;
}
int main()
{
CircQuene G1,G2,G3;
initQuene(G1);
initQuene(G2);
initQuene(G3);
int n = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int tep = 0;
scanf("%d", &tep);
getchar();
if (tep < 60) EnQuene(G1, tep);
else if (tep >= 60 && tep < 100) EnQuene(G2, tep);
else if (tep >= 100) EnQuene(G3, tep);
}
printf("60>x:");
while (!EmptyQuene(G1))
{
int tep = 0;
DeQuene(G1, tep);
printf("%d ", tep);
}
printf("\n");
printf("60<=x<100:");
while (!EmptyQuene(G2))
{
int tep = 0;
DeQuene(G2, tep);
printf("%d ", tep);
}
printf("\n");
printf("x>=100:");
while (!EmptyQuene(G3))
{
int tep = 0;
DeQuene(G3, tep);
printf("%d ", tep);
}
printf("\n");
return 0;
}
串练习-串的判等运算
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char s1[20], s2[20];
int l1 = 0, l2 = 0;
while (1)
{
char tep = 0;
scanf("%c", &tep);
if (tep == '\n') break;
s1[l1] = tep;
l1++;
}
while (1)
{
char tep = 0;
scanf("%c", &tep);
if (tep == '\n') break;
s2[l2] = tep;
l2++;
}
if (l1 != l2)
{
printf("not equal\n");
return 0;
}
else
{
for (int i = 0; i < l1; i++)
{
if (s1[i] != s2[i])
{
printf("not equal\n");
return 0;
}
}
printf("equal\n");
}
return 0;
}
串练习-串匹配
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
char* ch;
int maxsize;
int n;
}HString;
void initString(HString& S, int n)
{
if (n < MAXSIZE) n = MAXSIZE;
S.ch = new char[n];
if (S.ch == NULL) exit(1);
S.ch[0] = '\0';
S.n = 0;
S.maxsize = n;
}
void createString(HString& S)
{
int l = 0;
while (1)
{
char tep = 0;
scanf("%c", &tep);
if (tep == '\n') break;
S.ch[l] = tep;
l++;
}
S.n = l;
}
int maxs(HString& P, int j)
{
int max = 0;
for (int i = 1; i < j; i++)
{
int tep = 0;
//计算本次的tep值
for (int k = 0; k < i;k++)
{
tep = i;
if (P.ch[k] != P.ch[j - i + k])
{
tep = 0;
break;
}
}
//
max = tep;
}
return max;
}
void findnext(HString& P, int next[])
{
next[0] = -1;
next[1] = 0;
int j = 2,k = 0;
while (j < P.n)
{
k = maxs(P, j);
next[j] = k;
j++;
}
}
int findfast(HString& S, HString& P, int next[])
{
int i = 0, j = 0;
while (j < P.n && i < S.n)
{
if (j==-1||S.ch[i] == P.ch[j]) i++, j++;
else
{
j = next[j];
}
}
if (j < P.n - 1) return -1;
else return i - j + 1;
}
int main()
{
HString S, P;
initString(S, 1);
initString(P, 1);
createString(S);
createString(P);
int next[MAXSIZE];
findnext(P, next);
int position = findfast(S, P, next);
printf("%d\n", position);
return 0;
}
二叉树遍历输出
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef char TElemType;
typedef struct BTnode
{
TElemType data;
struct BTnode* lchild, * rchild;
}BiTnode, * BinTree;
void createBinTree_pre(BinTree& T)//先序创建二叉树
{
char ch;
scanf("%c", &ch);
if (ch == '#') T = NULL;
else
{
T = new BTnode;
T->data = ch;
createBinTree_pre(T->lchild);
createBinTree_pre(T->rchild);
}
}
int headOrderTraverse(BinTree& T)//先序遍历输出
{
if (T == NULL) return 0;
else
{
printf("%c ", T->data);
headOrderTraverse(T->lchild);
headOrderTraverse(T->rchild);
}
return 1;
}
int InOrderTraverse(BinTree& T)//中序遍历输出
{
if (T == NULL) return 0;
else
{
InOrderTraverse(T->lchild);
printf("%c ", T->data);
InOrderTraverse(T->rchild);
}
return 1;
}
int PostOrderTraverse(BinTree& T)//后序遍历输出
{
if (T == NULL) return 0;
else
{
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
printf("%c ", T->data);
}
return 1;
}
int main()
{
BinTree T;
createBinTree_pre(T);
printf("前序遍历结果:");
headOrderTraverse(T);
printf("\n");
printf("中序遍历结果:");
InOrderTraverse(T);
printf("\n");
printf("后序遍历结果:");
PostOrderTraverse(T);
printf("\n");
return 0;
}
叶子结点数及深度
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef char TElemType;
typedef struct BTnode
{
TElemType data;
struct BTnode* lchild, * rchild;
}BiTnode, * BinTree;
void createBinTree_pre(BinTree& T)//先序创建二叉树
{
char ch;
scanf("%c", &ch);
if (ch == '#') T = NULL;
else
{
T = new BTnode;
T->data = ch;
createBinTree_pre(T->lchild);
createBinTree_pre(T->rchild);
}
}
int headOrderTraverse(BinTree& T)//先序遍历输出
{
if (T == NULL) return 0;
else
{
printf("%c ", T->data);
headOrderTraverse(T->lchild);
headOrderTraverse(T->rchild);
}
return 1;
}
int InOrderTraverse(BinTree& T)//中序遍历输出
{
if (T == NULL) return 0;
else
{
InOrderTraverse(T->lchild);
printf("%c ", T->data);
InOrderTraverse(T->rchild);
}
return 1;
}
int PostOrderTraverse(BinTree& T)//后序遍历输出
{
if (T == NULL) return 0;
else
{
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
printf("%c ", T->data);
}
return 1;
}
int leafnumber(BinTree& T)
{
if (T->lchild == NULL && T->rchild == NULL) return 1;
else if (T->lchild != NULL && T->rchild == NULL) return leafnumber(T->lchild);
else if (T->lchild == NULL && T->rchild != NULL) return leafnumber(T->rchild);
else return leafnumber(T->lchild) + leafnumber(T->rchild);
}
int max(int a, int b)
{
if (a >= b) return a;
else return b;
}
int depthnum(BinTree& T)
{
if (T->lchild == NULL && T->rchild == NULL) return 1;
else if (T->lchild != NULL && T->rchild == NULL) return depthnum(T->lchild)+1;
else if (T->lchild == NULL && T->rchild != NULL) return depthnum(T->rchild)+1;
else return max(depthnum(T->lchild), depthnum(T->rchild)) + 1;
}
int main()
{
BinTree T;
createBinTree_pre(T);
int leafs = leafnumber(T);
int depths = depthnum(T);
printf("leafs=%d\n", leafs);
printf("Depth=%d\n", depths);
return 0;
}
图的存储—邻接表的建立
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
#define maxVertices 30
#define maxEdges 900
#define maxWeight 32767
typedef int Weight;
typedef char Type;
typedef struct {
int numVertices, numEdges;
Type VerticesList[maxVertices];
Weight Edge[maxVertices][maxVertices];
}MGraph;
void createMGraph(MGraph& G, int n)//建立邻接矩阵
{
G.numVertices = n;
G.numEdges = 0;
int i, j, k;
Type val;
Weight cost, e1, e2;
for (int i = 0; i < G.numVertices; i++)//定点表
{
scanf("%c", &val);
getchar();
G.VerticesList[i] = val;
for (j = 0; j < G.numVertices; j++)
{
G.Edge[i][j] = (i == j) ? 0 : maxWeight;//初始化邻接矩阵
}
}
k = 0;
while (k < G.numVertices)
{
while (1)
{
scanf("%d", &e1);
if (e1 == -1)
{
getchar();
break;
}
scanf("%d", &cost);
G.Edge[k][e1] = cost;
G.numEdges++;
}
k++;
}
}
void OutMGraph(MGraph& G)
{
int i, j, k;
k = 0;
while (k < G.numVertices)
{
printf("%c ", G.VerticesList[k]);
for (int i = G.numVertices-1; i >=0; i--)
{
if (G.Edge[k][i] != 0 && G.Edge[k][i] != maxWeight)
{
printf("(%d,%d)%d ", k, i, G.Edge[k][i]);
}
}
k++;
printf("\n");
}
}
int main()
{
MGraph G;
int n = 0;
scanf("%d", &n);
getchar();
createMGraph(G, n);
OutMGraph(G);
return 0;
}
二叉排序树
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int TElemType;
typedef struct BTnode
{
TElemType data;
struct BTnode* lchild, * rchild;
}BiTnode, * BinTree;
void createBiTree(BinTree& T, int val)//创建二叉树
{
if (T == NULL)
{
T = new BTnode;
T->data = val;
T->lchild = NULL;
T->rchild = NULL;
}
else
{
if (val >= T->data) createBiTree(T->rchild, val);
else createBiTree(T->lchild, val);
}
}
BinTree findBiTree(BinTree& T, BinTree& F, int val, int& c)//找到指定值位置
{
if (T->data == val)
{
return T;
}
else if (val > T->data)
{
F = T;
c = 1;
if (T->rchild == NULL) return NULL;
return findBiTree(T->rchild, F, val, c);
}
else if (val < T->data)
{
F = T;
c = 0;
if (T->lchild == NULL) return NULL;
return findBiTree(T->lchild, F, val, c);
}
else if (T == NULL) return NULL;
}
BinTree findmin(BinTree& T, BinTree& F)
{
if (T->lchild == NULL) return T;
else
{
F = T;
return findmin(T->lchild, F);
}
}
int delBiTree(BinTree& T, BinTree& F, int c)
{
BinTree P;
if (T->lchild == NULL && T->rchild == NULL)
{
if (c) F->rchild = NULL;
else F->lchild = NULL;
free(T);
return 1;
}
else if (T->lchild != NULL && T->rchild == NULL)
{
P = T->lchild;
T->data = P->data;
T->lchild = P->lchild;
T->rchild = P->rchild;
free(P);
return 1;
}
else if (T->lchild == NULL && T->rchild != NULL)
{
P = T->rchild;
T->data = P->data;
T->lchild = P->lchild;
T->rchild = P->rchild;
free(P);
return 1;
}
else if (T->lchild != NULL && T->rchild != NULL)
{
F = T;
P = findmin(T->rchild, F);
T->data = P->data;
delBiTree(P, F, 0);
}
}
int InOrderTraverse(BinTree& T)//中序遍历输出
{
if (T == NULL) return 0;
else
{
InOrderTraverse(T->lchild);
printf("%d ", T->data);
InOrderTraverse(T->rchild);
}
return 1;
}
int main()
{
int n, del_val, val;
BinTree T;
T = NULL;
scanf("%d", &n);
printf("原始数据:");
for (int i = 0; i < n; i++)
{
scanf("%d", &val);
printf("%d ", val);
createBiTree(T, val);
}
printf("\n");
scanf("%d", &del_val);
printf("中序遍历结果:");
InOrderTraverse(T);
printf("\n");
printf("删除结点后结果:");
BinTree F = NULL;
int c = 0;
BinTree P = findBiTree(T, F, del_val, c);
if (P == NULL)
{
printf("没有%d结点。\n", del_val);
}
else
{
delBiTree(P, F, c);
InOrderTraverse(T);
printf("\n");
}
return 0;
}
找到两个数
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
static int nums[MAXSIZE] = { 0 };
int hash(int target)
{
int leave = 0;
int a, b;
for (int i = 0; i < sizeof(nums) / 4; i++)
{
leave = target - nums[i];
a = i;
for (int j = 0; j < sizeof(nums) / 4; j++)
{
if (nums[j] == leave)
{
b = j;
printf("%d %d\n", a, b);
return 0;
}
}
}
return 0;
}
void inhash(int n)
{
int tep = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &tep);
nums[i] = tep;
}
}
int main()
{
static int n = 0;
scanf("%d", &n);
inhash(n);
int target = 0;
scanf("%d", &target);
hash(target);
return 0;
}
找出最大的几个数
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
static int nums[MAXSIZE] = { 0 };
int main()
{
int m=0, n=0;
int num[MAXSIZE] = { 0 };
int tep = 0;
int s = 0;
while (s<2)
{
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%d", &tep);
num[i] = tep;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-1; j++)
{
if (num[j] < num[j + 1])
{
tep = num[j];
num[j] = num[j + 1];
num[j + 1] = tep;
}
}
}
for (int i = 0; i < m; i++)
{
printf("%d", num[i]);
if (i == m - 1);
else printf(" ");
}
printf("\n");
s++;
}
return 0;
}