c++链表的基本入门操作和练习

单链表的基本操作

c++提供了list容器,能够完成插入,删除,反转等操作,为用户使用提供了便利。
具体的使用教程可参考
关于c++list容器的使用

基本操作
个人练习链表的基本操作
1.链表的创建
2.链表的遍历
3.链表元素的查找删除
4.链表的插入
5.反转链表
6.链表的长度

 #include<iostream>
 using namespace std;
 //***定义节点****
struct  node {
  int data;
     struct node* next;
 };
    
  //***链表的创建*** 
 node* create_list(const int& len){
    if(len<=0) {
     node* head = NULL;
  cout<<"输入链表大小不合法\n"; 
  return head;
    }
    else{
    
  node * head = NULL, *tail = NULL ,*pnew = NULL; 
  
  int val;//元素的值   
  head = new node; //链表的首地址 
  tail = head ;
      
   for(int i=0;i<len;++i){
    cout<<"请输入第"<<i+1<<"个元素的值:";
    cin>>val;
       pnew = new node;
    pnew->data = val ;
    tail->next = pnew;//从尾节点处开始赋值 
    pnew->next = NULL;
    tail = pnew; //最后使尾指针为NULL 
   }
    
 return head ;  
 }
   }
    

  //***从头节点后节点插入数据*** 
 void add_front(node* phead,const int& num){
  if(phead==NULL)cerr<<"链表不存在\n";
  else{
   node* pnew = new node;
     pnew->data = num;
     pnew->next = phead->next;
   phead->next = pnew;
   return ;
           }
 } 
 

 //***从尾节点后插入数据***
 void add_back(node*phead,const int& num){
  if(phead==NULL)cerr<<"链表不存在\n";
  else{
   node* pnew = new node;
   pnew->data = num;
   while(phead->next){
    phead=phead->next;
   }
   pnew->next = NULL;
   phead->next= pnew;
   return;
  }
  
 } 
 

 //****删除某个节点**** 
 void delete_ele(node* phead ,const int& num)
 {
  node *cur=NULL,*temp=NULL;
  while(phead){
   cur = phead;
   temp = phead->next; //带头节点
   if(!temp) 
   {
    cout<<"不存在该元素\n";
    return ;
   }
   if(temp->data == num)
   {
    cur->next = temp->next;
    delete temp;
    return;
   }
   phead = phead->next;
  }
 }
 
    
    
 //**打印** 
 void  print(const node* phead) 
 {  if(phead==NULL||phead->next==NULL)
  {  cout<<"链表为空或不存在\n"; 
   return;
   } 
    else{
     node * p ;
     p = phead->next;
     while(p!=NULL){
   cout<<p->data<<" ";
   p=p->next;
  }
  
  return; 
    }
 }
 
 
 //***销毁链表*** 
    void delete_list(node * phead)
 { 
  if(phead==NULL) return;
 
    else{
   node* cur=NULL;
    while(phead)
  {    cur = phead;  //当前的节点 
       phead = phead ->next;
       delete cur;
  } 
  //***释放尾节点*** 
     delete phead;
    if(!phead) cout<<"\n链表已释放\n";
    return ;
     }
  } 
  
  
  //***链表的大小***
  int list_size(node*phead){
   if(phead==NULL)return 0;
   else{
    node* cur = NULL;
    cur = phead->next;
    int count = 0;
   while(cur){
    count++;
    cur = cur->next;
   }
   return count;
   }
  } 
  
  
  //***从特定的位置pos前插入*** 
  //note:此处的计算pos方式和数组一致,即从0开始计数 
  void insert_ele(node * phead,const int& pos,const int& num)  
  { 
    // 1.判断下标的合法性 
    //小于0,不合法
    //大于链表长度则表示在末尾节点后插入 
    //***位置小于0*** 
    int posnew = pos;
   if(pos<0)cerr<<"下标不存在\n";
   //***位置大于0*** 
   else if(pos>list_size(phead))
   {
    add_back(phead,num); 
    return;
    } 
    //***位置在0~size间
    else
    { 
      //***第0个位置**** 
      if(pos==0) {add_front(phead,num);return;}
      else{
       node* pnew = new node;
       pnew->data = num;
       //***定位*** 
        while(posnew-->0){
             phead = phead->next;
     }
    pnew->next = phead->next;
    phead->next = pnew;
    return;
   }
  } 
  } 
  
  
  //***反转链表***
  void reverse_list(node*phead) 
  {   if(phead==NULL) cerr<<"链表不存在\n";
      else{
       //***newhead表示新的首节点*** 
   //***pnew用于改变指向和连接节点***
   //***temp记录pheap的初始值*** 
    node* newhead = NULL,*pnew = NULL,*temp=phead;
    phead = phead->next; //首节点 
   while(phead){
    pnew = phead;
      phead = phead->next;
    //***设置尾节点标志和改变指向**** 
    pnew->next = newhead;
    newhead = pnew;
     
   }
   //***改变0号元素的地址*** 
   temp->next = newhead;
   //***改变头节点*** 
   phead = temp; 
  }
   }
   
   
   //****修该pos处的值**** 
   //采用数组的下标计数方式,即从0开始计算 
    void change_ele(node*phead ,const int& pos,const int&num) {
     if(pos<0||pos>=list_size(phead)){
      cerr<<"输入下标不合法\n";
  }
  else if(pos==0){
   phead = phead->next;
   phead->data = num;
   return;
  }
  else{ int posnew = pos;
   phead = phead->next;
       while(posnew-->0){
       phead = phead->next;
         }
       phead->data = num;
    return;  
  }
 }

 //**判断链表是否为空** 
 bool list_empty(node*phead){
  if(phead==NULL||phead->next==NULL) return true;
  else return false;
 } 


 int main(){
  node* n=NULL;
  //n = init_list();
  int len = 5;
  n = create_list(len);
     //add_back(n,6);
     //insert_ele(n,5,90); 
     change_ele(n,4,100);
     //reverse_list(n);
  print(n);
  cout<<"\n链表长度:"<<list_size(n)<<endl;
  delete_list(n);
  cout<<list_empty(n)<<endl;
  return 0;
 }

个人练习记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值