用C编程语言在最后位置插入节点的代码

时间:2017-01-27 05:34:16

标签: c struct linked-list

我正在使用C编程语言在链表上编写代码。当我使用在线编译器时,我的代码工作正常,但当我使用Codeblock运行代码时,代码无效。我发布的代码请给我解决方案。我的代码是在最后一个位置的链接列表中添加节点。

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

struct Node
{
    int data;
    struct Node* next;
};

struct Node* Head;
void insert(int);
void print();

int main()
{
    Head = NULL;

    insert(2);
    insert(3);
    insert(4);

    print();

    return 0;
}

void insert(int a)
{
    struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));

    temp1-> data = a;
    temp1-> next = NULL;

    if(Head == NULL)
    {
        Head = temp1;
        return;
    }

    struct Node* temp = Head;

    while(temp->next!= NULL)
    {
        temp = temp->next;
    }

    temp-> next = temp1;
}


void print()
{
    struct Node* temp2=Head;

    while(temp2 != NULL)
    {

        printf("%d \n", temp2->data);
        temp2 = temp2->next;
    }

    return;

}

1 个答案:

答案 0 :(得分:0)

以下是如何实现自己想要的目标。

我建议你在编程时使用自上而下的方法。

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

struct node
{
    int info;
    struct node *next;
};

struct node *Insert(struct node *, int);
void Print_List(struct node *);
void Remove_List(struct node *);

int main(int argc, char **argv)
{
    struct node *head;

    head = NULL;

    head = Insert(head, 10);
    head = Insert(head, 20);

    Print_List(head);

    Remove_List(head);

    head = NULL;

    return 0;
}

struct node *Create_New_Node(int);
struct node *Head_Insert(struct node *, int);
struct node *Queue_Insert(struct node *, int);
struct node *Insert(struct node *, int);
void Print_List(struct node *);
void Remove_List(struct node *);

struct node *Insert(struct node *top, int elem)
{
    if(top == NULL)
    {
        top = Head_Insert(top, elem);
    }
    else
    {
        top = Queue_Insert(top, elem);
    }

    return top;
}

struct node *Create_New_Node(int elem)
{
    struct node *new_node;

    new_node = (struct node *)malloc(sizeof(struct node));

    if(new_node != NULL)
    {
        new_node -> info = elem;
        new_node -> next = NULL;
    }

    return new_node;
}


struct node *Head_Insert(struct node *top, int elem)
{
    struct node *new_node = Create_New_Node(elem);

    if(new_node != NULL)
    {
        new_node -> next = top;
    }

    return new_node;
}

struct node *Queue_Insert(struct node *top, int elem)
{
    if(top != NULL)
    {
        if(top -> next != NULL)
        {
            top -> next = Queue_Insert(top -> next, elem);
        }
        else
        {
            struct node *new_node = Create_New_Node(elem);

            if(new_node != NULL)
            {
                 top -> next = new_node;
            }
        }
    }

    return top;
}

void Print_List(struct node *top)
{
    while(top != NULL)
    {
        printf("\nInfo : %d\tAddress : %u\tNext link address : %u\n", top -> info, top, top -> next);
        top = top -> next;
    }

    return;
}

void Remove_List(struct node *top)
{
    if(top != NULL)
    {
        Remove_List(top -> next);
        top -> next = NULL;
        free(top);
    }

    return;
}

示例输出:

  

信息:10地址:39149584下一个链接地址:39149616

     

信息:20地址:39149616下一个链接地址:0