63,594
社区成员




// 数据结构.cpp : 定义控制台应用程序的入口点。
//
#include<iostream>
using namespace std;
struct Node
{
int value;
Node* next;
};
void creatlist(Node *head)//
{
head=new Node;
head->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{
Node* p=new Node;
p->value=i;
p->next=head->next;
head->next=p;
}
}
int print(Node *head)
{
Node *p=head;
while(p)
{
p=p->next;
cout<<p->value<<endl;
}
return 0;
}
int main()
{
Node* head;
creatlist(head);
print(head);
return 0;
}
struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)//二级指针
{
*head=new Node;
(*head)->value = 0; //*head要加括号
(*head)->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{
Node* p=new Node;
p->value=i;
p->next=*head; //注意都是*head
*head=p; //是*head=p,不是(*head)->next = p,那就循环了
}
}
int print(Node *head)
{
Node *p=head;
while(p)
{
cout<<p->value<<endl; //这两行要交换一下位置
p=p->next;
}
return 0;
}
int main()
{
Node* head;
creatlist(&head);
print(head); //倒序输出
return 0;
}
void del(Node **head)
{
cout<<"删除一个元素";
Node* q=*head;
Node* p=*head;
int i;
cin>>i;
if(p->value == i){
*head = p->next;
delete p;
}
else{
while(p && p->value != i){
q = p;
p = p->next;
}
if(p && p->value == i){
q->next = p->next;
delete p;
}
}
}
// 数据结构.cpp : 定义控制台应用程序的入口点。
//
#include<iostream>
using namespace std;
struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)//
{
(*head)=new Node;
(*head)->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{
Node* p=new Node;
p->value=i;
p->next=(*head)->next;
(*head)->next=p;
}
}
int print(Node *head)
{
Node *p=head;
while(p)
{ cout<<p->value<<endl;
p=p->next;
}
return 0;
}
del(Node **head)
{
cout<<"删除一个元素";
Node* q;
Node* p=*head;
int i;
cin>>i;
while(p->value!=i)
{
q=p;
p=p->next;
}
q->next=p->next;
delete p;
return 0;
}
int main()
{
Node *head;
creatlist(&head);
print(head);
del(&head);
print(head);
return 0;
}
p->next=(*head)->next;
(*head)->next=p;
照样工作。。。// 数据结构.cpp : 定义控制台应用程序的入口点。
//
#include<iostream>
using namespace std;
struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)//
{
*head=new Node;
*head->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{
Node* p=new Node;
p->value=i;
p->next=*head->next;
*head->next=p;
}
}
int print(Node **head)
{
Node *p=*head;
while(p)
{
p=p->next;
cout<<p->value<<endl;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
Node *head;
creatlist(&head);
print(&head);
return 0;
}
struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)//二级指针
{
*head=new Node;
(*head)->value = 0; //*head要加括号
(*head)->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{
Node* p=new Node;
p->value=i;
p->next=*head; //注意都是*head
*head=p; //是*head=p,不是(*head)->next = p,那就循环了
}
}
int print(Node *head)
{
Node *p=head;
while(p)
{
cout<<p->value<<endl; //这两行要交换一下位置
p=p->next;
}
return 0;
}
int main()
{
Node* head;
creatlist(&head);
print(head); //倒序输出
return 0;
}