数据结构期末

函数题

//6-1 顺序表的删除操作
int del(SeqList *sq,int i)  
{  
    int j;  i--;
  	if ((i<0) || (i > sq->n-1)) return -1;
    else { 
        for (j=i+1;j<= sq->n-1;j++) sq->a[j-1]=sq->a[j];  
     	sq->n--;
		return 1;  
    }
} 
//6-2 求链式表的表长
int Length(List L){
    int n=0;
    List p = L;
    while(p!=NULL){
        n++;
        p=p->Next;
    }
    return n;
}

//6-3 递增的整数序列链表的插入
List Insert( List L, ElementType X ){
List tail=(List)malloc(sizeof(struct Node));
// List tail=(List)malloc(sizeof(struct Node));
tail->Data=X;
tail->Next=NULL;
List head=L;
 while(L->Next!=NULL &&L->Next->Data<=X){
 	L=L->Next;
 }
     tail->Next=L->Next;
    L->Next=tail;
    return head;
}
//6-4 查找中间结点
Node* find_middle(Node* head)
{
     int count = 0;
     Node *p = head;
      if(p == NULL)
          return p;
      while(p->next != NULL)
      {
          count++;
          p = p->next;
      }
      p = head;
      count = count/2;
      while(count > 0)
      {
            p = p->next;
            count--;
      }
      return p;
}
//6-5 求链表的倒数第m个元素
ElementType Find( List L, int m ){
    List L1,L2;
    L1=L->Next;
    L2=L->Next;
    int i;
    for(i = 0;i < m;i++){
        if(L1==NULL){
            return ERROR;
        }
        L1=L1->Next;
    }
    while(L1){
        L1=L1->Next;
        L2=L2->Next;
    }
    return L2->Data;
}
//6-6 链表逆置
struct ListNode *reverse( struct ListNode *head )//没有头结点
{
    struct ListNode *p,*q;
    q=(struct ListNode *)malloc(sizeof(struct ListNode));
    
    if(head==NULL)
        return ;
    p=head->next;
    head->next=NULL;
    
    while(p!=NULL){
        q=p;
        p=p->next;
        q->next=head;
        head=q;
    }
    
    return head;
}
//6-7 两个有序链表序列的合并
List Merge( List L1, List L2 ){
    List a,c,b,l;
    l=(List)malloc(sizeof(PtrToNode));
    a=L1->Next,b=L2->Next,c=l;
    while(a&&b){
        if(a->Data>b->Data){
            c->Next=b;
            b=b->Next;
        }
        else{
            c->Next=a;
            a=a->Next;
        }
        c=c->Next;
    }
    if(a) c->Next=a;
    if(b) c->Next=b;
    L1->Next=NULL;
    L2->Next=NULL;
    return l;
}
//6-8 另类堆栈
bool Push (Stack S, ElementType X) {
    if (S -> Top == S -> MaxSize) {
        printf("Stack Full\n");
        return false;
    }
    S -> Data[(S -> Top)++] = X;
    return true;
}
ElementType Pop(Stack S) {
    ElementType res;
    if (S -> Top == 0) {
        printf("Stack Empty\n");
        return ERROR;
    }
    res = S -> Data[--(S -> Top)];
    return res;
}
//6-9 另类循环队列
bool AddQ(Queue Q, ElementType X)
{
	if (Q->Count == Q->MaxSize)
	{
		printf("Queue Full\n");
		return false;
	}
	Q->Count++;
	Q->Data[(Q->Front + Q->Count) % Q->MaxSize] = X;
	return true;
}
ElementType DeleteQ(Queue Q)
{
	if (Q->Count == 0)
	{
		printf("Queue Empty\n");
		return ERROR;
	}
	Q->Count--;
	Q->Front = (Q->Front+1) % Q->MaxSize;
	return Q->Data[Q->Front];
}
//6-1 求二叉树高度
int GetHeight( BinTree BT ){
    if (BT==NULL)
        return 0;
    int h1=GetHeight(BT->Left);
    int h2=GetHeight(BT->Right);
    return h1>h2?h1+1:h2+1;
}
//6-2 先序输出叶结点
void PreorderPrintLeaves( BinTree BT ){
    if(BT==NULL)
        return;
    if(BT->Left==NULL && BT->Right==NULL)
        printf(" %c",BT->Data);
    PreorderPrintLeaves(BT->Left);
    PreorderPrintLeaves(BT->Right);
}
//6-3 二叉树的遍历
void InorderTraversal( BinTree BT ){
    if(BT){
        InorderTraversal(BT->Left);
        printf(" %c",BT->Data);
        InorderTraversal(BT->Right);
        
    }
}
void PreorderTraversal( BinTree BT ){
    if(BT){
        printf(" %c",BT->Data);
        PreorderTraversal(BT->Left);
        PreorderTraversal(BT->Right);
    }
    
}
void PostorderTraversal( BinTree BT ){
    if(BT){
        PostorderTraversal(BT->Left);
        PostorderTraversal(BT->Right);
        printf(" %c",BT->Data);
    }
}

void LevelorderTraversal( BinTree BT ){
    
    BinTree T[1000];
    int front=0,rear=0;
    
    if(BT){
        T[rear++]=BT;
        while(front!=rear){
            BT=T[front++];
            printf(" %c",BT->Data);
            if(BT->Left)
                T[rear++]=BT->Left;
            if(BT->Right)
                T[rear++]=BT->Right;
        }
    }
    
}
//6-4 统计二叉树结点个数
int NodeCount(BiTree T)
{
    int cnt = 0;
    if(T)
    {
        cnt++;
        cnt += NodeCount(T->lchild);
        cnt += NodeCount(T->rchild);
    }
    return cnt;
}
//6-5 统计二叉树度为1的结点个数
int NodeCount(BiTree T)
{
    if(!T)
        return 0;
    if((T->lchild && !T->rchild) || (!T->lchild && T->rchild))
    {
        return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;   //思考为什么要 +1 ?
    }
    return NodeCount(T->lchild) + NodeCount(T->rchild);
}
//6-15 求采用邻接矩阵作为存储结构的无向图各顶点的度
void degree(MGraph G)
{
    for (int i = 0; i < G.vexnum; i++) //图的当前顶点数
    {
        int c = 0;
        for (int j = 0; j < G.vexnum; j++)
        {
            if (G.arcs[j][i] != 0) //邻接矩阵
                c++;
        }
        printf("%c:%d\n", G.vexs[i], c); //存放顶点的一维数组
    }
}
//6-16 求采用邻接矩阵作为存储结构的有向图各顶点的入度
void indegree(MGraph G)
{
    for (int i = 0; i < G.vexnum;i++) 
    {
        int cnt=0;
        for (int j = 0; j < G.vexnum;j++)
        {                       
             if(G.arcs[j][i] == 1)
                 cnt++;
        }
        printf("%c:%d\n", G.vexs[i], cnt); 
    }
}
//6-17 邻接矩阵存储图的深度优先遍历
void DFS(MGraph Graph, Vertex V, void (*Visit)(Vertex))
{
    Visit(V);
    Visited[V] = 1;
    for (int i = 0; i < Graph->Nv; i++)
    {
        if (Graph->G[V][i] == 1)
        {
            if (Visited[i] == false)
            {
                // Visited[i] = 1;
                DFS(Graph, i, Visit);
            }
        }
    }
}
//6-18 图的深度遍历-邻接表实现
void DFS(ALGraph *G, int i)
{
    printf(" %d", i);
    visited[i] = 1;
    struct ArcNode *w;
    for (w = G->vertices[i].firstarc; w; w = w->nextarc)
    {
        if (visited[w->adjvex] == 0)
            DFS(G, w->adjvex);
    }
}
//6-19 图的广度遍历-邻接矩阵实现
void BFS(MGraph G,Vertex i)
{
  int q[MaxVertexNum],front=0,rear=0;
  printf(" %d",i);
  visited[i]=1;
  q[rear++]=i;
  while(front!=rear)
  {
    int now=q[front++];
    for(i=0;i<G.vexnum;i++)
    {
      if(G.arcs[now][i]==1&&visited[i]==0)
      {
        printf(" %d",i);
        visited[i]=1;
        q[rear++]=i;
      }
    }
  }
}
//6-20 邻接表存储图的广度优先遍历
void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ){
	Visit(S);
	Visited[S] = true;
	Vertex queue[105];
	int front = 0,rear = 0;
	queue[rear++] = S; 
	PtrToAdjVNode p;
	while(front<rear){
		Vertex x = queue[front];
		front++;
		p = Graph->G[x].FirstEdge;
		while(p){
			Vertex v = p->AdjV;
			if(!Visited[v]){
				Visit(v);
				Visited[v] = true;
				queue[rear++] = v;
			}
			p = p->Next;
		} 
	}
}

编程题

//7-1 字符串的冒泡排序
#include <stdio.h>
#include <string.h>
void BubbleSort_For_Strings(char(*Array)[20],int Line,int Times) 
{
    int i,j;
    for(i=0;i<Times;i++)
    {
        for(j=0;j<Line-i-1;j++)
        {
            if(strcmp(Array+j,Array+j+1)>0)
            {
                char temp[20];
                strcpy(temp,Array+j);
                strcpy(Array+j,Array+j+1);
                strcpy(Array+j+1,temp);
            }
        }
    }
}

int main()
{
    int N,K,i=0,j=0;
    char a[100][20]={0};
    scanf("%d %d",&N,&K);
    getchar();  
    for(i=0;i<N;i++)
    {
        j=-1;
        do
        {
            j++;
            scanf("%c",&a[i][j]);
        }while(a[i][j]!='\n');
    }
    BubbleSort_For_Strings(a,N,K);
    for(i=0;i<N;i++)
    {
        j=-1;
        do
        {
            j++;
            printf("%c",a[i][j]);
        }while(a[i][j]!='\n');
    }
    return 0;
}
//7-2 两个有序链表序列的交集
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}node;
struct node *createList();
int main()
{
	node *L1 = createList();
	node *L2 = createList();
	int cnt = 0,num = 0;
	node *p1 = L1;
	node *p2 = L2; 
	while(p1!=NULL&&p2!=NULL)
	{
		if(p1->data<p2->data)
		{
			p1 = p1->next;
		}
		else if(p1->data == p2->data)
		{
			num++;		
			if(cnt == 0) 
			{
				cnt = 1;
				printf("%d",p1->data);
			}
			else
			{
				printf(" %d",p1->data);
			}
			p1 = p1->next;
			p2 = p2->next;
		}
		else
		{
			p2 = p2->next;
		}
	}
	if(num==0)
		printf("NULL");	
	return 0;
}
struct node *createList()
{
	node *head = NULL;
	head = (node*)malloc(sizeof(node));
	head->next = NULL;
	node *p = head;
	int x;
	scanf("%d",&x);
	while(x!= -1)
	{
		node *q =NULL;
		q = (node*)malloc(sizeof(node));
		q->data = x;
		p->next = q;
		p = q;
		q->next = NULL;
		scanf("%d",&x);
	}
	return head->next;
}
//7-3 括号匹配
#include <iostream>
#include<stack>
#include<string>
using namespace std;
bool check(string s);
int main()
{
	string s;
	while(getline(cin,s)){
		cout<<(check(s)? "yes" : "no")<<endl;	
	}
	return 0;
}
bool check(string s){
	stack<char> st;
	for(int i=0;i<s.size();i++){
		char c=s[i];
		if(c=='('||c=='['||c=='{'){
			st.push(c);
		} 
		else if(c==')'||c==']'||c=='}'){
			if(st.empty()){
				return false;
			}
			char x=st.top();
			st.pop(); 
			if(c==')'&&x!='('||
			c==']'&&x!='['||
			c=='}'&&x!='{') {
				return false;
			}
		}
	}
	return st.empty();
}
//7-4 表达式求值
#include <cctype>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
    string st;
    while (cin >> st) {
        stack<string> s;
        stack<string> ope;
        string temp;
        for (char ct : st) {
            if (isdigit(ct)) {
                temp = ct;
                s.push(temp);
            }
            else {
                double left, right, res;
                switch (ct) {
                    case '+':
                        right = stod(s.top());
                        s.pop();
                        left = stod(s.top());
                        s.pop();
                        res = left + right;
                        temp = to_string(res);
                        s.push(temp);
                        break;
                    case '-':
                        right = stod(s.top());
                        s.pop();
                        left = stod(s.top());
                        s.pop();
                        res = left - right;
                        temp = to_string(res);
                        s.push(temp);
                        break;
                    case '*':
                        right = stod(s.top());
                        s.pop();
                        left = stod(s.top());
                        s.pop();
                        res = left * right;
                        temp = to_string(res);
                        s.push(temp);
                        break;
                    case '/':
                        right = stod(s.top());
                        s.pop();
                        left = stod(s.top());
                        s.pop();
                        res = left / right;
                        temp = to_string(res);
                        s.push(temp);
                        break;
                }
            }
        }
        double out = stod(s.top());
        printf("%.2lf\n", out);
    }
    return 0;
}
//7-5 队的基本操作
#include<stdio.h>
#include<stdlib.h>
struct Queue{
    int data;
    struct Queue* next;
}*queue;

struct Queue *createQueue(); 
void push(int num);        
void pop();             
int main()
{
    queue = createQueue();
    int N,num;
    scanf("%d",&N);
    for(int z=0;z<N;z++){
        scanf("%d",&num);
        if(num==0) pop();
        else push(num);
    }
    putchar('\n');
    while (queue->data!=0){
        printf("%d ",queue->data);
        queue->data = 0;
        queue = queue->next;
    }
    return 0;
}
struct Queue *createQueue()
{
    struct Queue *head,*body;
    head = (struct Queue *) malloc(sizeof(struct Queue));
    head->data = 0; 
    body = head;
    for(int z=1;z<10;z++){
        body->next = (struct Queue *) malloc(sizeof(struct Queue));
        body = body->next;
        body->data = 0;
    }
    body->next = head;
    return head;
}
void push(int num)
{
    struct Queue *head = queue;
    for(int z=0;z<9;z++){
        if(head->data==0){
            head->data = num;
            return;
        }
        head = head->next;
    }
    printf("FULL ");
}
void pop()
{
    if(queue->data==0) printf("EMPTY ");
    else
    {
        printf("%d ",queue->data);
        queue->data = 0;
        queue = queue->next;
    }
}
//7-6 银行业务队列简单模拟
#include<bits/stdc++.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef long long ll;
struct LinkQueue{
    int *base;
    int front,last;
};
void InitQueue(LinkQueue& q){
    q.base = (int*)malloc(1007*sizeof(int));
    q.front = q.last = 0;
}
void push(LinkQueue&q,int e){
    q.base[q.last++] = e;
}
void pop(LinkQueue&q){
    q.front++;
}
int GetTop(LinkQueue&q){
    return q.base[q.front];
}
int EmptyQueue(LinkQueue&q){
    if(q.front==q.last) return true;
    return false;
}
int main()
{
    //freopen("i.txt","r",stdin);
    int n;
    LinkQueue q1,q2;
    scanf("%d",&n);
    InitQueue(q1);InitQueue(q2);
    for(int i=1;i<=n;i++){
        int e;
        scanf("%d",&e);
        if(e&1){
            push(q1,e);
        }else push(q2,e);
    }
    int i=0;
    while(!EmptyQueue(q1)||!EmptyQueue(q2)){
        if(!EmptyQueue(q1)){
            if(i++) printf(" ");
            cout<<GetTop(q1);
            pop(q1);
        }
        if(!EmptyQueue(q1)){
            if(i++) printf(" ");
            cout<<GetTop(q1);
            pop(q1);
        }
        if(!EmptyQueue(q2)){
            if(i++) printf(" ");
            cout<<GetTop(q2);
            pop(q2);
        }
    }
}
//7-7 h0181. 约瑟夫问题
#include<iostream>
using namespace std;
const int MAX = 305;
int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) break;
		int arr[MAX] = { 0 };
		for (int i = 1; i <= n; i++) arr[i] = 1;
		int sum = n, cur = 1, count = 0;
		while (sum > 1) {
			if (arr[cur] == 1) {
				count++;
				if (count == m) {
					arr[cur] = 0;
					count = 0;
					sum--;
				}
			}
			cur++;
			cur = (cur == n + 1) ? 1 : cur;
		}
		for (int i = 1; i <= n; i++) {
			if (arr[i] == 1) {
				printf("%d\n", i);
				break;
			}
		}
	}
	return 0;
}
//7-8 列出叶结点
#include <stdio.h>
#include <stdlib.h>
typedef struct TNode *Tree;
struct TNode
{
    int data;
    struct TNode *Left;
    struct TNode *Right;
};
int flag[10]; 
struct Node
{
    int left, right;
}num[10];

Tree Build(int node)
{
    Tree t = (Tree) malloc(sizeof(struct TNode));
    t->data = node;
    t->Left = t->Right = NULL;
    if(num[node].left != 100) t->Left = Build(num[node].left);
    if(num[node].right != 100) t->Right = Build(num[node].right);
    return t;
}

void Print(Tree T)
{
    if(T == NULL) return;
    Tree queue[10];
    int left=0, right=0;
    queue[right++] = T;
    int cnt = 0; 
    while (left < right)
    {
        Tree t = queue[left++];
        if(t->Left==NULL && t->Right==NULL) {
            if(cnt == 0) printf("%d", t->data);
            else printf(" %d", t->data);
            cnt++;
        }
        if (t->Left) queue[right++] = t->Left;
        if (t->Right) queue[right++] = t->Right;
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    getchar();
    for(int i=0; i<n; i++)
    {
        char x, y;
        scanf("%c %c",&x, &y);
        getchar();
        if(x == '-') num[i].left = 100;
        else
        {
            num[i].left = x - '0';
            flag[x - '0'] = 1;
        }

        if(y == '-') num[i].right = 100;
        else
        {
            num[i].right = y - '0';
            flag[y - '0'] = 1;
        }
    }
    int node;
    for(int i=0; i<n; i++)
    {
        if(flag[i] == 0) node = i;
    }
    Tree t = Build(node);
    Print(t);
    return 0;
}
//7-9 还原二叉树
#include<iostream>
using namespace std;
typedef struct BiTNode{
	char c;
	struct BiTNode *lchild;
	struct BiTNode *rchild;
}BiTNode,*BiTree;
BiTree Build(char *pre,char *in,int n){
	if(n <= 0)
		return NULL;
	BiTree T = new BiTNode;
	T->c = *pre; 
	char *p = in;
	while(p){
		if(*p == *pre)
			break;
		p++;
	}
	int len = p - in; 
	T->lchild = Build(pre + 1,in,len);
	T->rchild = Build(pre + 1 + len,p+1,n - len - 1);
	return T;
}
int Hight(BiTree T){
	if(!T)
		return 0;
	int m = Hight(T->lchild);
	int n = Hight(T->rchild);
	if(m > n)
		return ++m;
	else
		return ++n;
}
int main(){
	int n,h;
	char pre[51],in[51];
	BiTree T;
	scanf("%d%s%s",&n,&pre,&in);
	T = Build(pre,in,n);
	h = Hight(T);
	printf("%d\n",h);
	return 0;
}
//7-10 插入排序
#include<stdio.h>
int main()
{
    int n;
   scanf("%d",&n);
    int a[n];
    for(int i=0;i<n;i++)
    {
       scanf("%d",&a[i]);
    }
    for(int i=1;i<n;i++)
    {
        int min=a[i];
        int j=i-1;
        for(j=i-1;j>=0;j--)
        {
            if(min<a[j])
            {
                a[j+1]=a[j];
            }
            else
            {
                break;
            }
        }
        a[j+1]=min;
        for(int k=0;k<n;k++)
            {
                printf("%d",a[k]);
                if(k<n-1)
                {
                   printf(" ");
                }
            }
        if(i<n-1)
        {
            printf("\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值