Home > database >  How to add and use tail in C list
How to add and use tail in C list

Time:04-20

So I am trying to create list. Function to add number on front of the list is working but I'm having trouble adding a number to the end of the list. I know that tail changes data value but I don't know how should i printf this and use it to change my list.

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


struct list
{
    int data;
    struct lista *next;
};
struct listWsk
{
    struct list *head,*tail;
};
int create(struct listWsk *list, int data)
{
    struct list *new_front = malloc(sizeof(struct list));
    struct list *new_back = malloc(sizeof(struct list));
    if(NULL != new_front && NULL != new_back)
    {
        new_front->data = data;
        new_back->data = data;
        list->head = new_front;
        list->tail = new_back;
        list->tail->next = NULL;
        list->head->next = NULL;
        return 1;
    }
};
void print(struct listWsk list)
{
    while(list.head != NULL)
    {
        printf("%d ",list.head->data);
        list.head = list.head->next;
    }
}
void front(struct listWsk *list, int data)
{
    struct list *new = malloc(sizeof(struct list));
    if(new != NULL)
    {
        new ->data = data;
        new ->next = list ->head;
        list ->head = new;
    }
};
void back(struct listWsk *list,int data)
{
    struct list *new = malloc(sizeof(struct list));
    if(new != NULL)
    {
        new -> data = data;
        new ->next = NULL;
       list ->tail->next = new;
       list->tail = new;
    }

}

int main()
{



    struct listWsk lista = {NULL,NULL};
    create(&lista,5);
    front(&lista,8);
    back(&lista,3);
    print(lista);


}

Output: 8 5 and I don't know how to add tail number to the list.

CodePudding user response:

Corrected code

struct list
{
    int data;
    struct list* next; <<<<==== you had lista here - surpised it even compiled
};
struct listWsk
{
    struct list* head, * tail;
};
int create(struct listWsk* list, int data)
{
    // you dont need 2 nodes here just one
    // head and tail both point to it
    struct list* new_front = malloc(sizeof(struct list));
    if (NULL != new_front)
    {
        new_front->data = data;
        new_front->next = NULL;
        list->head = new_front;
        list->tail = new_front;
        return 1;
    }
    return 0; <<< === you were not returning a value here
};
void print(struct listWsk list)
{
    while (list.head != NULL)
    {
        printf("%d ", list.head->data);
        list.head = list.head->next;
    }
}
void front(struct listWsk* list, int data)
{
    struct list* new = malloc(sizeof(struct list));
    if (new != NULL)
    {
        new->data = data;
        new->next = list->head;
        list->head = new;
    }
};
void back(struct listWsk* list, int data)
{
    struct list* new = malloc(sizeof(struct list));
    if (new != NULL)
    {
        new->data = data;
        new->next = NULL;
        list->tail->next = new;
        list->tail = new;

    }

}

int main()
{
    struct listWsk lista = { NULL,NULL };
    create(&lista, 5);
    front(&lista, 8);
    back(&lista, 3);
    // I added a few more test cases
    front(&lista, 81);
    back(&lista, 32);
    print(lista);


}

you problem was that you had 2 nodes instead of one in you list with 1 entry, things just got confused

  •  Tags:  
  • c
  • Related