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

时间:2014-03-25 05:48:46

标签: 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;
}

我认为错误在我的主要功能中,但我不清楚请帮助

4 个答案:

答案 0 :(得分:1)

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,我想你的意思是将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,虽然可以使用任何名称,因为它们是本地的   声明它们的函数:

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;
}

答案 1 :(得分: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;
  }
}

答案 2 :(得分: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);
            pos(num,loc);
            break;
        case 4: printf("\ndisplay the values: ");
            display();
            break;
        case 5: printf("exit");
            display();
        }
    }
    return 0;
}

答案 3 :(得分: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;
}