1. 2023 期末 第一题 顺序表(8+7=15分)
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
}SqList;
Status InitList_Sq(SqList *L)
{
L->elem=(ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
if(!L->elem)
return ERROR;
L->length=0;
return OK;
}
Status CreateList_Sq(SqList *L, int n)
{
if(n<=0||n>MAXSIZE)
return ERROR;
int i;
for(i=0;i<n;i++)
{
scanf("%d", &L->elem[i]);
}
L->length=n;
return OK;
}
Status DeleteItem(SqList *L,int a,int b)
{
if(a >= b)
{
printf("SqList delete range error.");
return ERROR;
}
int i = 0, k = 0;
for(i = 0;i < L->length;i ++)
{
if(L->elem[i] >b || L->elem[i] < a)
{
L->elem[k ++] = L->elem[i];
}
}
L->length = k;
return OK;
}
void PrintList_Sq(SqList L)
{
int i;
for(i=0;i<L.length;i++)
{
printf("%d ", L.elem[i]);
}
printf("\n");
}
int main()
{
int n;
scanf("%d",&n);
if(n <= 0 || n > MAXSIZE)
{
printf("SqList length error.");
return ERROR;
}
SqList L;
InitList_Sq(&L);
CreateList_Sq(&L, n);
int a, b;
scanf("%d %d",&a, &b);
if(DeleteItem(&L, a, b) == OK)
PrintList_Sq(L);
return 0;
}
2. 2023 期末第二题 单链表基本操作与应用(5+10=15分)
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE 0
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode* next;
}LNode, *LinkList;
Status InitList(LinkList *L)
{
int x,i;
*L = (LinkList)malloc(sizeof(LNode));
if(*L == NULL)
{
printf("memory malloc failure\n");
return ERROR;
}
(*L)->next = NULL;
LinkList r;
r = *L;
for(i=0; ;i++)
{
scanf("%d",&x);
if(x == -1)
break;
LinkList p = (LinkList)malloc(sizeof(LNode));
if(!p)
{
printf("initial failure\n");
return ERROR;
}
p->data = x;
p->next = NULL;
r->next = p;
r = p;
}
return OK;
}
Status DestoryList(LinkList *L)
{
LinkList p;
while(*L)
{
p = (*L)->next;
free(*L);
*L = p;
}
return OK;
}
Status ListEmpty(LinkList L)
{
return (L->next == NULL);
}
Status ClearList(LinkList L)
{
LinkList p, q;
p = L->next;
while (p)
{
q = p->next;
free(p);
p = q;
}
L->next = NULL;
return OK;
}
int ListLength(LinkList L)
{
int total = 0;
LinkList p = L->next;
while(p)
{
total ++;
p = p->next;
}
return total;
}
Status ListTraverse(LinkList L)
{
LinkList p = L->next;
while (p)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return OK;
}
Status ListOperate(LinkList *L)
{
LinkList p = (*L);
if(ListEmpty(*L))
{
LinkList r;
r = (LinkList)malloc(sizeof(LNode));
if(!r) return ERROR;
r->data = 0;
LinkList s;
s = (LinkList)malloc(sizeof(LNode));
if(!s) return ERROR;
s->data = 0;
p->next = r;
r->next = s;
s->next = NULL;
return 0;
}
int max = p->next->data, min = p->next->data;
while(p->next)
{
if(p->next->data > max)
{
max = p->next->data;
}
else if(p->next->data < min)
{
min = p->next->data;
}
p = p->next;
}
LinkList r;
r = (LinkList)malloc(sizeof(LNode));
if(!r) return ERROR;
r->data = min;
r->next = p->next;
p->next = r;
LinkList s;
s = (LinkList)malloc(sizeof(LNode));
if(!s) return ERROR;
s->data = max;
s->next = (*L)->next;
(*L)->next = s;
return 1;
}
int main()
{
LinkList L;
if (InitList(&L) == ERROR)
{
return 0;
}
ListTraverse(L);
int len = ListLength(L);
printf("%d\n",len);
ListOperate(&L);
ListTraverse(L);
DestoryList(&L);
return 0;
}
3. 2023 期末-第三题 队列(5+10=15分)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define null 0
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef struct{
int *base;
int front;
int rear;
}SqQueue;
void InitQueue(SqQueue *Q){
Q->base=(int *)malloc(MAXSIZE*sizeof(int));
Q->front=0;
Q->rear=0;
}
int QueueLength(SqQueue *Q)
{
return (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
}
int EnQueue(SqQueue *Q, int e)
{
if((Q->rear+1)%MAXSIZE==Q->front)
return ERROR;
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%MAXSIZE;
return OK;
}
int DeQueue(SqQueue *Q,int *e)
{
if(Q->front==Q->rear)
return ERROR;
*e=Q->base[Q->front];
Q->front=(Q->front+1)%MAXSIZE;
return OK;
}
int GetHead(SqQueue *Q,int *e)
{
if(Q->front==Q->rear)
return ERROR;
*e=Q->base[Q->front];
return OK;
}
int Baoshu(SqQueue *Q)
{
while(QueueLength(Q) > 1)
{
int bianhao ;
GetHead(Q, &bianhao);
int i;
for(i = 1;i < MAXSIZE;i ++)
{
int e;
DeQueue(Q, &e);
if(i == bianhao)
{
printf("%d ",e);
Baoshu(Q);
}
else
{
EnQueue(Q, e);
}
}
}
return Q->base[Q->front];
}
int main()
{
int n;
int i;
int j;
SqQueue Q;
InitQueue(&Q);
scanf("%d",&n);
for(i=1;i<=n;i++)
EnQueue(&Q, i);
j=Baoshu(&Q);
printf("\n");
printf("%d ",j);
return 0;
}
4. 2023 期末第四题 排序(7+8=15分)
#include <stdio.h>
void bubbleSort(int arr[], int n)
{
int i, j;
for(i = 0 ;i < n - 1; i ++)
{
for(j = 0 ;j < n - i - 1;j ++)
{
if(arr[j] < arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j +1];
arr[j + 1] = temp;
}
}
}
}
void selectionSort(int arr[], int n)
{
int i ,j;
for(i = 0; i < n;i ++)
{
int min = arr[i],min_n = i;
for(j = i ; j < n;j ++)
{
if(arr[j] < min)
{
min = arr[j];
min_n = j;
}
}
if(min_n != i)
{
arr[min_n] = arr[i];
arr[i] = min;
}
}
}
void printfArray(int arr[], int n) {
bubbleSort(arr, n);
printf("Array by bubbleSort are: ");
int i;
for(i=0; i< n; i++){
printf("%d ", arr[i]);
}
printf("\n");
selectionSort(arr, n);
printf("Array by selectionSort are: ");
for(i=0; i< n;i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[100];
int n, k, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printfArray(arr, n);
return 0;
}
5. 2023 期末第五题 二叉树遍历和应用(7+7+6=20分)
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define OVERFLOW 0
#define OK 1
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
} BinTNode, *BTNode;
BinTNode *CreateBinTree()
{
BinTNode *t;
char ch;
ch = getchar();
if (ch == '0')
t = NULL;
else
{
t = (BinTNode *)malloc(sizeof(BinTNode));
if(!t) exit(OVERFLOW);
t->data = ch;
t->lchild = CreateBinTree();
t->rchild = CreateBinTree();
}
return t;
}
void preorder(BTNode t)
{
if (t != NULL)
{
printf("%3c", t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(BTNode t)
{
if(t)
{
inorder(t->lchild);
printf("%3c",t->data);
inorder(t->rchild);
}
}
int Leaf(BTNode t)
{
if(!t) return 0;
else if(t->rchild == NULL && t->lchild == NULL) return 1;
else
return Leaf(t->lchild) + Leaf(t->rchild);
}
int Max(BTNode t)
{
if(!t) return 0;
int submax = Max(t->rchild) > Max(t->lchild) ? Max(t->rchild) : Max(t->lchild);
return submax > t->data ? submax: t->data;
}
int main()
{
BTNode t = NULL;
t = CreateBinTree();
preorder(t);
printf("\n");
inorder(t);
printf("\n");
printf("%d\n", Leaf(t));
printf("%c\n", Max(t));
}
6.期末第六题 应用题-社交网络下谣言传播分析(一) (5+5=10分)
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
int info;
} ArcNode;
typedef struct VNode
{
int data;
ArcNode *firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct ALGraph
{
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
void CreateUDN(ALGraph *G)
{
scanf("%d %d",&G->vexnum, &G->arcnum);
int i;
for(i = 1;i <= G->vexnum ;i ++)
{
G->vertices[i].data = i;
G->vertices[i].firstarc = NULL;
}
for(i = 0 ;i < G->arcnum ;i ++)
{
int v1, v2,weight;
getchar();
scanf("%d %d %d",&v1, &v2, &weight);
ArcNode *p1;
p1 = (ArcNode*)malloc(sizeof(ArcNode));
if(!p1) return ;
p1->adjvex = v2;
p1->info = weight;
p1->nextarc = G->vertices[v1].firstarc;
G->vertices[v1].firstarc = p1;
ArcNode *p2;
p2 = (ArcNode*)malloc(sizeof(ArcNode));
if(!p2) return;
p2->adjvex = v1;
p2->info = weight;
p2->nextarc = G->vertices[v2].firstarc;
G->vertices[v2].firstarc = p2;
}
}
int VexCount(ALGraph G, int v)
{
ArcNode *p = G.vertices[v].firstarc;
int sum = 0;
while(p)
{
sum += p->info;
p = p->nextarc;
}
return sum;
}
int main()
{
ALGraph G;
int choice;
static int count = 0;
CreateUDN(&G);
int i;
for(i=0; i<G.vexnum; i++)
{
printf("%d ", VexCount(G, i+1));
}
printf("\n");
return 0;
}
7.2023 期末第六题 应用题-社交网络下谣言传播分析(二) (5+5=10分)
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
#define TH 4
#define OVERFLOW -1
#define ERROR 0
#define OK 1
typedef int Status;
typedef int QElemType;
typedef struct ArcCell
{
unsigned int weight;
}AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct MGraph
{
AdjMatrix arcs;
int vexnum, arcnum;
}MGraph;
typedef struct QNode
{
int data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
int visited[MAX_VERTEX_NUM];
int credit[MAX_VERTEX_NUM];
int times[MAX_VERTEX_NUM];
Status InitQueue(LinkQueue *Q)
{
(*Q).front=(*Q).rear=(QueuePtr)malloc(sizeof(QNode));
if(!(*Q).front)
exit(OVERFLOW);
(*Q).front->next=NULL;
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
return(Q.front==Q.rear);
}
Status EnQueue(LinkQueue *Q,QElemType e)
{
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
if(!p)
exit(OVERFLOW);
p->data=e;
p->next=NULL;
(*Q).rear->next=p;
(*Q).rear=p;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e)
{
QueuePtr p;
if((*Q).front==(*Q).rear)
return ERROR;
p=(*Q).front->next;
*e=p->data;
(*Q).front->next=p->next;
if((*Q).rear==p)
(*Q).rear=(*Q).front;
free(p);
return OK;
}
void CreateUDN(MGraph *G)
{
scanf("%d %d",&G->vexnum, &G->arcnum);
int i, j;
for(i = 0; i < G->vexnum; i++)
{
for(j = 0; j < G->vexnum; j++)
{
G->arcs[i][j].weight = 0;
}
}
for(i = 0; i < G->arcnum ;i ++)
{
int v1, v2, weight;
getchar();
scanf("%d %d %d",&v1, &v2, &weight);
G->arcs[v1 - 1][v2 - 1].weight = weight;
G->arcs[v2 - 1][v1 - 1].weight = weight;
}
}
void BFSGraph(MGraph G, int v)
{
int i, j;
LinkQueue Q;
InitQueue(&Q);
for(i = 0; i < G.vexnum; i++)
{
visited[i] = 0;
credit[i] = 0;
times[i] = 0;
}
visited[v] = 1;
credit[v] = 0;
times[v] = 1;
printf("%d ", v + 1);
EnQueue(&Q, v);
while(!QueueEmpty(Q))
{
int u;
DeQueue(&Q, &u);
for(j = 1; j < G.vexnum; j++)
{
if(G.arcs[u][j].weight != 0 && !visited[j])
{
credit[j] += G.arcs[u][j].weight;
if (credit[j] >= TH)
{
visited[j] = 1;
times[j] = times[u] + 1;
EnQueue(&Q, j);
printf("%d ", j + 1);
}
}
}
}
}
int main()
{
MGraph G;
int i,j;
CreateUDN(&G);
for(i=0; i< G.vexnum; i++){
for(j=0; j< G.vexnum; j++)
printf("%d ", G.arcs[i][j].weight);
printf("\n");
}
BFSGraph(G, 0);
printf("\n");
for(i = 0; i< G.vexnum; i++)
{
printf("%d ", credit[i]);
}
printf("\n");
for(i = 0; i< G.vexnum; i++)
{
printf("%d ", times[i]);
}
printf("\n");
return 0;
}