簡體   English   中英

在鏈接列表中打印字符串

[英]Printing strings in linked lists

所以我很難讓我的程序打印我輸入的兩個字符串,或者無論您想輸入多少個字符串,它總是打印出多次輸入的最后一個字符串。 對於所有注釋掉的代碼,我感到很抱歉,其中大多數您不需要閱讀。

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

struct node{
    char *data;
    struct node *next;
}*head;

typedef struct node NODE;

// Function prototypes
void append(char myStr[]);
void add( char myStr[] );
//void addafter(char myStr[], int loc);
void insert(char myStr[]);
int delete(char myStr[]);
void display(struct node *r);
int count();
// main function
int  main()
{
  int i;
  struct node *n;
  head = NULL;
  char myStr[50];

while(1)
{
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert\n");
    printf("2.Display\n");
    printf("3.Size\n");
    printf("4.Delete\n");
    printf("5.Exit\n");
    printf("Enter your choice : ");

    if(scanf("%d", &i) <= 0)
    {
        printf("Enter only an Integer\n");
        exit(0);
    }
    else
    {

        switch(i)
        {

            case 1:
                printf("Enter the name to insert : ");
                scanf("%50s", myStr);
                insert(myStr);
                break;
            case 2:
                if(head == NULL)
                {
                    printf("List is Empty\n");
                }
                else
                {
                    printf("Name(s) in the list are : ");
                }
                display(n);
                break;
            case 3:
                printf("Size of the list is %d\n",count());
                break;
            case 4:
                if(head == NULL)
                    printf("List is Empty\n");
                else
                {
                    printf("Enter the myStrber to delete : ");
                    scanf("%50s",myStr);

                    if(delete(myStr))
                        printf("%s deleted successfully\n",myStr);
                    else
                        printf("%s not found in the list\n",myStr);
                }
                break;
            case 5:
                return 0;
            default:
                printf("Invalid option\n");
        }
    }
}

return 0;
}
// Function definitions
void append(char myStr[])
{
struct node *temp,*right;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = myStr;
right=(struct node *)head;

while(right->next != NULL)
{
    right = right->next;
}
right->next = temp;
right = temp;
right->next = NULL;
}
// adding a node to the beginning of the linked list
void add( char myStr[] )
{
struct node *temp;
temp =(struct node *)malloc(sizeof(struct node));
temp->data = myStr;

// only one node on the linked list
if (head == NULL)
{
    head = temp;
    head->next = NULL;
}

else
{
    temp->next = head;
    head = temp;
}
}

void insert(char myStr[])
{
int c = 0;
struct node *temp;
temp = head;
if(temp == NULL)
{
    add(myStr);
}
else
{
        append(myStr);
}
}
int delete(char myStr[])
{
struct node *temp, *prev;
temp = head;

while(temp != NULL)
{
    if(temp->data == myStr)
    {
        if(temp == head)
        {
            head = temp->next;
            head = (*temp).next;
            free(temp);
            return 1;
        }
        else
        {
            prev->next = temp->next;
            free(temp);
            return 1;
        }
    }
    else
    {
        prev = temp;
        temp = temp->next;
    }
}
return 0;
}
void  display(struct node *r)
{
r = head;

if(r == NULL)
{
    return;
}

while(r != NULL)
{
    printf("%s ", r->data);
    r = r->next;
    if(r == NULL)
    {
        printf("\nOur linked list is finished!");

    }
}

printf("\n");
}
int count()
{
struct node *n;
int c = 0;
n = head;

while(n != NULL)
{
    n = n->next;
    c++;
}

return c;
}

問題似乎是主函數上的myStrchar[] ,因此每次插入數據時,其內容都會被覆蓋。 注意,結構節點data字段是char* ,它只是指向myStr地址。

希望有幫助!

char *data ,來自struct的變量始終分配有myStr的地址作為其指針,它只會向您顯示myStr的值

您的程序只有一個地方可以編寫輸入myStr。
每次輸入時,都會刪除myStr,並將其他內容寫入myStr。
所有節點的數據成員都指向myStr。 myStr將僅包含最后一個輸入。
display()函數詢問每個節點什么是data data指向myStr,因此每個節點都將打印myStr的內容。 myStr僅包含最后一個輸入,因此所有節點都將打印最后一個輸入。

要解決此問題,在add()append()函數中,您需要使用malloc()為數據成員提供一些內存。 然后使用strcpy()將myStr的內容復制到數據成員。
temp->data = malloc ( strlen ( myStr) + 1);
strcpy ( temp->data, myStr);
而不是temp->data = myStr;
您將需要#include<string.h>

內存將需要在delete()函數中delete()
free(temp->data);
在釋放temp之前執行此操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM