c - 如何使用链接的实现在单链接列表中插入和删除元素

标签 c data-structures struct singly-linked-list

我尝试了使用链接实现存储数据的单链接列表程序。编译代码时,编译器未显示任何错误消息,而是将程序带到CLI。但是在CLI中,它没有提供所需的输出。程序在InsertFront()函数的错误位置终止,我在主函数中首先调用该函数。另外我不确定在前面,结尾和所需位置插入元素以及在上述位置删除元素的全部六个功能在哪里。下面是我的代码,当我运行它时,它显示列表已满

#include<stdio.h>
#include<stdlib.h>
typedef enum {FALSE,TRUE} Boolean;
typedef int ListEntry;
typedef int Position;
typedef struct listnode{
    ListEntry entry;
    struct listnode *next;
}Node;
typedef struct list {
    int count;
    Boolean full;
    Node* head;
}List;
void CreateList(List *l){
    l->count = 0;
    l->head = NULL;
    l->full = FALSE;
}
Boolean IsListEmpty(List *l){
    return(l->head==NULL);
}
Boolean IsListFull(List *l){
    return(l->full);
}
int ListSize(List *l){
    return (l->count);
}
void InsertFront(List *l,ListEntry x){
    if(IsListFull(l)){
        printf("List is Full\n");
        exit(1);
    }
    Node *np;
    np = (Node* )malloc(sizeof(Node));
    np->entry = x;
    if(l->head == NULL)
        np->next = NULL;
    else
        np->next = l->head;
    l->head = np;
    l->count++;
}
void InsertEnd(List *l, ListEntry x){
    if(IsListFull(l)){
        printf("List is Full\n");
        exit(1);
    }
    Node *np, *temp;
    np =(Node* )malloc(sizeof(Node));
    np->entry = x;
    np->next = NULL;
    if(l->head == NULL)
        l->head = np;
    else{
        temp = l->head;
        while(temp->next != NULL){
            temp = temp->next;
        }
        temp->next = np;
        l->count++;
    }
}
void Insert(List *l,ListEntry x,Position p){
    if(IsListFull(l)){
        printf("List os Full\n");
        exit(1);
    }
    Node *temp;
    temp = l->head;
    int j=0;
    if(l->count<p)
        printf("Position is out of list\n");
    else{
        while(j<p){
            temp = temp->next;
            j=j+1;
        }
        Node *np;
        np = (Node* )malloc(sizeof(Node));
        np->entry = x;
        np->next = temp->next;
        temp->next = np;
        l->count++;
    }
}
void DeleteFront(List *l, ListEntry *x){
    if(l->head == NULL){
        printf("Underflow\n");
        exit(1);
    }
    else{
        Node *temp;
        temp = l->head;
        l->head = l->head->next;
        *x = temp->entry;
        l->count--;
            free(temp);

    }
}
void DeleteEnd(List *l, ListEntry *x){
    if(l->head == NULL){
        printf("Underflow\n");
        exit(1);
    }
    else{
        Node *temp;
        temp = l->head;
        while(temp->next->next != NULL){
            temp = temp->next;
        }
        *x = temp->next->entry;
        temp->next = NULL;
        l->count--;
            free(temp);

    }
}
void Delete(List *l, ListEntry *x, Position p){
    if(IsListEmpty(l)){
        printf("List is Emptry\n");
        exit(1);
    }
    Node *temp;
    temp = l->head;
    int j = 1;
    if(l->count<p){
        printf("Position is out of list\n");
        exit(1);
    }
    else{
        while(j<p){
            temp = temp->next;
            j=j+1;
        }
        *x = temp->next->entry;
        temp->next = temp->next->next;
        l->count--;
    }
}
void main(){
    List l;
    int num;
    InsertFront(&l,23);
    Insert(&l,12,3);
    InsertEnd(&l,45);
    InsertEnd(&l,13);
    InsertEnd(&l,6);
    InsertFront(&l,5);
    InsertFront(&l,25);
    DeleteEnd(&l,&num);
    printf("Deleted element: %d\n",num);
}

最佳答案

对于初学者来说,数据成员full和函数IsListFull没有意义。在程序中,除了功能CreateList之外,数据成员full均未更改。

所以结构清单应该像

typedef struct list {
    int count;
    Node* head;
}List;


插入或删除节点的功能不得发出任何消息。相反,他们可以返回整数值0或1,说明相应的操作是否成功。

例如,可以通过以下方式定义函数InsertFront

int InsertFront( List *l, ListEntry x )
{
    Node *np = malloc( sizeof( Node ) );
    int success = np != NULL;

    if ( success )
    {
        np->entry = x;
        np->next = l->head;
        l->head = np;
        l->count++;
    }

    return success;
}


函数Insert无效。如果列表为空,则该函数具有未定义的行为。

函数DeleteEnd也是无效的。例如,当列表仅包含一个节点时,则while语句中的条件

while(temp->next->next != NULL){


调用未定义的行为。

并且功能Delete无效。例如,当列表仅包含一个节点并且指定位置等于0时。

最后,您忘记了使用函数CreateList在main中初始化列表。

关于c - 如何使用链接的实现在单链接列表中插入和删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59160356/

相关文章:

Android NDK/JNI UnsatisfiedLinkError With .So library

java - 难以理解链式哈希表的实现

c - 如何订购相互使用的 C 结构/函数声明?

c++ - 初始化结构指针的值

C Typedef 和结构问题

c++ - C/C++ 使用 tcmalloc

c - 为什么scanf将字符串放入char数组,直接输入却不行?

c - 使用 union 处理错误

java - Java 中的 JSON 解析和数据操作

algorithm - 集合操作的数据结构