中等——动态数据结构

1.

n个人围成一圈,顺序编号。从第一个人开始从1到m报数,凡报到m的人退出圈子,编程求解最后留下的人的初始编号。
样例输入:(第一行输入)
6 3(两个输入数据之间有空格)
样例输出:(换行输出)
1
#include <stdio.h>
#include <stdlib.h>

#define MAXN 100

int main (void)
{
    int n;
    int m;

    scanf ("%d%d", &n, &m);

    int queue[MAXN + 10] = {0};

    int i;
    for (i = 0; i < n; i ++)
    {
        queue[i] = i + 1;
    }

    int index = 0;
    for (i = 0; i < n; i ++)
    {
        int cnt = 0;

        while (cnt < m)
        {
            while (queue[index] == 0)
            {
                index = (index + 1) % n;

            }

            index = (index + 1) % n;
            cnt ++;
        }

        index --;

        if (index < 0)
        {
            index = n - 1;
        }

        if (i == n - 1)
        {
            printf ("%d\n", queue[index]);
        }

        queue[index] = 0;
    }

    return 0;
}

2.

建立一个链表,使链表中从头到尾的结点数据域依次是一个数组的各个元素的值。程序先建立链表然后再遍历输出(假定链表和数组均有6个整型元素)。
程序运行示例如下:
输入数组6个元素的值。
1 3 5 7 9 11
此链表各个结点的数据域为:1 3 5 7 9 11 
#include <stdio.h>
#include <stdlib.h>

#define MAXN 6

struct Node
{
    int data;
    struct Node *next;
};

struct Head
{
    int length;
    struct Node *next;
};

typedef struct Node *pNode;
typedef struct Head *pHead;

pHead Create (void);
void Insert (pHead pH, int pos, int val);
void Print (pHead pH, pNode p);

int main (void)
{
    printf ("输入数组6个元素的值。\n");

    pHead pH = Create ();

    int num;
    int i;
    for (i = 0; i < MAXN; i ++)
    {
        scanf ("%d", &num);

        Insert (pH, 0, num);
    }

    printf ("此链表各个结点的数据域为:");

    Print (pH, pH -> next);

    return 0;
}

pHead
Create (void)
{
    pHead pH = (pHead)malloc (sizeof (struct Head));

    pH -> length = 0;
    pH -> next = NULL;

    return pH;
}

void
Insert (pHead pH, int pos, int val)
{
    if (pH == NULL || pos < 0 || pos > pH -> length)
    {
        printf ("Error");

        return;
    }

    pNode p = (pNode)malloc (sizeof (struct Node));
    p -> data = val;
    p -> next = NULL;

    pNode pCur = pH -> next;

    if (pos == 0) // else 部分可不写,此处锻炼个人熟练度
    {
        pH -> next = p;
        p -> next = pCur;
    }
    else
    {
        int i;
        for (i = 1; i < pos; i ++)
        {
            pCur = pCur -> next;
        }

        p -> next = pCur -> next;
        pCur -> next = p;
    }

    pH -> length ++;
}

void
Print (pHead pH, pNode p)
{
    if (p -> next != NULL)
    {
        Print (pH, p -> next);
    }

    printf ("%d ", p -> data);
}

3.

在一个有序(按非递减顺序)的链表中插入一个元素为x的结点,使插入后的链表仍然有序(链表数据域为整型数,初始时输入6个元素)。
程序运行示例如下:
输入数组6个元素的值。
12 23 34 45 56 67
此链表各个结点的数据域为:12 23 34 45 56 67 
输入要插入的数据x:36
插入后链表各个结点的数据域为:12 23 34 36 45 56 67 
#include <stdio.h>
#include <stdlib.h>

#define MAXN 6

struct Node
{
    int data;
    struct Node *next;
};

struct Head
{
    int length;
    struct Node *next;
};

typedef struct Node *pNode;
typedef struct Head *pHead;

pHead Create (void);
void Insert (pHead pH, int pos, int val);
int Search (pHead pH, int val);
void Print (pHead pH, pNode p);

int main (void)
{
    printf ("输入数组6个元素的值。\n");

    pHead pH = Create ();

    int num;
    int i;
    for (i = 0; i < MAXN; i ++)
    {
        scanf ("%d", &num);

        Insert (pH, 0, num);
    }

    printf ("此链表各个结点的数据域为:");

    Print (pH, pH -> next);

    int val;
    printf ("\n输入要插入的数据x:");
    scanf ("%d", &val);

    int flag = Search (pH, val);

    printf ("插入后链表各个结点的数据域为:");

    Insert (pH, flag, val);

    Print (pH, pH -> next);

    return 0;
}

pHead
Create (void)
{
    pHead pH = (pHead)malloc (sizeof (struct Head));

    pH -> length = 0;
    pH -> next = NULL;

    return pH;
}

void
Insert (pHead pH, int pos, int val)
{
    if (pH == NULL || pos < 0 || pos > pH -> length)
    {
        printf ("Error");

        return;
    }

    pNode p = (pNode)malloc (sizeof (struct Node));
    p -> data = val;
    p -> next = NULL;

    pNode pCur = pH -> next;

    if (pos == 0)
    {
        pH -> next = p;
        p -> next = pCur;
    }
    else
    {
        int i;
        for (i = 1; i < pos; i ++)
        {
            pCur = pCur -> next;
        }

        p -> next = pCur -> next;
        pCur -> next = p;
    }

    pH -> length ++;
}

int
Search (pHead pH, int val)
{
    if (pH == NULL)
    {
        printf ("Error");
        return 0;
    }

    int pos = 1;
    pNode p = pH -> next;

    while (p -> next != NULL)
    {
        if (p -> data >= val && p -> next -> data <= val)
        {
            return pos;
        }

        p = p -> next;
        pos ++;
    }

    return pH -> length;
}

void
Print (pHead pH, pNode p)
{
    if (p -> next != NULL)
    {
        Print (pH, p -> next);
    }

    printf ("%d ", p -> data);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘的企鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值