c - 如何在单链表的开始、结束和选定位置插入节点?

标签 c malloc singly-linked-list

我是 C 编程新手,我尝试过在单链表程序中插入节点,但我没有得到正确的输出,而且我不知道如何纠正我的程序,如果有人知道请帮忙。

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
}*head;
int loc;

void addbegin(int num)
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if(head=NULL)
    {
        head=temp;
        head->next=NULL;
    }
    else
    {
        temp->next=head;
        head=temp;
    }
}

void addend(int num)
{
    struct node *temp1,*temp2;
    temp1=(struct node *)malloc(sizeof(struct node));
    temp1->data=num;
    temp2=head;
    if(head==NULL)
    {
        head=temp1;
        head->next=NULL;
    }
    else
    {
        while(temp2->next != NULL)
        temp2=temp2->next;
        temp1->next=NULL;
        temp2->next=temp1;
    }

}

void pos(int num,int loc)
{
    int length();
    struct node *temp,*cur_ptr,*prev_ptr;

    int i;
    cur_ptr=head;
    if(loc > (length()+1) || loc<= 0)
    {
        printf("it is illegal call:");
    }
    else 
    {
        if(loc == 1)
        {
            addbegin(num);
        }
        else
        {
            for(i=1;i<loc;i++)
            {
                prev_ptr=cur_ptr;
                cur_ptr=cur_ptr->next;
            }
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            prev_ptr->next=temp;
            temp->next=cur_ptr;
        }
    }
}

int length()
{
    struct node *cur_ptr;
    int count = 0;
    cur_ptr=head;
    while(cur_ptr!=NULL)
    {
        cur_ptr=cur_ptr->next;
        count++;
    }
    return(count);
}
void display()
{
    struct node *temp=NULL;
    if(temp==NULL)
    {
        printf("list is empty:");
    }
    while(temp!=NULL)
    {
        printf("%d",temp->data);
        temp=temp->next;
    }
}

int main()
{

    int num;
    head=NULL;
    int choice;
    while(1)
    {
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert at begin\n");
    printf("2.insert at end\n");
    printf("3.insert at selected position\n");
    printf("4.Display\n");
    printf("5.Exit\n");
    printf("Enter your choice : ");
    if(scanf("%d",&choice)<=0)
    {
        printf("Enter only an Integer\n");

    } 

    printf("enter your choice:");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1: printf("Enter the number to insert at begin : ");
            scanf("%d",&num);
            addbegin(num);
            break;
    case 2: printf("Enter the number to insert at end: ");
            scanf("%d",&num);
            addend(num);
            break;
    case 3: printf("Enter the number to insert at selected position: ");
            scanf("%d",&num);
            pos(num,loc);
            break;
    case 4: printf("display the values");
            display();
            break;
    case 5: printf("exit");

    display();
    }
    }
    return 0;
}

我认为错误出在我的主要函数中,但不清楚,请帮忙

最佳答案

在您的 addbegin 中方法至少有一个明显的错误:

if(head=NULL)

应该是

if (head == NULL)

因为您需要比较,而不是分配。

在您的 pos 中方法你有一个函数声明:int length();它不应该在那里,而应该在顶部,在 main 之前。

另一个问题,这次是在显示方法中:

void display()
{
    struct node *temp=NULL;
    if(temp==NULL) {
        printf("List is empty:");
    }
    while(temp!=NULL) {
        printf("%d",temp->data);
        temp=temp->next;
    }
}

这里 temp 将始终为 NULL,我猜你的意思是分配 headtemp指针,否则永远不会遍历列表。

最后,在 insert at specific position 中选择您需要询问位置值并将其传递给函数调用,因此添加 int loc; 的声明在 main 中,将第三种情况更改为:

case 3:
            printf("Enter the number to insert at selected position: ");
            scanf("%d",&num);
            printf("Enter the position: ");
            scanf("%d",&loc);
            pos(num,loc);
            break;

最后我要引用 C99 标准的5.1.2.2.1 程序启动部分:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

所以,请更改您的 main 声明并包括 return末尾的行(可能 return 0; 表示程序成功退出)。

这变得相当冗长。经过建议的更改后,您的程序应如下所示:

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

struct node {
    int data;
    struct node *next;
}*head;

void addbegin(int num)
{
    struct node *temp;
    temp = malloc(sizeof(struct node));
    temp->data=num;
    if(head==NULL) {
        head=temp;
        head->next=NULL;
    } else {
        temp->next=head;
        head=temp;
    }
}

void addend(int num)
{
    struct node *temp1, *temp2;
    temp1 = malloc(sizeof(struct node));
    temp1->data = num;
    temp2 = head;
    if(head == NULL) {
        head = temp1;
        head->next = NULL;
    } else {
        while(temp2->next != NULL)
            temp2=temp2->next;
        temp1->next=NULL;
        temp2->next=temp1;
    }
}

int length()
{
    struct node *cur_ptr;
    int count = 0;
    cur_ptr = head;
    while(cur_ptr != NULL) {
        cur_ptr = cur_ptr->next;
        count++;
    }
    return (count);
}

void pos(int num, int loc)
{
    struct node *temp, *cur_ptr, *prev_ptr;

    int i;
    cur_ptr=head;
    if(loc > (length()+1) || loc<= 0) {
        printf("it is illegal call:");
    } else {
        if(loc == 1) {
            addbegin(num);
        } else {
            for(i=1; i<loc; i++) {
                prev_ptr=cur_ptr;
                cur_ptr=cur_ptr->next;
            }
            temp = malloc(sizeof(struct node));
            temp->data=num;
            prev_ptr->next=temp;
            temp->next=cur_ptr;
        }
    }
}

void display()
{
    struct node *temp = head;
    if(temp == NULL) {
        printf("List is empty:");
    }

    printf("The list contains the following values:\n");
    while(temp!=NULL) {
        printf("%d\n",temp->data);
        temp=temp->next;
    }
}

int main()
{
    int choice, num, loc;
    head = NULL;

    while(1) {
        printf("\nList Operations\n");
        printf("===============\n");
        printf("1.Insert at begin\n");
        printf("2.insert at end\n");
        printf("3.Insert at selected position\n");
        printf("4.Display\n");
        printf("5.Exit\n");
        printf("Enter your choice : ");
        if(scanf("%d",&choice)<=0) {
            printf("Enter only an Integer\n");
        }

        switch(choice) {
        case 1:
            printf("Enter the number to insert at begin : ");
            scanf("%d",&num);
            addbegin(num);
            break;
        case 2:
            printf("Enter the number to insert at end: ");
            scanf("%d",&num);
            addend(num);
            break;
        case 3:
            printf("Enter the number to insert at selected position: ");
            scanf("%d",&num);
            printf("Enter the position: ");
            scanf("%d",&loc);
            pos(num,loc);
            break;
        case 4:
            printf("Display the values\n");
            display();
            break;
        case 5:
            printf("exit");
            exit(0); // maybe you should exit here.
            display();
        }
    }

    return 0;
}

关于c - 如何在单链表的开始、结束和选定位置插入节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22626391/

相关文章:

java - 从单链表打印节点

c - 后续函数调用中的 malloc

c - 将 sbrk 分成 2 个

java - 单节点java链表中存储多个变量

c - 如何将值从 CSV 文件移动到 float 组?

c - 如何避免 realloc 溢出?

java - 单链表上的快速排序

c - 你如何在cmd中运行一个c程序

c - 在 C 中使用 scanf 接受用户提供的尽可能多的输入

c - 用完 ram 声明全局二维数组问题