数据结构代码题怎么写(数据结构程序代码)
数据结构代码题?
#include iostream
#include algorithm
#define MAX_SIZE 10000
using namespace std;
void first_way(int *arr, int n)
{
int map[MAX_SIZE] = { 0 };//这里元素最大不超过9999,可以按需求放大一些
for (size_t i = 0; i n; i++)
{
map[arr[i]]++;
}
int main_elem = 0;
for (size_t i = 0; i MAX_SIZE; i++)
{
if (map[main_elem] map[i]) main_elem = i;
}
if (map[main_elem] n / 2) cout main_elem endl;
else cout -1 endl;
}
void sec_way(int *arr, int n)
{
sort(arr, arr + n);
int count = 1, max_count = 1;
int main_elem = arr[0];
for (size_t i = 1; i n; i++)
{
if (arr[i] != arr[i - 1])
{
if (count max_count)
{
max_count = count;
main_elem = arr[i - 1];
}
count = 0;
}
count++;
}
if (count n / 2) cout arr[n - 1] endl;
if (max_count n / 2) cout main_elem endl;
else cout -1 endl;
}
int main()
{
int arr1[8] = { 0, 5, 5, 3, 5, 7, 5, 5 };
int arr2[8] = { 0, 5, 5, 3, 5, 1, 5, 7 };
cout "第一个数组测试:" endl;
first_way(arr1, 8);
sec_way(arr1, 8);
cout "第二个数组测试:" endl;
first_way(arr2, 8);
sec_way(arr2, 8);
? return 0;
}
刚学数据结构,题目不会,给个代码(算法)参考
1
#include "stdio.h"
#define MAXSIZE 32
typedef char datatype;
typedef struct _Node{
datatype data;
_Node *lchild, *rchild;
}Node, *PNode, *PBitTree;
void visit(PNode node){
printf("%c\t", node-data);
}
(1)
void PreOrder(PBitTree root){
if(!root) return;
visit(root);
PreOrder(root-lchild);
PreOrder(root-rchild);
}
void InOrder(PBitTree root){
if(!root) return;
InOrder(root-lchild);
visit(root);
InOrder(root-rchild);
}
void PostOrder(PBitTree root){
if(!root) return;
PostOrder(root-lchild);
PostOrder(root-rchild);
visit(root);
}
(2)
void Display(PBitTree root){
PNode nodeQueue[MAXSIZE]; /*结点队列*/
int levelQueue[MAXSIZE]; /*用于保存结点层次信息的队列*/
int front=0, rear=0, curlevel = 0, bline = 0; /*bline用于判断是否需要换行*/
PNode p = root;
if(p) { /*根结点入队*/
nodeQueue[rear] = p;
levelQueue[rear] = 0;
rear = (rear + 1) % MAXSIZE;
}
while(front != rear) { /*如果队列不空*/
p = nodeQueue[front]; /*获取队首元素*/
bline = levelQueue[front] != curlevel ? 1 : 0; /*判断是否需要换行*/
curlevel = levelQueue[front];
front = (front + 1) % MAXSIZE; /*出队*/
if(bline) printf("\n"); /*如果需要换行,则输出换行符*/
visit(p);
if(p-lchild) { /*如果左孩子不空,将左孩子入队*/
nodeQueue[rear] = p-lchild;
levelQueue[rear] = curlevel + 1;
rear = (rear + 1) % MAXSIZE;
if(p-rchild) { /*如果右孩子不空,将右孩子入队*/
nodeQueue[rear] = p-rchild;
levelQueue[rear] = curlevel + 1;
rear = (rear + 1) % MAXSIZE;
}
}
}
(3)
void Exchange(PBitTree root) {
if(!root) return;
PNode tmp;
Exchange(root-lchild);
Exchange(root-rchild);
tmp = root-lchild;
root-lchild = root-rchild;
root-rchild = tmp;
}
2
typedef char datatype;
typedef struct _listNode{
datatype data;
_listNode *next;
}RQueueNode, *PRQueueNode, *PRQueue;
PRQueue rear;
void InitRQueue( ) { /*初始化队列*/
rear = (PRQueue) malloc(sizeof(RQueueNode)); /*生成附加头结点*/
rear-next = rear; /*设置为循环的空队*/
}
void EnRQueue(datatype x) { /*入队*/
PRQueueNode *newNode;
newNode = (PRQueue) malloc(sizeof(RQueueNode)); //生成新结点
newNode-data = x;
newNode-next = rear-next; /*将新结点链接到队尾*/
rear-next = newNode;
rear = newNode; /*将新结点作为新的队尾*/
}
datatype DeRQueue( ) { /*出队*/
PRQueueNode *front; /*队首指针*/
datatype tmp; /*用于返回队首元素数据*/
assert(rear-next != rear); /*断言队列不空*/
front = rear-next-next;
rear-next-next = front-next;
if(rear == front) rear = rear-next; /*如果出队的是队列中的最后一个结点*/
tmp = front-data;
free(front);
return tmp;
}
c语言数据结构代码题?
#includestdio.h
int main()
{
int s[111]={0};
int p;
int r[111]={0};
int n=0;
do{
scanf("%d",r[n++]);
}while(getchar()!='\n');//回车结束输入,输入n个数
scanf("%d",p);//要截取的数组长度
for(int i=0;ip;i++)
s[i]=r[i];//前p个数暂存在数组s中;
for(int i=p;in;i++)
r[i-p]=r[i];//后n-p个数移至数组前部(左边)
for(int i=n-p,j=0;in;i++,j++)
r[i]=s[j];//将数组s中暂存的数据赋值个数组r的后部(右边);
for(int i=0;in;i++)
printf("%d ",r[i]);
return 0;
}
有没有人能帮我写下这个数据结构的题目啊用C语言写
这个我写过,看看下面有没有你要的,我调试过的
/* 递归法 */
#include stdio.h
#define MAX 100
typedef struct BTNode
{
char data;
struct BTNode *lchild;
struct BTNode *rchild;
} BTNode, *BiTree;
int main()
{
BiTree T=NULL;
printf("\ninput the tree number:");
/* CreateBiTree( T ); */
Creat_BiTree( T);
printf("\nthe preorder is:");
PreOrderTraverse( T );
printf("\nthe pre_traverse is:");
Pre_Traverse( T );
printf("\nthe inorder is:");
InOrderTraverse( T );
printf("\nthe in_traveras is:");
In_Traverse( T );
printf("\nthe postorder is:");
PostOrderTraverse( T );
printf("\nthe post_traverse is:");
Post_Traverse( T );
printf("\nthe levelorder is:");
LevelorderTraverse( T );
printf("\nthe depth of tree is:%d", DepthTree(T));
printf("\nthe 2 nodes of all the tree is:%d", BiTree_2Node(T));
BT_Exchange(T);
printf("\noutput after exchang:");
PreOrderTraverse( T );
printf("\nthe leaf is:%d", Count_Leaf(T));
}
int CreateBiTree(BiTree *T) /* 先序遍历创建一棵二叉树 */
{
char ch ;
ch = getchar();
if(ch == '#')
(*T) = NULL;
else
{
(*T) = (BiTree)malloc(sizeof(BTNode));
(*T)-data = ch;
CreateBiTree( (*T)-lchild );
CreateBiTree( (*T)-rchild );
}
}
int PreOrderTraverse(BiTree T) /* 先序遍历 */
{
if(T)
{
printf("%2c", T-data);
PreOrderTraverse(T-lchild);
PreOrderTraverse(T-rchild);
}
}
int InOrderTraverse(BiTree T) /* 中序遍历 */
{
if(T)
{
InOrderTraverse(T-lchild);
printf("%2c", T-data);
InOrderTraverse(T-rchild);
}
}
int PostOrderTraverse(BiTree T) /* 后序遍历 */
{
if(T)
{
PostOrderTraverse(T-lchild);
PostOrderTraverse(T-rchild);
printf("%2c", T-data);
}
}
int LevelorderTraverse(BiTree T) /* 层次遍历 */
{
BTNode *Queue[MAX], *p;
int front = -1, rear = -1;
rear = (rear+1)%MAX;
Queue[rear] = T;
while( front != rear)
{
front = (front+1) % MAX;
p = Queue[front];
printf("%2c", p-data);
if(p-lchild)
{
rear = (rear+1)%MAX;
Queue[rear] = p-lchild;
}
if(p-rchild)
{
rear = (rear+1)%MAX;
Queue[rear] = p-rchild;
}
}
}
int DepthTree(BiTree T) /* 求树的高度 */
{
int depth1, depth2;
if(T == NULL)
return 0;
else
{
depth1 = DepthTree(T-lchild);
depth2 = DepthTree(T-rchild);
return depth1 depth2 ? depth1+1 : depth2+1;
}
}
int BiTree_2Node(BiTree T) /* 统计结点的度为2的数*/
{
int n1, n2;
if(T == NULL) return 0;
else
{
n1 = BiTree_2Node(T-lchild);
n2 = BiTree_2Node(T-rchild);
if(T-lchild T-rchild )
return 1+n1+n2;
else
return n1+n2;
}
}
int BT_Exchange(BiTree T) /* 交换二叉树的左右结点 */
{
BTNode *p;
if( T != NULL)
{
p = T-lchild;
T-lchild = T-rchild;
T-rchild = p;
BT_Exchange( T-lchild );
BT_Exchange( T-rchild );
}
}
int count=1;
int Count_Leaf(BiTree T) /* 计算树的叶子结点 */
{
if( T == NULL) return 0;
if(T-lchild == NULL T-rchild == NULL)
return count++;
Count_Leaf(T-lchild);
Count_Leaf(T-rchild);
}
/* 非递归法*/
int Creat_BiTree(BiTree *T) /* 非递归创建二叉树*/
{
BTNode *stack[MAX], *p;
int top = -1, j=0, k, i=0;
char ch, str[MAX];
while(ch != '.')
{
ch = getchar();
str[j++] = ch;
}
(*T) = NULL;
ch = str[i];
while(ch != '\0')
{
switch(ch)
{
case '(' : top++ ;
stack[top] = ch;
k = 1;
break;
case ')' : top--;
break;
case ',' : k = 2;
break;
default: p = (BTNode *)malloc(sizeof(BTNode));
p-data = ch;
p-lchild = NULL;
p-rchild = NULL;
}
if( (*T) == NULL)
(*T) = p;
else
{
switch( k )
{
case 1: stack[top]-lchild = p;
break;
case 2: stack[top]-rchild = p;
break;
}
}
ch = str[++i];
}
}
int Pre_Traverse(BiTree T) /* 非递归先序遍历*/
{
BTNode * stack[MAX], *p;
int top = -1;
p = T;
do
{
while( p )
{
printf("%2c ", p-data);
top++;
stack[top] = p;
p = p-lchild;
}
if(top -1)
{
p = stack[top];
top--;
p = p-rchild;
}
} while(top -1 || p);
}
int In_Traverse(BiTree T) /* 中序非递归遍历*/
{
BTNode *stack[MAX], *p;
int top = -1;
p = T;
do
{
while( p )
{
top++;
stack[top] = p;
p = p-lchild;
}
if( top -1)
{
p = stack[top];
printf("%2c ", p-data);
top--;
p = p-rchild;
}
} while(top - 1 || p);
}
int Post_Traverse(BiTree T) /* 后序非递归遍历*/
{
BTNode *stack[MAX], *p, *q;
int top = -1, flag;
p = T;
do
{
while( p )
{
top++;
stack[top] = p;
p = p-lchild;
}
flag = 1;
q = NULL;
while( top -1 flag)
{
p = stack[top];
if(p-rchild == q)
{
printf("%2c ", p-data);
top--;
q = p;
}
else
{
p = p-rchild;
flag = 0;
}
}
} while(top -1);
}