vendredi 29 mai 2015

Printing strings in linked lists

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;
}

Aucun commentaire:

Enregistrer un commentaire