c - 在链表中插入元素到列表中最大元素之后

标签 c pointers linked-list

我一直在这里绞尽脑汁,看看是否能找到解决方案,但经过几次无限循环后,这是我想要审查的代码:

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

#define MAX_TREE_STRING 100

typedef struct Node {
    char val;
    struct Node *next;
} node;

node *create_list(char *list_string) {
    node *momentary=NULL, *new=NULL, *head;
    int i;

    for(i=0; i<strlen(list_string); i++) {
        new = (node *)malloc(sizeof(node));
        new->val = list_string[i];
        new->next = NULL;
        if (!momentary) head = new;
        else momentary->next = new;
        momentary = new;
    }
    return head;
}

int print_list(node *head) {
    node *momentary;

    if(!head) return -1;
    for(momentary=head; momentary!=NULL; momentary=momentary->next)
        printf("%c ", momentary->val);
    printf("\n");
    return 0;
}

node *find_biggest(node *head) {
    node *momentary=NULL, *biggest=head;

    if (head==NULL) return NULL;
    for (momentary=head; momentary!=NULL; momentary=momentary->next) {
        if (momentary->val > biggest->val) biggest = momentary;

    }

    return biggest;
}

void insert_after_biggest(node **head, node **biggest, char val) {

    node *p = *head, *temp=NULL;

    *head = p->next;


    if(*head!=NULL){

        if(p->val==(*biggest)->val){

            temp=p;
            p->next=temp;
            p->val=temp->val;

            p->next=NULL;

            *biggest=p;
            p->next=(*biggest);
            p->val=(*biggest)->val;
            //*biggest=p;

            p->next=NULL;

            //temp=p;
            p->next=temp;
            p->val=temp->val;

        }

        else if(p->val<(*biggest)->val){

            *head = p->next;

            }
    }

}

int main () {
    node *head=NULL, *biggest=NULL;
    int menu_choice;
    char c, val, list_string[MAX_TREE_STRING];

    setbuf(stdout, NULL);
    do {
        menu_choice = 0;
        printf("\n1 Create list \n2 Print");
        printf("\n3 Insert after biggest");
        printf("\n4 exit\n");
        scanf("%d", &menu_choice);
        switch (menu_choice) {
            case 1:
                if (head) {
                    printf("List exist.\n");
                    break;
                }
                printf("Enter list as digits without spaces: ");
                scanf(" %s", list_string);
                head = create_list(list_string);
                break;
            case 2:
                print_list(head);
                break;
            case 3:
                scanf(" %c", &val);
                insert_after_biggest(&head, &biggest, val);
                break;
            case 4:
                break;
            default:
                while((c=getchar())!='\n' && c!=EOF);
        }
    } while(menu_choice!=4);
    return 0;
}

Now the task here is:

Write a function "insert_after_biggest" so that the new elements are put behind the element with the highest value and complexity of the function must be O (1). In other words, the function "find_biggest" that has complexity of O (n) can be called only if the "biggest" has not yet been defined. If the list has no elements , it still needs to be first.

Here's a console example to have clearer picture:

1|<-Insert into linked list option| Enter list as digits without spaces: 683 |<-Prompt and entered value| 3 |<-Option(the function)| 2 |<-Number to be inserted| 2 |<-Print option| 6 8 2 3|<-Output| 3 |<-Option| 8 |<-Number to be inserted| 2 |<-Print option| 8 |<-Output|

我自己输入了这段代码,但我不知道如何看待它。 我虚心请教是否有人可以帮我解决这个问题,代码可以编译,但在控制台中执行选项 3 只会使其无限循环运行,导致控制台崩溃。

具体:如何解决它(一步一步)以及它应该是什么样子(完成的代码,可能包含在这里?)。

谢谢。

最佳答案

如果你可以假设

1)您不需要删除元素

您只需要保留一个额外的指针,始终指向具有最大值的节点。如果您从未打过电话,那么很高兴您可以扫描列表中最大的一个 insert_after_biggest,但这甚至没有必要。将该节点称为 max,并将指向 max 的指针称为 p_max。

对于每个插入

a) 您要插入的值大于 max 保存的值,在这种情况下,您插入新值,并更改 p_max = p_new。

n_new->next = p_max;
p_max = p_new;

b) 您要插入的值大于持有的买入最大值,在这种情况下,只需插入并保持 p_max 不变。

对于 insert_after_max 你明确地这样做

p_new->next = p_max->next;
p_max->next = p_new;

或者如果新值更大,则按上述操作。

由于不需要扫描列表来插入,所以插入的复杂度是O(1)

关于c - 在链表中插入元素到列表中最大元素之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21394872/

相关文章:

C:线程安全记录到文件

c - C 中的链表;分段故障

java - 尝试对链接列表进行排序

c++ - 在我的场景中,哪种路径搜索算法的实现最快?

c - 无法理解将 int 添加到 char 数组的验证语句的逻辑

c - 如何声明指向文件的动态指针数组

C 结构和 malloc 函数

java - 如何在我的 Java GUI 中使用 linkedList 和多个类

c - 使用 strtok 和填充数组

c - 没有库函数的 C 中的反向字符串函数