在嘗試在鏈表中添加和列印中間代碼時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
uj5u.com熱心網友回復:
所以這是我處理您的代碼的方式。我將完成并解釋這個答案,但只是為了讓你有一些東西可以看,我會給出整個代碼
MAX_NAME_SIZE
此宏將允許您快速更改名稱引數的最大允許大小。
鏈接串列的更改:
讓我們從我更改結構的方式開始,我已將其轉換為 typedef,因此分配會更容易(這樣我們不必每次都鍵入 struct)并且我允許給定任何大小名稱引數。
哨兵
現在更重要的更改將允許您進一步簡化代碼。這是一個 wiki 頁面來了解它是什么Sentinel,但簡單地說,它用作頭節點,它包含一個我們不想讀取的值,因此允許將頭作為簡單指標而不是雙精度指標傳遞指標!
錯誤.h
我添加了此標頭以允許以更簡潔的方式管理錯誤例外,例如當您要洗掉節點的索引超出范圍時。
釋放鏈表
我已經實作了一個簡單的免費函式,一旦你完成了所有需要的操作,它就會釋放你的鏈表,否則我們將面臨記憶體泄漏!
#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);
}
我已經測驗了所有功能以及邊緣情況和例外,它們作業得很好,請不要猶豫,提出任何問題!此致,
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/485435.html
下一篇:fgets()沒有被執行
