从C中的链接列表中删除第一个元素

时间:2014-10-20 03:53:20

标签: c struct

我很难从C中的链接列表中删除第一个元素。

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

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

typedef struct list {
    struct node *head;
} linklist;

void insertElement(linklist *list, node* curr);
void removeStart(node *curr);

int main(void){
    int i;
    node *curr;
    linklist *list;

    list = (linklist*)malloc(sizeof(linklist)); /* create head node */
    list->head = NULL;

    for(i=0; i<4; i++){
        curr = (node*)malloc(sizeof(node));
        curr->data = i;

        curr->next = list->head; /* insert element to start */
        list->head = curr;       /* "                     " */
    }

    curr = list->head; /* traverse link list to start */

    insertElement(list, curr);

    curr = list->head;

    removeStart(curr);

    curr = list->head;

    while(curr != NULL) 
    {
            printf("%d\n", curr->data);
            curr = curr->next;
    }
    return 0;
}

void insertElement(linklist *list, node *curr){
    curr = (node*)malloc(sizeof(node));
    curr->data = 4;
    curr->next = list->head;
    list->head = curr;
}

void removeStart(node *curr){
    node *tmp;

    tmp = curr;
    curr = curr->next;

    free(tmp);
} 

编译代码时,最终结果应为:

3 2 1 0

有人可以提供一些关于我应该怎么做的建议,也可以单独设置如何删除链接列表中的任何节点。感谢。

2 个答案:

答案 0 :(得分:2)

尝试以下方法。至少输出与您显示的输出一致。

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

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

typedef struct list 
{
    struct node *head;
} linklist;

void insertElement( linklist *list, int value )
{
    node *tmp = malloc( sizeof( node ) );

    tmp->data = value;
    tmp->next = list->head;

    list->head = tmp;
}

void removeStart( linklist *list )
{
    if ( list != NULL && list->head != NULL )
    {
            node *tmp = list->head;
            list->head = list->head->next;
            free( tmp );
    }
}

int main(void) 
{
    int i;
    node *tmp;
    linklist list;

    list.head = NULL;

    for ( i = 0; i < 4; i++ ) insertElement( &list, i );

    insertElement( &list, i );
    removeStart( &list );

    for ( tmp = list.head; tmp != NULL; tmp = tmp->next )
    {
        printf( "%d ", tmp->data );
    }

    for ( tmp = list.head; tmp != NULL;  )
    {
        node *current = tmp;
        tmp = tmp->next;
        free( current );
    }

    return 0;
}

3 2 1 0 

答案 1 :(得分:0)

我的解决方案:

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

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

typedef struct list {
    struct node *head;
} linklist;

void insertElement(linklist *list, node* curr);
void removeStart(linklist *list);

int main(void){
    int i;
    node *curr;
    linklist *list;

    list = (linklist*)malloc(sizeof(linklist)); /* create head node */
    list->head = NULL;

    for(i=0; i<4; i++){
        curr = (node*)malloc(sizeof(node));
        curr->data = i;

        curr->next = list->head; /* insert element to start */
        list->head = curr;       /* "                     " */
    }

    curr = list->head; /* traverse link list to start */

    insertElement(list, curr);

    curr = list->head;

    removeStart(list);

    curr = list->head;

    while(curr != NULL) 
    {
            printf("%d\n", curr->data);
            curr = curr->next;
    }
    return 0;
}

void insertElement(linklist *list, node *curr){
    curr = (node*)malloc(sizeof(node));
    curr->data = 4;
    curr->next = list->head;
    list->head = curr;
}

void removeStart(linklist *list){
    node *tmp = NULL;
    if(list->head != NULL)
    {
    tmp = list->head;
    list->head = list->head->next;
    }
    free(tmp);
} 
相关问题