1.
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node* next;
Node(): next(NULL){}
Node(int _val): val(_val),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node* head=new Node(1);
print(head);
return 0;
}
2.
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node* next;
Node(): next(NULL){}
Node(int _val): val(_val),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node* head=new Node(1);
auto a=new Node(2);
head->next=a;
auto b=new Node(3);
a->next=b;
print(head);
return 0;
}
3.头插
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node* next;
Node(): next(NULL){}
Node(int _val): val(_val),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node* head=new Node(1);
auto a=new Node(2);
head->next=a;
auto b=new Node(3);
a->next=b;
auto c=new Node(4);
c->next=a;
head->next=c;
print(head);
return 0;
}
4.
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node* next;
Node(): next(NULL){}
Node(int _val): val(_val),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node* head=new Node(1);
auto a=new Node(2);
head->next=a;
auto b=new Node(3);
a->next=b;
auto c=new Node(4);
c->next=head->next;
head->next=c;
print(head);
return 0;
}
5.删除
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node* next;
Node(): next(NULL){}
Node(int _val): val(_val),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node* head=new Node(1);
auto a=new Node(2);
head->next=a;
auto b=new Node(3);
a->next=b;
auto c=new Node(4);
c->next=head->next;
head->next=c;
c->next=c->next->next;
delete a;
print(head);
return 0;
}
6.
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node* next;
Node(): next(NULL){}
Node(int _val): val(_val),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node* head=new Node(1);
auto a=new Node(2);
head->next=a;
auto b=new Node(3);
a->next=b;
auto c=new Node(4);
c->next=head->next;
head->next=c;
c->next=c->next->next;
delete a;
auto p=head;
head=head->next;
delete p;
print(head);
return 0;
}