从今天开始学习数据结构(c++/c)---链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lulujiang1996/article/details/82385512

先实现这么多功能,后续再填代码(本人一菜渣,c式闹着玩编程如下):

#include <iostream>
using namespace std;
struct Node{
    int value;
    Node *next;
};
//尾插法
void insertNode(Node *p,int i){
    Node *q=p;
    while(q->next!=NULL)
    {
        q=q->next;
    }
    Node *node=new Node;
    node->value=i;
    q->next=node;
    node->next=NULL;

}
// 删除某值
void deleteNode(Node *p,int key)
{
    Node *pre=p;
    Node *q=pre->next;
    while(q!=NULL)
    {
        if(q->value==key)
        {
            pre->next=q->next;
            q=q->next;
        }else{
        pre=pre->next;
        q=pre->next;    
       }
    }
}
//更新某值
void updateKey(Node *p,int key,int newkey)
{
    Node *q=p;
    while(q!=NULL)
    {
        if(q->value==key)
        {
            q->value=newkey;
        }
        q=q->next;
    }
}
//查找某值,将下标位置存储到a数组中
int SelectKey(Node *p,int key,int a[]){
    Node *q=p;
    int i=0,count=0;
    while(q!=NULL)
    {
        if(q->value==key)
           a[i++]=count;

        q=q->next;
        count+=1;
    }
    return i;

}
//实现链表翻转
void reverseList(Node *p)
{
    Node *q=p->next,*m;

    Node *temp=q->next;
    q->next=NULL;
    while(temp!=NULL)
    {

        m=temp->next;
        temp->next=q;
        q=temp;
        temp=m;

    }
    p->next=q;


}
//从后查找某位置的值
int findNodeFromBack(Node *p,int i)
{
    Node *q=p,*w=p;

    int count=0,key,k=0;
    while(q->next!=NULL)
    {
        q=q->next;
        count+=1;
    }
    while(w->next!=NULL)
    {
        if((count-i)==k)
        {
          return w->value;
        }
        w=w->next;
        k+=1;
    }

}
//创建环
void CreateLoop(Node *p)
{
    Node *q=p;
    while(q->next!=NULL)
        q=q->next;
    q->next=p;
}

//判断是否有环
void containLoop(Node *p)
{
    Node *q=p->next;
    while(q!=p)
    {
        q=q->next;
        if(q==NULL)
        {
            cout<<"无环"<<endl;
            return;
        }

    }
    cout<<"有环"<<endl;

}
//实现链表输出
void showNode(Node *p)
{
    while(p!=NULL)
    {
        cout<<p->value;
        p=p->next;
    }
}
int main()
{
    Node *h=new Node;
    int *a=new int[80];
    h->next=NULL;
    h->value=-1;
    insertNode(h,0);
    insertNode(h,1);
    insertNode(h,2);
    insertNode(h,3);
    showNode(h);
    cout<<endl;
    //deleteNode(h,1);
    //showNode(h);

    /*updateKey(h,1,0);
    showNode(h);
    cout<<endl;
    int index=SelectKey(h,0,a);
    cout<<index<<endl;
    cout<<a<<endl;
    for(int i=0;i<index;i++)
    {
        cout<<a[i];
    }
    cout<<endl;*/
    reverseList(h);
    showNode(h);
    cout<<endl;
    containLoop(h);
    //CreateLoop(h);
    //containLoop(h);
    cout<<findNodeFromBack(h,0);

    return 0;
}

STL实现链表

#include <iostream>
using namespace std;
#include<list>
#include<algorithm>
void Print(int &item)
{
    cout<<item<<" ";
}
int main()
{
   list<int> listintegers;
   //头插法
   listintegers.push_front(5);
   listintegers.push_front(3);
   listintegers.push_front(1);
   for(list<int>::iterator it=listintegers.begin();it!=listintegers.end();it++)
   {
       cout<<"-"<<*it;
   }
   //尾插法
    cout<<endl;
    listintegers.push_back(6);
    listintegers.push_back(7);
    std::for_each(listintegers.begin(),listintegers.end(),Print);
    cout<<endl;
   //删除某元素
    listintegers.remove(3);
    std::for_each(listintegers.begin(),listintegers.end(),Print);
    cout<<endl;
   //删除尾元素
    listintegers.pop_back();
    std::for_each(listintegers.begin(),listintegers.end(),Print);
   cout<<endl;
    //删除首元素
    listintegers.pop_front();
    std::for_each(listintegers.begin(),listintegers.end(),Print);
    cout<<endl;
    //查找某元素
    list<int>::iterator listinter;
    int posi=0;
    listinter=find(listintegers.begin(),listintegers.end(),5);
    for( list<int>::iterator iter=listintegers.begin();iter!=listinter;iter++)
    {
        posi+=1;

    }
    cout<<posi<<endl;
    if(listinter==listintegers.end())
        cout<<"NO"<<endl;
    else
    {
        cout<<"YES"<<endl;
    }

    //修改
    for( list<int>::iterator iter=listintegers.begin();iter!=listintegers.end();iter++)
    {
        if(*iter==6)
            *iter=7;

    }
    std::for_each(listintegers.begin(),listintegers.end(),Print);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/lulujiang1996/article/details/82385512