[英]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,因此分配會更容易(這樣我們不必每次都鍵入 struct)並且我允許給定任何大小名稱參數。
哨兵
現在更重要的更改將允許您進一步簡化代碼。 這是一個 wiki 頁面來了解它是什么Sentinel ,但簡單地說,它用作頭節點,它包含一個我們不想讀取的值,因此允許將頭作為簡單指針而不是雙精度指針傳遞指針!
錯誤.h
我添加了此標頭以允許以更簡潔的方式管理錯誤異常,例如當您要刪除節點的索引超出范圍時。
釋放鏈表
我已經實現了一個簡單的 free 函數,一旦你完成了所有需要的操作,它將釋放你的鏈表,否則我們將面臨內存泄漏!
#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);
}
我已經測試了所有功能以及邊緣情況和異常,它們工作得很好,請不要猶豫,提出任何問題! 此致,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.