2017-09-02 127 views
-1

爲什麼原型函數聲明以*開頭?這是由Reema Thareja撰寫的一本數據結構實現代碼。有人可以請幫助我。當使用開關盒時,正常的功能是由主功能調用的?C中使用指針的函數聲明

#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
#include <malloc.h> 

struct node 
{ 
    int data; 
    struct node *next; 
}; 

struct node *start = NULL;//this type of function declaration 
struct node *create_ll(struct node *); 
struct node *display(struct node *); 
struct node *insert_beg(struct node *); 
struct node *insert_end(struct node *); 
struct node *insert_before(struct node *); 
struct node *insert_after(struct node *); 
struct node *delete_beg(struct node *); 
struct node *delete_end(struct node *); 
struct node *delete_node(struct node *); 
struct node *delete_after(struct node *); 
struct node *delete_list(struct node *); 
struct node *sort_list(struct node *); 

int main(int argc, char *argv[]) { 
    int option; 
    do 
    { 
     printf("\n\n *****MAIN MENU *****"); 
     printf("\n 1: Create a list"); 
     printf("\n 2: Display the list"); 
     printf("\n 3: Add a node at the beginning"); 
     printf("\n 4: Add a node at the end"); 
     printf("\n 5: Add a node before a given node"); 
     printf("\n 6: Add a node after a given node"); 
     printf("\n 7: Delete a node from the beginning"); 
     printf("\n 8: Delete a node from the end"); 
     printf("\n 9: Delete a given node"); 
     printf("\n 10: Delete a node after a given node"); 
     printf("\n 11: Delete the entire list"); 
     printf("\n 12: Sort the list"); 
     printf("\n 13: EXIT"); 
     printf("\n\n Enter your option : "); 
     scanf("%d", &option); 
     switch(option) 
     { 
     case 1: start = create_ll(start); 
      printf("\n LINKED LIST CREATED"); 
      break; 
     case 2: start = display(start); 
      break; 
     case 3: start = insert_beg(start); 
      break; 
     case 4: start = insert_end(start); 
      break; 
     case 5: start = insert_before(start); 
      break; 
     case 6: start = insert_after(start); 
      break; 
     case 7: start = delete_beg(start); 
      break; 
     case 8: start = delete_end(start); 
      break; 
     case 9: start = delete_node(start); 
      break; 
     case 10: start = delete_after(start); 
      break; 
     case 11: start = delete_list(start); 
      printf("\n LINKED LIST DELETED"); 
      break; 
     case 12: start = sort_list(start); 
      break; 
     } 
    }while(option !=13); 
    getch(); 
    return 0; 
} 

這是功能使用格式*函數。我只包括第一個功能:

struct node *create_ll(struct node *start) 
    { 
     struct node *new_node, *ptr; 
     int num; 
     printf("\n Enter -1 to end"); 
     printf("\n Enter the data : "); 
     scanf("%d", &num); 
     while(num!=-1) 
     { 
     new_node = (struct node*)malloc(sizeof(struct node)); 
     new_node -> data=num; 
     if(start==NULL) 
     { 
     new_node -> next = NULL; 
     start = new_node; 
     } 
     else 
     { 
     ptr=start; 
     while(ptr->next!=NULL) 
     ptr=ptr->next; 
     ptr->next = new_node; 
     new_node->next=NULL; 
     } 
     printf("\n Enter the data : "); 
     scanf("%d", &num); 
     } 
     return start; 
    } 

    struct node *display(struct node *start) 
    { 
     struct node *ptr; 
     ptr = start; 
     while(ptr != NULL) 
     { 
     printf("\t %d", ptr -> data); 
     ptr = ptr -> next; 
     } 
     return start; 
    } 
+1

請縮進您的代碼,至少如果您希望別人讀取它。 –

回答

0

這些函數沒有什麼特別之處,只不過它們每次調用時都會返回鏈表的頭部。我們來看看這個場景:你傳遞給create_ll函數(例如)一個鏈表,當然你不會傳遞整個鏈表;當然,你只需傳遞鏈表第一個元素(節點)的指針即可。這就是我們所說的頭。這個函數將接受傳入的指針並完成它的工作,並完成它將返回一個指向傳入鏈表第一個元素的指針(確切的指針)。很簡單!

我希望這將讓你瞭解更多

0

*爲返回類型,不是函數名的一部分。該函數返回一個指向節點結構體的指針,用C語言編寫,如

struct node *