C++ 结构中typedef的奇怪行为
我在C++ 结构中typedef的奇怪行为,c++,struct,C++,Struct,我在C++中制作了一个单链表的程序,因此在类中使用了struct。我知道typedef与struct一起使用,因此声明之后不需要使用struct关键字。但是当我不使用typedef时,即使在将来的声明中不使用struct关键字,代码也会成功编译,但当我使用typedef时,它不会编译。 代码如下: #include <iostream> using namespace std; struct node{ int data; node* next; }
C++
中制作了一个单链表
的程序,因此在类中使用了struct
。我知道typedef
与struct
一起使用,因此声明之后不需要使用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;
}
#包括
使用名称空间std;
结构节点{
int数据;
节点*下一步;
}*头部;
类单表{
公众:
结构节点*创建节点(int);
void insert_begin();
无效插入位置();
void insert_last();
void delete_pos();
无效排序();
void display();
};
int main()
{
int选择、节点、元素、位置、i;
单列表sl;
node*head=NULL;
而(1)
{
CUT< P>在C++中定义/声明对象时,不需要使用<代码>结构> /COD>关键字。C中的方法不在C++中。 < P>下面的代码声明<代码>头<代码>是指向“代码>节点< /COD>变量.< /P>的指针。
struct node{
int data;
node* next;
}*head;
但是,下面将头定义为指向节点的类型指针的别名
typedef struct node{
int data;
node* next;
}*head;
定义struct
struct node{
int data;
node* next;
}*head;
C++中的名称<代码>节点< /C>可不使用<代码> TyPulfF。您必须考虑C,它需要<代码> TyPulf< /C> >
在C++中,可以使用:
struct node{
int data;
node* next;
};
node* head;
在C语言中,您需要使用:
struct node{
int data;
struct node* next;
};
struct node* head;
或
在哪里使用typedef?在定义新节点时,我们是否应该在不定义时使用struct node*ntypedef@ashish你可以使用它,但是你也可以省略它。C++中不需要它。谢谢。C++和C++有点混淆,因为我在C++中编写代码的风格是C类。
// Define the struct and a typedef in one statement.
typedef struct node{
int data;
struct node* next;
} node;
node* head;