使用双指针的链表

时间:2016-02-14 07:09:38

标签: c

首次使用双指针创建和显示链表

#include "stdio.h"
#include "stdlib.h"

struct node
{
    int data;
    struct node * next;
};
void Insert(struct node **, int , int );
void display(struct node *);
int main()
{
    int c, data, position;
    struct node* head;
    do{
        printf("Enter a choice :\n");
        printf("1. Add an element.\n");
        printf("2. Del an element.\n3.Display List.\n");
        printf("4.Delete linked list.\n5.Exit.\n");
        printf("Your Choice :");
        scanf("%d",&c);
        switch(c){
            case 1 :
                printf("\nEnter data and position :\n");
                scanf("%d %d",&data,&position);
                Insert(&head,data,position);
                break;
            case 2 :

                break;
            case 3 :
                printf("Linked List : \n");
                display(head);
                break;
            case 4 :

                break;
            case 5 :
                exit(0);
            default :
                printf("Invalid Choice.\n");
                break;
        }
    }while(1);

    return 0;
}


void Insert(struct node **ptrhead, int item, int position){
    struct node *p,*newnode;
    //node creation.
    newnode = (struct node *)malloc(sizeof(struct node));
    if (!newnode)
    {
        printf("Memory Error.\n");
        return;
    }
    newnode->next = NULL;
    newnode->data = item;

    p = *ptrhead;

    // Creates initial node
    if (!(p->data))
        {
            p = newnode;
        }

    // insertion at beginning
    if (position==1)
    {
            newnode->next = p;
            p = newnode;
            free(newnode);
    }
    // insertionn at middle or end.
    else
    {
        int i=1;
        while(p->next!=NULL && i<position-1){
            p=p->next;
            i++;
        }
        newnode->next = p->next;
        p->next = newnode;
    }

    *ptrhead = p;

};

// Display Linked list
void display(struct node *head){
    if (head)
    {
        do{
            printf("%d\n", head->data);
            head = head->next;
        }while(head->next);
    }

};

我稍后会添加删除和其他操作的功能。现在,我只想插入并显示fns才能工作。但是输出是带有错误值的无限循环循环。我无法弄清楚我的代码中有什么问题,请帮忙吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

不知道为什么有人今天会写这种类型的C,看起来好像我正在为你做功课......无论如何,你要求修改你的代码,而不是重写它,所以这里&# 39;是最小的变化集。

head应初始化为NULL

if (!(p->data))不对。 if语句应该只是:

// Creates initial node
if (!p)
{
    *ptrhead = newnode;
    return;
}

删除free(newnode);

中间/结束代码的插入可能是

int i = 1;
struct node *n = p;
while (n->next != NULL && i<position - 1){
    n = n->next;
    i++;
}
newnode->next = n->next;
n->next = newnode;

最终的insert功能:

void Insert(struct node **ptrhead, int item, int position)
{
    struct node *p, *newnode;
    //node creation.
    newnode = (struct node *)malloc(sizeof(struct node));
    if (!newnode)
    {
        printf("Memory Error.\n");
        return;
    }
    newnode->next = NULL;
    newnode->data = item;

    p = *ptrhead;

    // Creates initial node
    if (!p)
    {
        *ptrhead = newnode;
        return;
    }

    // insertion at beginning
    if (position == 1)
    {
        newnode->next = p;
        p = newnode;
    }
    // insertionn at middle or end.
    else
    {
        int i = 1;
        struct node *n = p;
        while (n->next != NULL && i<position - 1){
            n = n->next;
            i++;
        }
        newnode->next = n->next;
        n->next = newnode;
    }

    *ptrhead = p;

}

您的print功能不是很正确,只需制作它:

// Display Linked list
void display(struct node *head)
{
    while (head)
    {
        printf("%d\n", head->data);
        head = head->next;
    }
}