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

C 如何在单链表的开始、结束和选定位置插入节点?,c,malloc,singly-linked-list,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 *

我是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;
}
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
}*头部;
int loc;
void addbegin(int num)
{
结构节点*temp;
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=num;
if(head=NULL)
{
压头=温度;
head->next=NULL;
}
其他的
{
温度->下一步=头部;
压头=温度;
}
}
无效加数(整数)
{
结构节点*temp1,*temp2;
temp1=(结构节点*)malloc(sizeof(结构节点));
temp1->data=num;
temp2=头部;
if(head==NULL)
{
头部=temp1;
head->next=NULL;
}
其他的
{
while(temp2->next!=NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
无效位置(整数、整数位置)
{
int length();
结构节点*temp、*cur\u ptr、*prev\u ptr;
int i;
cur_ptr=头部;
如果(loc>(length()+1)| | locdata=num;
上一步->下一步=温度;
温度->下一步=电流;
}
}
}
int-length()
{
结构节点*cur_ptr;
整数计数=0;
cur_ptr=头部;
while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
计数++;
}
返回(计数);
}
无效显示()
{
结构节点*temp=NULL;
if(temp==NULL)
{
printf(“列表为空:”;
}
while(temp!=NULL)
{
printf(“%d”,临时->数据);
温度=温度->下一步;
}
}
int main()
{
int-num;
head=NULL;
智力选择;
而(1)
{
printf(“\n列出操作\n”);
printf(“============================\n”);
printf(“1.Insert at begin\n”);
printf(“2.在末尾插入\n”);
printf(“3.在选定位置插入\n”);
printf(“4.显示\n”);
printf(“5.退出\n”);
printf(“输入您的选择:”);

如果在
addbegin
方法中(scanf(“%d”,&choice),至少有一个明显错误:

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总是空的,我想你是想把
head
分配给
temp
指针,否则它永远不会遍历列表

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

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节程序启动:

程序启动时调用的函数名为main 实现没有声明此函数的原型。它应该是 使用int的返回类型定义且不带参数:

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

或具有两个参数(此处称为 argc和argv,但可以使用任何名称,因为它们是 声明它们的函数):

因此,请更改
main
的声明,并在末尾添加一行
return
(可能
return0;
表示程序成功退出)

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

#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;
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
}*头部;
void addbegin(int num)
{
结构节点*temp;
temp=malloc(sizeof(struct node));
温度->数据=num;
if(head==NULL){
压头=温度;
head->next=NULL;
}否则{
温度->下一步=头部;
压头=温度;
}
}
无效加数(整数)
{
结构节点*temp1,*temp2;
temp1=malloc(sizeof(struct node));
temp1->data=num;
temp2=头部;
if(head==NULL){
头部=temp1;
head->next=NULL;
}否则{
while(temp2->next!=NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
int-length()
{
结构节点*cur_ptr;
整数计数=0;
cur_ptr=头部;
while(cur_ptr!=NULL){
cur_ptr=cur_ptr->next;
计数++;
}
返回(计数);
}
无效位置(整数、整数位置)
{
结构节点*temp、*cur\u ptr、*prev\u ptr;
int i;
cur_ptr=头部;
如果(loc>(length()+1)| | locdata=num;
上一步->下一步=温度;
温度->下一步=电流;
}
}
}
无效显示()
{
结构节点*温度=头部;
if(temp==NULL){
printf(“列表为空:”;
}
printf(“列表包含以下值:\n”);
while(temp!=NULL){
printf(“%d\n”,临时->数据);
温度=温度->下一步;
}
}
int main()
{
int选择,num,loc;
head=NULL;
而(1){
printf(“\n列出操作\n”);
printf(“============================\n”);
printf(“1.Insert at begin\n”);
printf(“2.在末尾插入\n”);
printf(“3.在选定位置插入\n”);
printf(“4.显示\n”);
printf(“5.退出\n”);
printf(“输入您的选择:”);
if(scanf(“%d”,&choice)
及

#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
}*头部;
int loc;
void addbegin(int num)
{
结构节点*temp;
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=num;
如果(head==NULL)//这不是赋值,则需要==进行比较
{
压头=温度;
head->next=NULL;
}
其他的
{
温度->下一步=头部;
压头=温度;
}
}
无效加数(整数)
{
结构节点*temp1,*temp2;
temp1=(结构节点*)malloc(sizeof(结构节点));
temp1->data=num;
temp2=头部;
if(head==NULL)
{
头部=temp1;
head->next=NULL;
}
其他的
{
while(temp2->next!=NULL)
#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;
}
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 display()
{
struct node *temp=NULL;
temp = head;
  if(temp==NULL)
  {
    printf("list is empty:");
  }
  while(temp!=NULL)
  {
    printf("%d",temp->data);
    temp=temp->next;
  }
}
#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) //this not assign, you need == to compare
    {
        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;
    }

}

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=(struct node*)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:");
    }
    while(temp!=NULL)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

int main()
{

    int num;

    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");

        } 

        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("\ndisplay the values: ");
            display();
            break;
        case 5: printf("exit");
            display();
        }
    }
    return 0;
}
#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) //this not assign, you need == to compare
    {
        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;
    }

}

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=(struct node*)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:");
    }
    while(temp!=NULL)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

int main()
{

    int num;

    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");

        } 

        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 to insert: 1 for insert at begin, so on: ");
            scanf("%d",&loc);
            pos(num,loc);
            break;
        case 4: printf("\ndisplay the values: ");
            display();
            break;
        case 5: 
            display();
        printf("\n exiting program \n");
        exit(0);
        }
    }
    return 0;
}