结构中typedef的奇怪行为
Strange behaviour of typedef in struct
我在C++
中制作了一个singly linked list
的程序,所以在类中使用了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;
}
定义struct
后
struct node{
int data;
node* next;
}*head;
在C++中,名称node
可以在没有typedef
的情况下使用。你一定在想C,它需要typedef
。
在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;
在C++中定义/声明对象时,不需要使用struct
关键字。在C中是这样,而不是在C++中。
以下代码声明head
是指向node
变量的指针。
struct node{
int data;
node* next;
}*head;
但是,下面将把head
定义为指向node
的类型指针的别名。
typedef struct node{
int data;
node* next;
}*head;
相关文章:
- 如何创建 typedef 结构的前向声明
- 无法转发声明C typedef结构-正在查找其他建议
- 如何使用 SWIG 接口访问 python 中的 C++ typedef'd 结构
- 具有未声明/未定义类型的 typedef 结构
- 在 CPP 中初始化 typedef 结构变量
- C++ 无法在 typedef 结构中重载<运算符
- 在 typedef 结构中声明一个数组
- 从 csv 文件中获取不同类型的数据,并将其放入 C++ 中的 typedef 结构中
- 为什么 typedef 结构允许默认初始值设定项?
- 为什么我不能像正常一样初始化 typedef 结构字符串或字符?
- 在Typedef结构中,下划线意味着什么
- 来自 typedef 结构的错误
- 在Typedef结构(C )中超载运算符
- C++中具有对象名称的typedef结构的正向声明
- 为什么Typedef结构会产生链路故障
- 在Typedef结构中定义结构
- 类中typedef结构的奇怪行为
- C/C++ 中的套接字编程 - 接收由 typedef 结构定义的消息
- 扩展typedef结构(与VC++11兼容)
- 为什么子类在C++中看不到父类的 typedef 结构?