gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:25:15 24 4
gpt4 key购买 nike

我尝试了使用链接实现存储数据的单链接列表程序。编译代码时,编译器未显示任何错误消息,而是将程序带到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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com