2014-09-28 57 views
-7

代碼:賦值時將整數指針,未在C語言中施放[默認啓用]

#include<stdio.h> 
#include<stdlib.h> 
struct node 
{ 
    int data; 
    struct node *next; 
}; 
struct node *head,*temp; 
int insert_end(int); 
int insert_begin(int); 
int display(void); 
int delete_end(void); 
int delete_begin(void); 

int main() 
{ 
    head=(struct node *)malloc(sizeof(struct node)); 
    temp=(struct node *)malloc(sizeof(struct node)); 
    head->next = -1; 
    int choice,a,b; 
label: 
printf("\n\t1.insert_end 2. insert_begin 3.\n delete_end 4.delete_begin \n5.display 6.exit "); 
scanf("%d",&choice); 
switch (choice) 
{ 
    case 1: 
{ 
    printf("\tenter the no to be insert at the end "); 
    scanf("%d",&a); 
    insert_end (a); 

    goto label; 

} 
case 2: 
printf("\tenter the no to be insert at the beginnig "); 
scanf("%d",&a); 
insert_begin (a); 

goto label; 

case 3: 
{ 
    b = delete_end(); 
    if (b == 0) 
    {  
     printf(" \t\tfailed ! ! \n");} 
     else 
     { 
      printf(" \t\t success ! ! \n");} 

     goto label; 
} 

case 4: 
{ 
    b = delete_begin(); 
    if (b == 0) 
    { 
     printf(" \t\tfailed ! ! \n");} 
    else 
    { 
     printf(" \t\t success ! ! \n");} 

     goto label; 
    } 
case 5: 
{ 
    temp =head ; 
    display(); 
    goto label; 
} 
case 6: 
{ 
    exit (0); 
} 
default: 
{ 
    printf("wrong options"); 
    goto label; 
} 

} 
} 

int insert_end (int a) 
{ 
temp = head; 
while (1) 
{ 
    if (temp->next == -1) 
    { 
     temp->data =a; 
     temp->next =0; 
     return 0; 
    } 
     else if (temp->next == 0) 
     { 
      temp->next = malloc(sizeof (struct node)); 
      temp =temp->next; 
      temp->data =a; 
      temp->next =0; 
      return 0; 
     } 
     else 
     { 
      temp = temp->next; 
     } 
    } 
} 


int display() 
{ 
temp=head; 
while (1) 
{ 
    if (head->next == -1) 
    { 
     printf("\tEmpty ! ! !\t\n"); 
     break; 
    } 
    else if ((head->next == 0)) 
    { 
     printf(" %d ", head->data); 
     break; 
    } 
else if (temp->next != 0) 
{ 
    printf (" %d ->", temp->data); 
    temp =temp->next ; 
} 
else if (temp->next == 0) 
{ 
    printf(" %d ", temp->data); 
    return 0; 
} 
} 
} 


int insert_begin(int a) 
{ 
if(head->next == -1) 
{ 
    head->data =a; 
    head->next=0; 
    return 1; 
} 
else 
{ 
    temp =malloc(sizeof (struct node)); 
    temp->data =a; 
    temp->next = head; 
    head = temp; 
    return 1; 
} 
} 

int delete_end (void) 
{ 
temp=head; 
while (1) 
{ 
    if (head->next == -1) 
    { 
     return 0; 
    } 
    else if (head->next == 0) 
    { 
     head->next = -1; 
     return 1; 
    } 
    else if (temp->next->next == 0) 
    { 
     temp->next =0; 
     return 1; 
    } 
    else 
    temp=temp->next; 
} 
} 


int delete_begin(void) 
{ 
if (head->next == -1) 
{ 
    return 0; 
} 
    else if (head -> next == 0) 
    { 
      head->next = -1; 
      return 1; 
    } 
    else 
    { 
     head=head->next; 
     return 1; 
    } 
} 

error: 

linkedlist1.c: In function ‘main’: 

linkedlist1.c:19:12: warning: assignment makes pointer from integer without a cast [enabled by default] 

head->next = -1; 
     ^
linkedlist1.c: In function ‘insert_end’: 

linkedlist1.c:87:16: warning: comparison between pointer and integer [enabled by default] 
if (temp->next == -1) 
      ^
linkedlist1.c: In function ‘display’: 

linkedlist1.c:115:16: warning: comparison between pointer and integer [enabled by default] 
if (head->next == -1) 
      ^
linkedlist1.c: In function ‘insert_begin’: 

linkedlist1.c:141:15: warning: comparison between pointer and integer [enabled by default] 
if(head->next == -1) 
     ^
linkedlist1.c: In function ‘delete_end’: 

linkedlist1.c:162:16: warning: comparison between pointer and integer [enabled by default] 
if (head->next == -1) 
      ^
linkedlist1.c:168:12: warning: assignment makes pointer from integer without a cast [enabled by default] 
head->next = -1; 
     ^
linkedlist1.c: In function ‘delete_begin’: 

linkedlist1.c:184:17: warning: comparison between pointer and integer [enabled by default] 
if (head->next == -1) 
      ^
linkedlist1.c:190:12: warning: assignment makes pointer from integer without a cast [enabled by default] 
head->next = -1; 
     ^

當我試着使用GCC編譯,我結束了,上面寫着警告「分配,使指針從整如果沒有投射[默認啓用],請幫我解決上述警告,我不知道錯誤是什麼,

+1

'next'是一個指針,'-1'是一個整數。將一個指派給另一個沒有多大意義。 – 2014-09-28 10:08:14

+1

我建議使用'NULL'而不是'-1'。 – BLUEPIXY 2014-09-28 10:12:54

回答

0

用你的「警告」,你還會得到一個字符串數字,其中出現「warning」 請給出出現錯誤的字符串

我沒有在第一次看到你的警告

只需更換-1 NULL或更換-1 (void *)-1

課程標準應寫入NULL,但如果你真的願意,你可以寫(void *)-1

+0

head-> next = -1; – user3764370 2014-09-28 10:08:43

+0

謝謝我將它改爲..... head-> next =(struct node *) - 1 ..... it works – user3764370 2014-09-28 10:33:22

+0

good :)實際上C中的NULL被定義爲'#define NULL((void * )0)' – 2014-09-28 10:51:33

0

編譯器不被滿足聲明

head->next = -1; 

這將是更好,如果你會使用NULL,而不是-1和寫入

head->next = NULL; 
相關問題