0

So I'm having trouble getting my program to print both the strings I input, or however many you want to put in the list, it always prints out the last string inputted multiple times. I am sorry about all the commented out code, most of it you don't need to read.

#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;
}
3
  • If we don't need to read some of the code, please take the time to remove it rather than asking everybody who comes here to ignore it. Post the smallest example that demonstrates the problem. Commented May 28, 2015 at 23:27
  • Will do next time i apologize Commented May 29, 2015 at 4:07
  • You can still do it this time - just edit your question. Commented May 29, 2015 at 15:50

3 Answers 3

1

The problem seems to be that myStr at main function is a char[], so it's content is overritten every time you insert data. Notice that struct node data field is a char*, it's just pointing to myStr address.

Hope this help!

Sign up to request clarification or add additional context in comments.

2 Comments

But are i not putting myStr in a different part of the linked list? So when i call the display, each individual node will have a different myStr?
From what ive been taught, and its only been a couple of days, is that you create a node which will contain your data, and you have to link them together, so by doing that i create a right node that will add the most recent string to the end of the list, by creating *right and moving it to the right till it reaches null, then setting right equal to temp which is the new string, while my first string was at head and never moved
0

Your program has only one place to write your input, myStr.
With each input, myStr is erased and a something else is written to myStr.
The data member of all of the nodes, points to myStr. myStr will only contain the last input.
The display() function asks each node what is data. data points to myStr so each node prints the contents of myStr. myStr will only contain the last input so all the nodes print the last input.

To fix this, in the add() and append() functions, you need to give the data member some memory by using malloc(). Then copy the contents of myStr to the data member by using strcpy().
temp->data = malloc ( strlen ( myStr) + 1);
strcpy ( temp->data, myStr);
Do this instead of temp->data = myStr;
You will need #include<string.h>

The memory will need to be free()'d in the delete() function.
free(temp->data);
Do this before freeing temp

4 Comments

Everything you have told me up to delete has worked, but the delete function seems to be unable to find the string it is looking for, i did as you said i put free(temp->data); right before i free'd temp in both situations, i dont see why this would happen since i have temp checking every node starting with head
yes you are completely right, thank you so much i really appreciate it!
if you dont mind explaining what are the differences are between the two, i know yours is comparing the two strings somehow
could i have dereferenced the two pointer and then compare them like *temp->*data == mystr ?
0

char *data that variable from struct is always assigned with the address of myStr as its a pointer it would only show you the value of myStr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.