Rivf
Rivf

Reputation: 125

Inserting & Printing Linked List in C

I am testing out my linked list functions with this snippet of code but I cannot get it to print correctly. I want the program to print out HELLO but instead it prints out LO. This is my first time working with linked lists so any help is appreciated.

I did try putting the print function after every insertion of a letter to see if the insert functions were working and they were. It would print out each letter but after inserting all the letters and printing once again it only prints out the last two letters. So I believe the problem is not with the insert functions but I could be wrong.

This is my header file:


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

typedef char DATA_TYPE;


typedef struct node_t
{
    DATA_TYPE item;
    struct node_t* next;
} node;

typedef struct
{
    node* head;
    int len;
} linkedlist_t;

void init_list(linkedlist_t* plist);
void insert_first(linkedlist_t* plist, DATA_TYPE item);
void insert_middle(linkedlist_t* plist, int pos, DATA_TYPE item);
void insert_last(linkedlist_t* plist, DATA_TYPE item);
void print(linkedlist_t* plist);    

Functions:

#include "linkedlist.h"

void init_list(linkedlist_t* plist)
{

    plist->head = (node *)malloc(sizeof(node));
    plist->head->next = NULL;
    plist->len = 0;
}


void insert_first(linkedlist_t* plist, DATA_TYPE item)
{
    node* cur, *newNode;

    newNode = (node *)malloc(sizeof(node));
    newNode->item = item;
    newNode->next = NULL;

    cur = plist->head;

    newNode->next = cur->next;
    cur->next = newNode;
    plist->len++;
}



void insert_middle(linkedlist_t* plist, int pos, DATA_TYPE item)
{

    node* cur, *newNode;
    if(pos < 0 || pos > plist->len){
        exit(1);
    }

    newNode = (node *)malloc(sizeof(node));
    newNode->item = item;
    newNode->next = NULL;

    cur = plist->head;
    for(int i = 0; i < pos; i++){
        cur = cur->next;
    }

    newNode->next = cur->next;
    cur->next = newNode;
    plist->len++;
}

void insert_last(linkedlist_t* plist, DATA_TYPE item)
{

    node* cur, *newNode;

    newNode = (node *)malloc(sizeof(node));
    newNode->item = item;
    newNode->next = NULL;

    cur = plist->head;
    while(cur->next != NULL){
        cur = cur->next;
    }

    newNode->next = cur->next;
    cur->next = newNode;
    plist->len++;

}


void print(linkedlist_t* plist)
{
    node* cur;
    while(cur != NULL){
        printf("%c", cur->item);
        cur = cur->next;
    }
    printf("\n");
}

Main function:

#include "linkedlist.h"
#include <string.h>

int main(void) {


    linkedlist_t list;

    init_list(&list);

    insert_first(&list, 'H');
    insert_middle(&list, 1, 'E');
    insert_middle(&list, 2, 'L');
    insert_middle(&list, 3, 'L');
    insert_last(&list, 'O');

    print(&list);

    return 0;
}

Upvotes: 0

Views: 100

Answers (1)

user15578331
user15578331

Reputation:

Your print function does not use the parameter plist.

You most likely want something like:

void print(linkedlist_t* plist)
{
    node* cur = plist->head;
    while(cur != NULL){
        printf("%c", cur->item);
        cur = cur->next;
    }
    printf("\n");
}

Also init_list is adding a node with an uninitialized character to the start of the list.

Upvotes: 3

Related Questions