RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-381645

Ростислав Юшкевич's questions

Martin Hope
Ростислав Юшкевич
Asked: 2020-05-20 09:01:27 +0000 UTC

组合函数用于删除图的边和节点

  • 0

有必要创建图的邻接列表并从中删除某些边(在 0 和 1、2 和 3、3 和 8 之间)和编号为 5 的节点。

如何实现删除功能?

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
//задание 3

struct adjlistnode //список смежности узла
{
    int dest;
    struct adjlistnode* next;
};
struct adjlist //список смежности
{
    struct adjlistnode* head;
};
struct Graph
{
    int V;
    struct adjlist* array;
};
struct adjlistnode* newadjlistnode(int dest);
struct Graph* creategraph(int V);
void addedge(struct Graph* graph, int src, int dest);
void printgraph(struct Graph* graph);
void deleteedge(struct Graph* graph, int src, int dest);
void deletevertex(struct Graph* graph, int src, int dest);

int main()
{
    setlocale(LC_ALL, "russian");
    int V = 8;
    struct Graph* graph = creategraph(V);
    addedge(graph, 0, 1);
    addedge(graph, 1, 2);
    addedge(graph, 2, 3);
    addedge(graph, 3, 8);
    addedge(graph, 8, 4);
    addedge(graph, 4, 6);
    addedge(graph, 6, 7);
    addedge(graph, 7, 0);
    addedge(graph, 0, 6);
    addedge(graph, 1, 6);
    addedge(graph, 1, 5);
    addedge(graph, 2, 5);
    addedge(graph, 3, 5);
    addedge(graph, 3, 4);
    addedge(graph, 4, 5);
    addedge(graph, 5, 6);
    printgraph(graph);
    return 0;
}

struct adjlistnode* newadjlistnode(int dest)//создание узла нового списка смежности
{
    struct adjlistnode* newnode = new adjlistnode;
    newnode->dest = dest;
    newnode->next = NULL;
    return newnode;
}
struct Graph* creategraph(int V)
{
    struct Graph* graph = new Graph;
    graph->V = V;
    graph->array = new adjlist; // создание массива списков смежности
    int i; //инициализация каждого списка смежности как пустого
    for (i = 0; i < V; ++i)
    {
        graph->array[i].head = NULL;
    }
    return graph;
}
void addedge(struct Graph* graph, int src, int dest)
{
    // + ребро из src в построение. Новый узел + в список смежности. Список src. Узел + в начале
    struct adjlistnode* newnode = newadjlistnode(dest);
    newnode->next = graph->array[src].head;
    graph->array[src].head = newnode;
    newnode = newadjlistnode(src);
    newnode->next = graph->array[dest].head;
    graph->array[dest].head = newnode;
}
void printgraph(struct Graph* graph)
{
    int v;
    for (v = 0; v < graph->V; ++v)
    {
        struct adjlistnode* pCrawl = graph->array[v].head;
        printf("\nСписок смежности вершины %d\n head", v);
        while (pCrawl)
        {
            printf("-> %d", pCrawl->dest);
            pCrawl = pCrawl->next;
        }
        printf("\n");
    }
}
c++
  • 1 个回答
  • 10 Views
Martin Hope
Ростислав Юшкевич
Asked: 2020-05-06 07:44:30 +0000 UTC

排序前打印数组元素的索引

  • 0

按降序对整数数组 X=(x1,x2,..,xn) 进行排序。按照元素最初的顺序打印原始数组元素的索引。打印数组中最大和最小元素的索引。这是代码:由于某种原因,如果您在排序之前查找数组的最大和最小元素,那么在它之后,数组将被第一个元素填充。以及如何以原始顺序显示索引?先感谢您

   #include <iostream>
#include <stdlib.h>
#include <stdio.h>

void shellsort(int* a, int leng);

int main()
{
    int n, i, j = 0;
    printf("Enter size:\n");
    scanf_s("%d", &n);
    printf("Enter elements: \n");
    int* mas = new int[n];
    for (i = 0; i < n; i++)
    {
        scanf_s("%d", &mas[i]);
    }
    int max = mas[0];
    int d = 0;
    for (i = 0; i < n; i++)
    {
        if (max < mas[i])
        {
            mas[i] = max;
            d = i;
        }
    }
    printf("Max index is: %d\n", d);
    int min = mas[0];
    int e = 0;
    for (i = 0; i < n; i++)
    {
        if (min > mas[i])
        {
            mas[i] = min;
            e = i;
        }
    }
    printf("Min index is: %d\n", e);
    shellsort(mas, n);
    printf("\n");

    delete[]mas;
}

void shellsort(int* a, int leng)
{
    int k = 0, i, j, p, temp, step;
    int* gap = new int[leng];
    gap[0] = leng / 2;
    while (gap[k] > 1)
    {
        k++;
        gap[k] = gap[k - 1] / 2;
    }
    for (i = 0; i <= k; i++)
    {
        step = gap[i];
        for (j = step; j < leng; j++)
        {
            temp = a[j];
            p = j - step;
            while (p >= 0 && temp > a[p])
            {
                a[p + step] = a[p];
                p = p - step;
            }
            a[p + step] = temp;
        }
    }
    printf("\n");
    for (i = 0; i < leng; i++)
    {
        printf("%d", a[i]);
    }
}
c++
  • 1 个回答
  • 10 Views
Martin Hope
Ростислав Юшкевич
Asked: 2020-04-08 03:19:37 +0000 UTC

运行时检查失败 #2。号码损坏

  • 1

运行时检查失败 #2 - 变量“编号”周围的堆栈已损坏。我不明白错误可能发生在哪里:我正在用 C 编写一个列表。结果,指针将 NULL 返回给 print 函数,因此不会打印列表。提前致谢

#include <iostream>
#include <string.h>

struct avia
{
    int number;
    float time;
    char destination;
    avia* next;
};
void instructions();
void createlist(avia* pbegin);
void insert(avia** elem, int number, float time, char destination);
int deleteelem(avia** elem, int number);
void printlist(avia* current);
void deletelist(avia* current);
int isempty(avia* elem);
int main()
{
    avia* head=NULL;
    int number;
    int choice;
    float time;
    char destination;
    instructions();
    scanf_s("%d", &choice);
    while (choice != 6)
    {
        switch (choice)
        {
        case 1:
            createlist(head);
            break;
        case 2:
            printf("Enter a number: \n");
            scanf_s("%d", &number);
            printf("Enter time:\n");
            scanf_s("%f", &time);
            printf("Enter destination:\n");
            scanf_s("%s", &destination, 30);
            insert(&head, number, time, destination);
            printlist(head);
            break;
        case 3:
            if (!isempty(head))
            {
                printf("Enter a number you want to delete\n");
                scanf_s("%d", &number);
                if (deleteelem(&head, number))
                {
                    printf("Object deleted");
                    printlist(head);
                }
                else
                {
                    printf("Number not found");
                }
            }
            else
            {
                printf("The list is empty\n");
            }
            break;
        case 4:
            if (!isempty(head))
            {
                deletelist(head);
            }
            else
            {
                printf("The list is empty\n");
            }
            break;
        case 5:
            if (!isempty(head))
            {
                printlist(head);
            }
            else
            {
                printf("The list is empty\n");
            }
            break;
        default:
            printf("Invalid choice\n");
            instructions();
            break;
        }
        printf("?\n");
        scanf_s("%d", &choice);
    }
    printf("End of run\n");
    return 0;
}
void instructions()
{
    printf("Enter choice:\n1 create an empty list\n 2 insert an element into the list.\n3 delete an element from the list. \n 4 delete the whole list\n 5 print the list\n 6 end program. \n");
}
void createlist(avia* elem)
{
    elem = NULL;
}
void insert(avia** elem, int number, float time, char destination)
{
    avia* newelem;
    newelem = (avia*)malloc(sizeof(avia)); 
    if (newelem != NULL)
    {
        newelem->number = number;
        newelem->time = time;
        newelem->destination = destination;
        newelem->next = NULL;
    }
    else printf("List not inserted. No memory available\n");
}
int deleteelem(avia** elem, int number)
{
    avia* previous;
    avia* current;
    avia* temp;
    if (number == (*elem)->number)
    {
        temp = *elem;
        *elem = (*elem)->next;
        free(temp);
        return(number);
    }
    else
    {
        previous = *elem;
        current = (*elem)->next;
        while (current != NULL && current->number != number)
        {
            previous = current;
            current = current->next;
        }
        if (current != NULL)
        {
            temp = current;
            previous = current->next;
            free(temp);
            return(number);
        }
    }
}
void printlist(avia* current)
{
    if (current == NULL)
    {
        printf("The list is empty\n");
    }
    else
    {
        printf("The list is: ");
        while (current != NULL)
        {
            printf("%d, %f, %s ->", current->number, current->time, current->destination);
            current = current->next;
        }
        printf("NULL");
    }
}
void deletelist(avia* current)
{
    avia* temp;
    if (current == NULL)
    {
        printf("The list is empty\n");
    }
    else
    {
        while (current != NULL)
        {
            temp = current;
            current = current->next;
            free(temp);
        }
    }
}
int isempty(avia* elem)
{
    return elem == NULL;
}
runtime
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5