小编Cir*_*nor的帖子

如何创建头节点

我想我在newList中弄错了.不能更改Typedef结构实现.这是我学校的实验室作业..在此先感谢:)

#include<stdio.h>

    typedef struct node *nodeptr;
    struct node {
    int item;
    nodeptr next;
};
typedef nodeptr List;


List newList(); 
Run Code Online (Sandbox Code Playgroud)

newList创建一个头并返回一个指向头节点的指针

void display(List list);
void addFront(List list, int item);

List newList(){
    List list;
    list=(nodeptr)malloc(sizeof(List));
    list->next=NULL;
    return list;
} //I think my new list is incorrect..
void display(List list){
    nodeptr ptr=list;
    while(ptr!=NULL){
        printf("%d",ptr->item);
        ptr=ptr->next;
    }
    printf("\n");
}
void addEnd(List list, int item){
    nodeptr temp, ptr;
    temp=(List)malloc(sizeof(nodeptr));
    temp->item=item;
    temp->next=NULL;
    if(list->next==NULL)
        list=temp;
    else{
        ptr=list;
        while(ptr->next!=NULL)
            ptr=ptr->next;
        ptr->next=temp;
    }

}
Run Code Online (Sandbox Code Playgroud)

我似乎无法从列表中添加10 ..

int main(void){
    List …
Run Code Online (Sandbox Code Playgroud)

c linked-list

1
推荐指数
1
解决办法
5117
查看次数

标签 统计

c ×1

linked-list ×1