简体   繁体   English

在 C 中插入和打印链表

[英]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 .我希望程序打印出HELLO但它打印出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.我确实尝试在每次插入字母后print function 以查看insert功能是否正常工作。 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.所以我相信问题不在于insert功能,但我可能是错的。

This is my header file:这是我的 header 文件:


#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:主要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;
}

Your print function does not use the parameter plist .您的print function 不使用参数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.此外init_list正在将具有未初始化字符的节点添加到列表的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM