显示、打印和在正确位置删除的问题(链表)
Problem with Displaying, Printing in between and deleting in correct position (linked List)
尝试在链表中添加和打印中间代码 stops
时。当它执行 运行 时(删除某些功能后)删除和中间 si 不在定义的位置。
#include <stdio.h>
#include <stdlib.h>
struct FlavorList{
char name[100];
struct FlavorList* next ;
};
void addAtfront(struct FlavorList **head){
struct FlavorList *new_cake = (struct FlavorList*)malloc(sizeof(struct FlavorList));
printf("First cake: ");
scanf("%s",new_cake->name);
new_cake->next = *head ;
*head = new_cake ;
}
void addAtback(struct FlavorList **head){
struct FlavorList* new_cake = (struct FlavorList*) malloc(sizeof(struct FlavorList));
printf("Second cake : ");
scanf("%s",new_cake->name);
new_cake->next = NULL;
struct FlavorList *last = *head;
if (*head == NULL) {
*head = new_cake;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_cake;
return;
}
void addInbetween(struct FlavorList ** head , int pos){
struct FlavorList * new_cake = (struct FlavorList *) malloc(sizeof(struct FlavorList ));
printf("Between cake : ");
scanf("%s",new_cake->name);
new_cake->next = NULL;
struct FlavorList *zig = *head ;
int count = 0 ;
while(zig!=NULL && count<=pos){
count ++ ;
zig=zig->next ;
}
new_cake->next = zig->next ;
zig->next = new_cake ;
}
void delete(struct FlavorList **head, int pos){
struct FlavorList*current=*head;
struct FlavorList*previous=*head;
if(head ==NULL) {
printf("List is empty");
}
else if (pos==1) {
*head=current->next;
free(current);
current=NULL;
}
else {
while(pos!=1) {
previous=current;
current=current->next;
pos--;
}
previous->next=NULL;
free(current);
current=NULL;
}
}
void display(struct FlavorList **head){
struct FlavorList *zig = * head ;
while(zig != NULL){
printf("List of cakes");
printf("%s",zig->name);
printf(" ");
zig=zig->next;
}
printf("\n");
}
int main(){
struct FlavorList *head = NULL ;
printf("--------------------------------\n");
printf("-----------Cake Shop------------\n");
printf("--------------------------------\n");
addAtfront(&head);
addAtback(&head);
addInbetween(&head,2);
display(&head);
printf("Deleting cake at position 1: \n");
delete(&head,1);
printf("Present of now:\n");
display(&head);
}
我得到的输出。
--------------------------------
-----------Cake Shop------------
--------------------------------
First cake: First
Second cake : Second
Between cake : Between
...Program finished with exit code 0
Press ENTER to exit console.
当我再添加一个addatfront和addatback时,我可以到达显示和删除部分,但它们不在定义的位置。
之后的输出:
--------------------------------
-----------Cake Shop------------
--------------------------------
First cake: First1
First cake: First2
Second cake : Second1
Second cake : Second2
Between cake : Middle
List of cakes
First2
First1
Second1
Second2
Middle
Deleting cake at position 1:
Present of now:
List of cakes
First1
Second1
Second2
Middle
这就是我处理您的代码的方式。
我将完成并解释这个答案,但只是为了让您有所了解,我将提供整个代码
MAX_NAME_SIZE
此宏可让您快速更改名称参数的最大允许大小。
链表变化:
让我们从我更改结构的方式开始,我已将其转换为 typedef,以便更容易进行分配(这样我们就不必每次都键入结构)并且我允许任何给名称参数的大小。
哨兵
现在更重要的更改将使您能够进一步简化代码。这是一个了解它是什么的 wiki 页面 Sentinel,但简单地说,它用作头节点,它包含一个我们永远不想读取的值,因此允许将头作为简单指针传递而不是双指针!
err.h
我添加了这个 header 以允许更干净的方式来管理错误异常,例如当您要删除节点的索引超出范围时。
释放链表
我已经实现了一个简单的释放函数,一旦你用它完成了所有需要的操作,它就会释放你的链表,否则我们将面临内存泄漏!
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#define MAX_NAME_SIZE 100
typedef struct FlavorList
{
char *name;
struct FlavorList *next;
}FlavorList;
FlavorList * FlavorListBuildSentinel()
{
FlavorList *cake_sentinel = malloc(sizeof(FlavorList));
cake_sentinel->name = malloc(sizeof(char));
cake_sentinel->next = NULL;
return cake_sentinel;
}
void addAtfront(FlavorList *head)
{
printf("Adding a new cake in the front!\n");
char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
scanf("%s",new_name);
FlavorList *new_cake = malloc(sizeof(FlavorList));
new_cake->name = new_name;
new_cake->next = head->next;
head->next = new_cake;
}
void addAtback(FlavorList *head)
{
printf("Adding a new cake in the back!\n");
FlavorList *new_cake = malloc(sizeof(FlavorList));
while(head->next!=NULL)
{
head = head->next;
}
char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
scanf("%s",new_name);
new_cake->name = new_name;
new_cake->next = head->next;
head->next = new_cake;
}
void addInbetween(FlavorList *head,int pos)
{
printf("Adding a new cake at pos: %d!\n",pos);
while(pos && head->next!=NULL)
{
head = head->next;
pos--;
}
//If the pos is still not 0 and the list is out of range, we quit!
if(pos!=0)
{
errx(1, "Index out of range!");
}
//If not do the insertion
FlavorList *new_cake = malloc(sizeof(FlavorList));
char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
scanf("%s",new_name);
new_cake->name = new_name;
new_cake->next = head->next;
head->next = new_cake;
}
void delete(FlavorList *head, int pos)
{
while(pos && head->next!=NULL)
{
head = head->next;
pos--;
}
//If the pos is still not 0 and the list is out of range, we quit!
if(pos!=0 || head->next == NULL)
{
errx(1, "Index out of range!");
}
FlavorList * tmp = head->next;
if(head->next->next!=NULL)
{
head->next = head->next->next;
}
else
{
head->next = NULL;
}
free(tmp->name);
free(tmp);
}
void display(FlavorList* head)
{
printf("List of cake names\n");
int cpt = 1;
while(head->next)
{
head = head->next;
printf("Cake name at position: %d is %s\n",cpt,(head->name));
cpt++;
}
printf("\n");
}
//Frees the allocated node
void FlavorListFree(FlavorList* head)
{
FlavorList* previous;
while (head)
{
previous = head;
head = head->next;
free(previous->name);
free(previous);
}
}
int main()
{
FlavorList * head = FlavorListBuildSentinel();
printf("--------------------------------\n");
printf("-----------Cake Shop------------\n");
printf("--------------------------------\n");
addAtfront(head);
addAtback(head);
addInbetween(head,2);
display(head);
printf("Deleting cake at position 1: \n");
delete(head,1);
printf("Present of now:\n");
display(head);
FlavorListFree(head);
}
我已经测试了所有功能以及边缘情况和异常,它们工作正常,请不要犹豫,提出任何问题!
最好的问候,
尝试在链表中添加和打印中间代码 stops
时。当它执行 运行 时(删除某些功能后)删除和中间 si 不在定义的位置。
#include <stdio.h>
#include <stdlib.h>
struct FlavorList{
char name[100];
struct FlavorList* next ;
};
void addAtfront(struct FlavorList **head){
struct FlavorList *new_cake = (struct FlavorList*)malloc(sizeof(struct FlavorList));
printf("First cake: ");
scanf("%s",new_cake->name);
new_cake->next = *head ;
*head = new_cake ;
}
void addAtback(struct FlavorList **head){
struct FlavorList* new_cake = (struct FlavorList*) malloc(sizeof(struct FlavorList));
printf("Second cake : ");
scanf("%s",new_cake->name);
new_cake->next = NULL;
struct FlavorList *last = *head;
if (*head == NULL) {
*head = new_cake;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_cake;
return;
}
void addInbetween(struct FlavorList ** head , int pos){
struct FlavorList * new_cake = (struct FlavorList *) malloc(sizeof(struct FlavorList ));
printf("Between cake : ");
scanf("%s",new_cake->name);
new_cake->next = NULL;
struct FlavorList *zig = *head ;
int count = 0 ;
while(zig!=NULL && count<=pos){
count ++ ;
zig=zig->next ;
}
new_cake->next = zig->next ;
zig->next = new_cake ;
}
void delete(struct FlavorList **head, int pos){
struct FlavorList*current=*head;
struct FlavorList*previous=*head;
if(head ==NULL) {
printf("List is empty");
}
else if (pos==1) {
*head=current->next;
free(current);
current=NULL;
}
else {
while(pos!=1) {
previous=current;
current=current->next;
pos--;
}
previous->next=NULL;
free(current);
current=NULL;
}
}
void display(struct FlavorList **head){
struct FlavorList *zig = * head ;
while(zig != NULL){
printf("List of cakes");
printf("%s",zig->name);
printf(" ");
zig=zig->next;
}
printf("\n");
}
int main(){
struct FlavorList *head = NULL ;
printf("--------------------------------\n");
printf("-----------Cake Shop------------\n");
printf("--------------------------------\n");
addAtfront(&head);
addAtback(&head);
addInbetween(&head,2);
display(&head);
printf("Deleting cake at position 1: \n");
delete(&head,1);
printf("Present of now:\n");
display(&head);
}
我得到的输出。
--------------------------------
-----------Cake Shop------------
--------------------------------
First cake: First
Second cake : Second
Between cake : Between
...Program finished with exit code 0
Press ENTER to exit console.
当我再添加一个addatfront和addatback时,我可以到达显示和删除部分,但它们不在定义的位置。 之后的输出:
--------------------------------
-----------Cake Shop------------
--------------------------------
First cake: First1
First cake: First2
Second cake : Second1
Second cake : Second2
Between cake : Middle
List of cakes
First2
First1
Second1
Second2
Middle
Deleting cake at position 1:
Present of now:
List of cakes
First1
Second1
Second2
Middle
这就是我处理您的代码的方式。 我将完成并解释这个答案,但只是为了让您有所了解,我将提供整个代码
MAX_NAME_SIZE
此宏可让您快速更改名称参数的最大允许大小。
链表变化:
让我们从我更改结构的方式开始,我已将其转换为 typedef,以便更容易进行分配(这样我们就不必每次都键入结构)并且我允许任何给名称参数的大小。
哨兵
现在更重要的更改将使您能够进一步简化代码。这是一个了解它是什么的 wiki 页面 Sentinel,但简单地说,它用作头节点,它包含一个我们永远不想读取的值,因此允许将头作为简单指针传递而不是双指针!
err.h
我添加了这个 header 以允许更干净的方式来管理错误异常,例如当您要删除节点的索引超出范围时。
释放链表
我已经实现了一个简单的释放函数,一旦你用它完成了所有需要的操作,它就会释放你的链表,否则我们将面临内存泄漏!
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#define MAX_NAME_SIZE 100
typedef struct FlavorList
{
char *name;
struct FlavorList *next;
}FlavorList;
FlavorList * FlavorListBuildSentinel()
{
FlavorList *cake_sentinel = malloc(sizeof(FlavorList));
cake_sentinel->name = malloc(sizeof(char));
cake_sentinel->next = NULL;
return cake_sentinel;
}
void addAtfront(FlavorList *head)
{
printf("Adding a new cake in the front!\n");
char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
scanf("%s",new_name);
FlavorList *new_cake = malloc(sizeof(FlavorList));
new_cake->name = new_name;
new_cake->next = head->next;
head->next = new_cake;
}
void addAtback(FlavorList *head)
{
printf("Adding a new cake in the back!\n");
FlavorList *new_cake = malloc(sizeof(FlavorList));
while(head->next!=NULL)
{
head = head->next;
}
char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
scanf("%s",new_name);
new_cake->name = new_name;
new_cake->next = head->next;
head->next = new_cake;
}
void addInbetween(FlavorList *head,int pos)
{
printf("Adding a new cake at pos: %d!\n",pos);
while(pos && head->next!=NULL)
{
head = head->next;
pos--;
}
//If the pos is still not 0 and the list is out of range, we quit!
if(pos!=0)
{
errx(1, "Index out of range!");
}
//If not do the insertion
FlavorList *new_cake = malloc(sizeof(FlavorList));
char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
scanf("%s",new_name);
new_cake->name = new_name;
new_cake->next = head->next;
head->next = new_cake;
}
void delete(FlavorList *head, int pos)
{
while(pos && head->next!=NULL)
{
head = head->next;
pos--;
}
//If the pos is still not 0 and the list is out of range, we quit!
if(pos!=0 || head->next == NULL)
{
errx(1, "Index out of range!");
}
FlavorList * tmp = head->next;
if(head->next->next!=NULL)
{
head->next = head->next->next;
}
else
{
head->next = NULL;
}
free(tmp->name);
free(tmp);
}
void display(FlavorList* head)
{
printf("List of cake names\n");
int cpt = 1;
while(head->next)
{
head = head->next;
printf("Cake name at position: %d is %s\n",cpt,(head->name));
cpt++;
}
printf("\n");
}
//Frees the allocated node
void FlavorListFree(FlavorList* head)
{
FlavorList* previous;
while (head)
{
previous = head;
head = head->next;
free(previous->name);
free(previous);
}
}
int main()
{
FlavorList * head = FlavorListBuildSentinel();
printf("--------------------------------\n");
printf("-----------Cake Shop------------\n");
printf("--------------------------------\n");
addAtfront(head);
addAtback(head);
addInbetween(head,2);
display(head);
printf("Deleting cake at position 1: \n");
delete(head,1);
printf("Present of now:\n");
display(head);
FlavorListFree(head);
}
我已经测试了所有功能以及边缘情况和异常,它们工作正常,请不要犹豫,提出任何问题! 最好的问候,