概述
//C语言双向链表
#include <stdio.h>
struct Node{ //结构体节点
int val;
struct Node *next,*pre;
Node(int v){ //构造函数
val=v;
next=NULL;
pre=NULL;
}
};
void push_back(struct Node *head,int val) {//插入尾节点
struct Node *tmp=new struct Node(val);
while(head->next!=NULL){
head=head->next;
}
head->next=tmp;
tmp->pre = head;
}
void push_front(struct Node **head,int val){//插入首节点
struct Node *tmp=new struct Node(val);
tmp->next=*head;
(*head)->pre=tmp;
*head=tmp;
}
void pop_front(struct Node **head){//删除首节点
*head=(*head)->next;
delete (*head)->pre;
(*head)->pre=NULL;
}
void pop_back(struct Node *head){//删除尾节点
while(head->next!=NULL){
head=head->next;
}
head->pre->next=NULL;
delete head;
}
void insert(struct Node *head,int pos,int val){//插入节点
struct Node *tmp=new struct Node(val);
while(head->next!=NULL&&pos-->0){
head=head->next;
}
tmp->pre=head;
tmp->next=head->next;
head->next=tmp;
}
void erase(struct Node *head,int pos){//删除节点
if(pos==0) return;
while(head->next!=NULL&&pos-->0){
head=head->next;
}
head->pre->next=head->next;
delete head;
}
void print_node(struct Node *head){//打印链表
while(head!=NULL){
printf("%d ",head->val);
head=head->next;
}
printf("n");
}
int get_size(struct Node *head){//获取链表长度
int s=0;
while(head!=NULL){
head=head->next;
s++;
}
return s;
}
int main(int argc, char *argv[])
{
struct Node *list=new struct Node(5);
push_back(list,8);
push_back(list,6);
push_back(list,3);
print_node(list);
pop_back(list);
print_node(list);
push_front(&list,1);
print_node(list);
pop_front(&list);
print_node(list);
insert(list,1,9);
print_node(list);
erase(list,2);
print_node(list);
printf("size=%dn",get_size(list));
return 0;
}
/*
5 8 6 3
5 8 6
1 5 8 6
5 8 6
5 8 9 6
5 8 6
size=3
*/
//类链表结构与迭代器
#include <iostream>
#include <string>
using namespace std;
class list
{
public:
struct Node
{
int val;
Node *next;
Node(int v){
val=v;
next=NULL;
}
Node(){
val=0,next=NULL;
}
};
class iterator
{
public:
iterator(Node *node){ptr=node;}
iterator& operator ++(){
ptr=ptr->next;
return *this;
}
iterator* operator ++(int){
iterator *t=this;
ptr=ptr->next;
return t;
}
int& operator *(){
return ptr->val;
}
bool operator ==(const iterator &it){
return ptr==it.ptr;
}
bool operator !=(const iterator &it){
return ptr!=it.ptr;
}
private:
Node *ptr;
};
list():head(NULL),tail(NULL),m_size(0){}
~list(){clear();}
void push_back(int v)
{
Node *tmp=new Node(v);
++m_size;
if(head==NULL){
head=tail=tmp;
return;
}
tail->next=tmp;
tail=tmp;
}
void pop_back()
{
Node *t=head,*pre;
while(t!=tail)
{
pre=t;
t=t->next;
}
delete tail;
tail=pre;
tail->next=NULL;
--m_size;
}
void insert(int pos,int v)
{
Node *tmp=new Node(v);
if(pos==0){
tmp->next=head;
head=tmp;
}
else if(pos==-1 || pos>=m_size){
push_back(v);
return;
}
else{
Node *t=head,*pre;
while(t!=NULL && pos-->0){
pre=t;
t=t->next;
}
pre->next= tmp;
tmp->next=t;
}
++m_size;
}
void remove(int pos)
{
Node* tmp=NULL;
if(pos==0){
tmp=head->next;
delete head;
head=tmp;
}
else if(pos==-1 || pos>=m_size){
pop_back();
return;
}
else{
Node *t=head,*pre;
while(t!=NULL && pos-->0){
pre=t;
t=t->next;
}
pre->next=t->next;
delete t;
}
m_size--;
}
void clear(){
if(m_size==0) return;
release(head);
head=tail=NULL;
m_size=0;
}
void print_node(){
Node *t=head;
while(t!=NULL){
cout<<t->val<<" ";
t=t->next;
}
cout<<endl;
}
Node* begin(){return head;}
Node* end(){return NULL;}
private:
void release(Node *node){
if(node == NULL) return;
release(node->next);
delete node;
}
private:
Node *head,*tail;
int m_size;
};
int main(int argc, char *argv[])
{
list l1;
l1.push_back(3);
l1.push_back(7);
l1.push_back(4);
l1.push_back(5);
l1.print_node();
l1.insert(0,9);
l1.print_node();
l1.insert(3,8);
l1.print_node();
l1.remove(0);
l1.print_node();
l1.remove(3);
l1.print_node();
cout<<"this is iterator:"<<endl;
for(list::iterator it=l1.begin();it!=l1.end();it++)
cout<<*it<<" ";cout<<endl;
return 0;
}
/*
3 7 4 5
9 3 7 4 5
9 3 7 8 4 5
3 7 8 4 5
3 7 8 5
this is iterator:
3
7
8
5
*/
//结构体与模板类的对比
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(const int a)
{
return a<10;
}
template <typename T>//模板
class mycmp
{
public:
mycmp(T v){value=v;}
bool operator ()(const T a)
{
return a<value;
}
private:
T value;
};
int main(int argc, char *argv[])
{
int a[]={1,3,4,5,6,7,8,9,11};
cout<<count_if(a,a+sizeof(a)/sizeof(a[0]),cmp)<<endl;//输出a中小于10的元素个数
cout<<count_if(a,a+sizeof(a)/sizeof(a[0]),mycmp<int>(10))<<endl;//输出a中小于10的元素个数
cout<<count_if(a,a+sizeof(a)/sizeof(a[0]),mycmp<int>(8))<<endl;//输出a中小于8的元素个数
return 0;
}
/*
8
8
6
*/
最后
以上就是粗心老师为你收集整理的指针_链表_结构体_类 2021-02-17的全部内容,希望文章能够帮你解决指针_链表_结构体_类 2021-02-17所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复