首页 \ 问答 \ 如何在C编程中使用双向链表接受字符串?(How to accept string using doubly linked list in C programming?)

如何在C编程中使用双向链表接受字符串?(How to accept string using doubly linked list in C programming?)

这里我为DLL做了一个结构: -

struct node
{
    char letter;
    struct node* prev;
    struct node* next;
};

这是接受字符串的函数: -

struct node* accept(struct node* head)
{
    int i=0;
    char dummy, ch;
    struct node* memory, *memory1;
    memory = (struct node*)malloc(sizeof(struct node));
    printf("\n Enter the letters ");
    scanf("%c",&dummy);
    ch = getchar();
    if(ch == '\n')
    {
        return head;
    }
    memory->letter = ch;
    memory->prev = NULL;
    memory->next = NULL;
    head = memory;
    while(ch!='\n')
    {
        ch = getchar();
        memory1 = (struct node*)malloc(sizeof(struct node));
        memory1->letter = ch;
        memory1->prev = memory;
        memory1->next = NULL;
        memory = memory1;
        i++;
    }
    n = i;
    return head;
}

以下是近一半完成的计划: -

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

struct node
{
    char letter;
    struct node* prev;
    struct node* next;
};

int n= 0;

struct node* accept(struct node* head)
{
    int i=0;
    char dummy, ch;
    struct node* memory, *memory1;
    memory = (struct node*)malloc(sizeof(struct node));
    printf("\n Enter the letters ");
    scanf("%c",&dummy);
    ch = getchar();
    if(ch == '\n')
    {
        return head;
    }
    memory->letter = ch;
    memory->prev = NULL;
    memory->next = NULL;
    head = memory;
    while(ch!='\n')
    {
        ch = getchar();
        memory1 = (struct node*)malloc(sizeof(char));
        memory1->letter = ch;
        memory1->prev = memory;
        memory1->next = NULL;
        memory = memory1;
        i++;
    }
    n = i;
    return head;
}

void display(struct node* head)
{
    if(head == NULL)
    {
        printf("\n Nothing to display .. ");
        return ;
    }
    struct node* temp;
    temp = head;
    printf("\n");
    while(temp!=NULL)
    {
        printf("%c",temp->letter);
        temp = temp->next;
    }
    printf("\n");
}

void reverseDisplay(struct node* head)
{
    struct node* temp;
    temp = head;
    while(temp!= NULL)
    {
        reverseDisplay(temp->next);
    }
    printf("%c",temp->letter);
}

struct node* insertChar(struct node* head)
{
    int pos, i;
    char ch;
    struct node* temp, *temp1;
    temp1 = head;
    printf("\n Enter the position where you want to insert a letter : ");
    scanf("%d",&pos);
    if(pos <1 && pos >(n+1))
    {
        printf("\n INVALID POSITION .. ");
    }
    else
    {
        temp = (struct node*)malloc(sizeof(char));
        ch = getchar();
        temp->letter = ch;
        if(ch == '\n')
        {
            return head;
        }
        if(head == NULL)
        {
            temp->next = NULL;
            temp->prev = NULL;
            head = temp;
            return head;
        }
        for(i=1; i<pos-1; i++)
        {
            temp1 = temp1->next;
        }
        temp->next = temp1->next;
        temp1->next = temp;
        temp->prev = temp1;
        temp1->next->prev = temp;
        n++;

    }
    return head;

}

int main()
{
    struct node* head;
    head = NULL;
    int i,ch;
    char c;
    do
    {
    printf("\n Enter your choice :\n");
    printf("\n 1. Accept the string : ");
    printf("\n 2. Display the string : ");
    printf("\n 3. Reverse Display : ");
    printf("\n 4. Insert a character : ");
    printf("\n 5. Delete a character : ");
    printf("\n 6. Modify a character : ");
    printf("\n 7. Revert the string : ");
    printf("\n 8. Exit \n : ");
    scanf("%d",&ch);

    switch(ch)
    {
    case 1: printf("\n Accepting a string ...");
            head = accept(head);
            break;
    case 2:
            printf("\n Displaying the string .. ");
            display(head);
            break;
    case 3:
            printf("\n Reverse Displaying ..");
            if(head == NULL)
            {
                printf("\n There is nothing to be displayed ..");
                break;
            }
            printf("\n");
            reverseDisplay(head);
            break;
    case 4:
            head = insertChar(head);
            break;


    }
    }while(ch!=8);
    return 0;
}

问题是,当我选择显示文本时,这只显示输入到字符串中的第一个字母。 想不出来。 请帮我。

几个输出实例: -

输入您的选择:

  1. 接受字符串:
  2. 显示字符串:
  3. 反向显示:
  4. 插入一个字符:
  5. 删除一个字符:
  6. 修改一个字符:
  7. 还原字符串:
  8. 退出:1

    接受字符串...输入字母amit upadhyay

    输入您的选择:

  9. 接受字符串:

  10. 显示字符串:
  11. 反向显示:
  12. 插入一个字符:
  13. 删除一个字符:
  14. 修改一个字符:
  15. 还原字符串:
  16. 退出:2

    显示字符串.. a

    输入您的选择:

  17. 接受字符串:

  18. 显示字符串:
  19. 反向显示:
  20. 插入一个字符:
  21. 删除一个字符:
  22. 修改一个字符:
  23. 还原字符串:
  24. 出口 :

here i have made a structure for DLL :-

struct node
{
    char letter;
    struct node* prev;
    struct node* next;
};

and this is the function to accept the string :-

struct node* accept(struct node* head)
{
    int i=0;
    char dummy, ch;
    struct node* memory, *memory1;
    memory = (struct node*)malloc(sizeof(struct node));
    printf("\n Enter the letters ");
    scanf("%c",&dummy);
    ch = getchar();
    if(ch == '\n')
    {
        return head;
    }
    memory->letter = ch;
    memory->prev = NULL;
    memory->next = NULL;
    head = memory;
    while(ch!='\n')
    {
        ch = getchar();
        memory1 = (struct node*)malloc(sizeof(struct node));
        memory1->letter = ch;
        memory1->prev = memory;
        memory1->next = NULL;
        memory = memory1;
        i++;
    }
    n = i;
    return head;
}

and below is the almost half completed program :-

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

struct node
{
    char letter;
    struct node* prev;
    struct node* next;
};

int n= 0;

struct node* accept(struct node* head)
{
    int i=0;
    char dummy, ch;
    struct node* memory, *memory1;
    memory = (struct node*)malloc(sizeof(struct node));
    printf("\n Enter the letters ");
    scanf("%c",&dummy);
    ch = getchar();
    if(ch == '\n')
    {
        return head;
    }
    memory->letter = ch;
    memory->prev = NULL;
    memory->next = NULL;
    head = memory;
    while(ch!='\n')
    {
        ch = getchar();
        memory1 = (struct node*)malloc(sizeof(char));
        memory1->letter = ch;
        memory1->prev = memory;
        memory1->next = NULL;
        memory = memory1;
        i++;
    }
    n = i;
    return head;
}

void display(struct node* head)
{
    if(head == NULL)
    {
        printf("\n Nothing to display .. ");
        return ;
    }
    struct node* temp;
    temp = head;
    printf("\n");
    while(temp!=NULL)
    {
        printf("%c",temp->letter);
        temp = temp->next;
    }
    printf("\n");
}

void reverseDisplay(struct node* head)
{
    struct node* temp;
    temp = head;
    while(temp!= NULL)
    {
        reverseDisplay(temp->next);
    }
    printf("%c",temp->letter);
}

struct node* insertChar(struct node* head)
{
    int pos, i;
    char ch;
    struct node* temp, *temp1;
    temp1 = head;
    printf("\n Enter the position where you want to insert a letter : ");
    scanf("%d",&pos);
    if(pos <1 && pos >(n+1))
    {
        printf("\n INVALID POSITION .. ");
    }
    else
    {
        temp = (struct node*)malloc(sizeof(char));
        ch = getchar();
        temp->letter = ch;
        if(ch == '\n')
        {
            return head;
        }
        if(head == NULL)
        {
            temp->next = NULL;
            temp->prev = NULL;
            head = temp;
            return head;
        }
        for(i=1; i<pos-1; i++)
        {
            temp1 = temp1->next;
        }
        temp->next = temp1->next;
        temp1->next = temp;
        temp->prev = temp1;
        temp1->next->prev = temp;
        n++;

    }
    return head;

}

int main()
{
    struct node* head;
    head = NULL;
    int i,ch;
    char c;
    do
    {
    printf("\n Enter your choice :\n");
    printf("\n 1. Accept the string : ");
    printf("\n 2. Display the string : ");
    printf("\n 3. Reverse Display : ");
    printf("\n 4. Insert a character : ");
    printf("\n 5. Delete a character : ");
    printf("\n 6. Modify a character : ");
    printf("\n 7. Revert the string : ");
    printf("\n 8. Exit \n : ");
    scanf("%d",&ch);

    switch(ch)
    {
    case 1: printf("\n Accepting a string ...");
            head = accept(head);
            break;
    case 2:
            printf("\n Displaying the string .. ");
            display(head);
            break;
    case 3:
            printf("\n Reverse Displaying ..");
            if(head == NULL)
            {
                printf("\n There is nothing to be displayed ..");
                break;
            }
            printf("\n");
            reverseDisplay(head);
            break;
    case 4:
            head = insertChar(head);
            break;


    }
    }while(ch!=8);
    return 0;
}

The problem is that when i am choosing to display the text, then this is showing me only the first letter entered into the string. Can't figure it out. Please help me.

few output instances :-

Enter your choice :

  1. Accept the string :
  2. Display the string :
  3. Reverse Display :
  4. Insert a character :
  5. Delete a character :
  6. Modify a character :
  7. Revert the string :
  8. Exit : 1

    Accepting a string ... Enter the letters amit upadhyay

    Enter your choice :

  9. Accept the string :

  10. Display the string :
  11. Reverse Display :
  12. Insert a character :
  13. Delete a character :
  14. Modify a character :
  15. Revert the string :
  16. Exit : 2

    Displaying the string .. a

    Enter your choice :

  17. Accept the string :

  18. Display the string :
  19. Reverse Display :
  20. Insert a character :
  21. Delete a character :
  22. Modify a character :
  23. Revert the string :
  24. Exit :

原文:https://stackoverflow.com/questions/32959459
更新时间:2020-10-17 19:10

最满意答案

你缺少一个链接。

while(ch!='\n')
{
   ch = getchar();
   memory1 = (struct node*)malloc(sizeof(struct node));
   memory1->letter = ch;
   memory1->prev = memory;
   memory1->next = NULL;

   // The missing link
   memory->next = memory1;

   memory = memory1;
   i++;
}

You are missing to make a link.

while(ch!='\n')
{
   ch = getchar();
   memory1 = (struct node*)malloc(sizeof(struct node));
   memory1->letter = ch;
   memory1->prev = memory;
   memory1->next = NULL;

   // The missing link
   memory->next = memory1;

   memory = memory1;
   i++;
}
2015-10-05

相关问答

更多

我无法在C中正确创建双向链表(I can't create doubly linked list properly in C)

问题是您对所有输入使用相同的字符串,并将其用于所有节点。 这意味着所有节点的data成员都指向同一个字符串。 这个字符串当然只包含你上次读到的内容。 您可能希望将字符串缓冲区保留在main作为普通数组(而不是在堆上分配它)并使用eg strdup来复制节点的字符串。 不要忘记以后释放它们。 指针就是它听起来的样子,它是一个指向内存中其他位置的变量。 你可以有许多指针指向同一个内存。 在您的情况下,您将函数main的指针word传递给我们对fill_list所有调用。 这意味着您在fill_list ...

是否有可能编写一个不可变的双向链表?(Is it possible to write an immutable doubly linked list?)

是的,这是可能的。 但通常不会这样做,因为与单链表不同,双链表没有任何可以重用的子结构,例如,当一个元素被删除时。 而且,这样的列表似乎没有做任何不可变的Vector不能做的事情。 不过,让我们把它写下来,因为它很有趣。 简化问题:圆形双元素“列表” 作为热身,请看一下简化的问题:一个循环的双元素“列表”,其中两个节点相互引用: case class HalfRing(val value: Int)(otherHalf: => HalfRing) { def next = otherHalf ...

无法在C ++中的双向链表上实现合并排序(Trouble implementing merge sort on doubly-linked list in C++)

合并步骤中的主要问题发生在首先耗尽一个子列表的情况下。 另一个列表的其余部分将附加到合并列表的末尾,但不会保留任何prev指针。 在mergeList()的底部,它应该如下所示: if (first1 == NULL) { lastSmall->next = first2; first2->prev = lastSmall; <------ } else { lastSmall->next = first1; ...

带字符串的双链表(Doubly Linked List with strings)

你的函数insert_beginning有一个问题: var=(struct node *)malloc(sizeof(struct node)); var->data[100]=words[100] 通过这两行你想要复制words数组并将它放在列表的节点上:)但你实现是假的,你只需复制第100个char (这是数组的界限,因为第一个char开始于0,最后一个是99索引)从words数组到节点data 100th char。 您必须复制所有words内容,因此您可以使用strncpy函数 ...

在c中对双向链表进行排序(Sorting doubly linked list in c)

您未正确实现合并排序,该排序基于递归地将列表分为两部分,对它们进行排序并合并结果。 但是在你的代码中,你并没有真正将列表分成两半。 注意在行中: while((head_two != NULL) && (head_two->next != NULL)) { head = head->next; head_two = head->next->next; } head_two = head->next; head->next = NULL; 当head_two到达列表末尾时退出whi ...

双向链表的输出(Output of doubly linked list)

这个运营商 ostream& operator<<(ostream& out, const StringLinkedList& obj){ StringNode* temp = obj.head; while(temp != NULL){ out << temp->elem << ' '; temp = temp->next; } return out; } 是因为它尝试输出不存储字符串的head数据成员elem和tail也不存储字 ...

为什么我要在C ++中实现自己的双向链表?(Why would I want to implement my own doubly linked list in C++? [closed])

当已经有std :: list的时候,是否有任何实际的实际优势来实现我自己的双向链表? 可能不会。 我可以让自己的某件事更有效率吗? 也许 - 取决于任务。 例如,您可能只需要一个单链表,这可能最终会变得更快。 或者std :: list在过去的几年中已经得到了很多的改进,在大多数情况下它是最佳的双向链表实现? 大概。 所有这些东西的最佳答案可能是“在标准实现不起作用之前使用标准实现,然后弄清楚你将要做些什么。” is there any actual real-world advantage o ...

双重链表(Doubly linked lists)

if(head == null) { head = new CardInfoNode(); head.setCardInfo(info); head.setNext(tail); tail.setPrev(node) // here lies the problem. tail must be set to something // to make it doubl ...

双重链接结构列表 - 覆盖(Doubly Linked List of Structures - Overwriting)

所有id对象都具有相同的值 - idbuffer的第一个字符的idbuffer 。 主要的 cin >> idbuffer; r.id = idbuffer; ^^^^^^^^^ 然后在AddItem进一步 int AddItem( rec r ) //Return: 1 if a success, 0 if failed. //No duplicate id's, sort by lastname { rec * ptr = NULL; rec * current; ...

如何在C编程中使用双向链表接受字符串?(How to accept string using doubly linked list in C programming?)

你缺少一个链接。 while(ch!='\n') { ch = getchar(); memory1 = (struct node*)malloc(sizeof(struct node)); memory1->letter = ch; memory1->prev = memory; memory1->next = NULL; // The missing link memory->next = memory1; memory = memory1; ...

相关文章

更多

最新问答

更多
  • 一个Web角色上的两个Web应用程序 - 配置问题(Two web applications on one Web Role - config issue)
  • Chrome扩展程序:在后台页面中存储变量是否安全?(Chrome extension : storing variables in background page is secure?)
  • C ++ 11中的atomics是否阻止编译器重新读取共享变量?(Do atomics in C++11 prevent compiler to re-read from shared variables?)
  • Java代码重组(Java code restructuring)
  • 从video.js获取currentTime(Get currentTime from video.js)
  • 3列 - 带溢流元件的中心柱流体(3 columns - center column fluid with an overflow element)
  • JSX节点中的TypeScript类型参数(TypeScript type arguments in JSX nodes)
  • .NET自定义事件组织帮助(.NET custom event organization assistance)
  • 如何将内存地址(如0xc20803a000)转换为字符串?(How to convert memory address (like 0xc20803a000) to string? Golang)
  • 从python中的列表创建新列表的最佳方法(Best way to create a new list from a list in python)
  • 为什么RabbitMQ会从破坏的持久性日志文件中脱颖而出?(Why does RabbitMQ keep breaking from a corrupt persister log file?)
  • 将对象添加到模型中,不更新dom(Adding object to model, doesn't update dom)
  • 将array.count转换为String(Convert array.count to String)
  • Chrome中的HTML (HTML in Chrome)
  • Sklearn:分类Imputer?(Sklearn: Categorical Imputer?)
  • 为什么iOS 9方法仍适用于iOS 8.4?(Why Do iOS 9 Methods Still Work on iOS 8.4?)
  • 输入类型文件无法获取angularjs中文件类型的文件名(input type file is unable to get the file name with file type in angularjs)
  • 为什么SplFixedArray的json_encode转储对象而不是数组?(Why does json_encode of a SplFixedArray dump an object instead of an array?)
  • ORA-01858:找到一个非数字字符,其中xp.executenonquery()上的数字是预期的(ORA-01858: a non-numeric character was found where a numeric was expected on xp.executenonquery())
  • 通过使用sscanf提取数据在BST插入函数中传递参数(Passing arguments in a BST insert function by extracting data using sscanf)
  • 混合c和c ++的问题(problem mixing c and c++)
  • 检查几个选项的文本(Checking text against several options)
  • 在Flex中创建Captch组件(Create Captch Component in Flex)
  • 查询非常复杂,不确定是否可能。(Very complicated query, not sure if it possible. Involves not adding to a running sum)
  • AOSP - networkAttributes中CSV的含义(AOSP - Meaning of CSV in networkAttributes)
  • 将不同表中的数据拖入单个视图中(Pulling data from different tables into a single view)
  • 从Enum Java调用随机值(更新)(Calling Random Value from an Enum Java)
  • jwplayer与vimeo插件(jwplayer with vimeo plugin)
  • CSS依赖于ActiveX?(CSS dependent on ActiveX?)
  • TPL Dataflow没有完成多个目标(TPL Dataflow not completing with multiple targets)