2016-03-22 6 views
-2

singly linked listのプログラムをC++に作成していたので、クラス内でstructを使用しました。私はがstructで使用されていることを知っています。そのため、後で宣言にはstructキーワードを使用する必要はありません。しかし、typedefを使用しないと、将来の宣言でstructキーワードを使用しなくてもコードは正常にコンパイルされますが、typedefを使用するとコンパイルされません。ここ はコードです:struct内のtypedefの奇妙な振る舞い

#include <iostream> 
using namespace std; 

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

class single_list{ 
    public: 
     struct node* create_node(int); 
     void insert_begin(); 
     void insert_pos(); 
     void insert_last(); 
     void delete_pos(); 
     void sort(); 
     void display(); 
}; 

int main() 
{ 
    int choice,nodes,element,position,i; 
    single_list sl; 
    node* head = NULL; 
    while(1) 
    { 
     cout<<endl<<"List of operations"<<endl; 
     cout<<"1: Insert node at the beginning"<<endl; 
     cout<<"2: Insert node at a specific position"<<endl; 
     cout<<"3: Insert node at the last"<<endl; 
     cout<<"4: Delete a node at specific position"<<endl; 
     cout<<"5: Sorting the linked list"<<endl; 
     cout<<"6: Display the linked list"<<endl; 
     cout<<"Enter your choice"<<endl; 
     cin>>choice; 

     switch(choice) 
     { 
      case 1: 
       cout<<"Inserting node at the beninning"<<endl; 
       sl.insert_begin(); 
       cout<<endl; 
       break; 

      case 2: 
       cout<<"Inserting node at a specific position"<<endl; 
       sl.insert_pos(); 
       cout<<endl; 
       break; 

      case 3: 
       cout<<"Inserting node at the last place"<<endl; 
       sl.insert_last(); 
       cout<<endl; 
       break; 

      case 4: 
       cout<<"Deleting node at specific position"<<endl; 
       sl.delete_pos(); 
       cout<<endl; 
       break; 

      case 5: 
       cout<<"Sorting the linked list"<<endl; 
       sl.sort(); 
       cout<<endl; 
       break; 

      case 6: 
       cout<<"Displaying the linked list"<<endl; 
       sl.display(); 
       cout<<endl; 
       break; 

      default: 
       cout<<"Wrong Choice"<<endl;      
     } 
    } 
} 

node *single_list::create_node(int data) 
{ 
    node* temp; 
    temp = new node; 
    temp->data = data; 
    temp->next = NULL; 
    return temp; 
} 

void single_list::insert_begin() 
{ 
    cout<<"Enter value to be inserted"<<endl; 
    int data; 
    cin>>data; 
    node* temp; 
    node* p; 
    temp = create_node(data); 

    if (head == NULL) 
    { 
     head = temp; 
     head->next = NULL; 
    } 
    else 
    { 
     p = head; 
     head = temp; 
     head->next = p; 
    } 
} 

void single_list::insert_pos() 
{ 
    cout<<"Enter the position at which you want to enter the number"<<endl; 
    int pos; 
    cin>>pos; 
    cout<<"Enter the data of the node"<<endl; 
    int data; 
    node* t; 
    t = head; 
    node* temp1; 
    temp1->data = data; 
    for(int i=1;i<pos;i++) 
    { 
     t = t->next; 
    } 
    if(pos == 1) 
    { 
     if(head == NULL) 
     { 
      head = temp1; 
      head->next = NULL; 
     } 
     else 
     { 
      temp1->next = t->next; 
      t->next = temp1;  
     } 
    } 
    else 
    { 
     cout<<"Position out of range"<<endl; 
    } 

} 

void single_list::insert_last() 
{ 
    cout<<"Enter the data of the number"<<endl; 
    int data; 
    cin>>data; 
    node* temp1; 
    temp1->data = data; 
    temp1->next = NULL; 
    node* t; 
    t = head; 
    while(t != NULL) 
    { 
     t = t->next; 
    } 
    t->next = temp1; 
} 
+0

typedefはどこですか? – niyasc

答えて

1

あなたはnodeはC++でtypedefすることなく使用することができるstruct

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

名を定義した後。 typedefが必要なCを考える必要があります。 C++で

、それは使用することがOKである:

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

node* head; 

、あなたが使用する必要がありますが:C++で

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

struct node* head; 

または

// Define the struct and a typedef in one statement. 
typedef struct node{   
    int data; 
    struct node* next; 
} node; 

node* head; 
+0

ありがとうございます。私はC++のコードを書いているスタイルがCで、クラスは – ashish

+0

@ashishですので、あなたは歓迎です。私は喜んで助けてくれました。 –

0

あなたはC++でオブジェクトを宣言/定義するときstructキーワードを使用する必要はありません。これはC言語ではなくC言語での方法です。

+0

新しいノードを定義するにはtypedefを定義しないときには** struct node * n **を使用しないでください。 – ashish

+0

@ashishを使用することはできますが、省略することもできます。 C++では必須ではありません – ixSci

0

headは、変数nodeへのポインタであると宣言しています。

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

しかしながら、以下はnodeのタイプのポインタのエイリアスであることheadを定義します。

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