数据结构之链表基本操作(C++)


#include<iostream>
using namespace std;


struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(NULL){}
};
/***************************************************************************************************/
//单链表的建立
ListNode *creat()
{
    ListNode *head=new ListNode(0);
    ListNode *p=head;
    ListNode *node=NULL;
    int x=0;
    int cycle=1;
    while(cycle)
    {
        cout<<"please input the data:"<<endl;
        cin>>x;
        if(x!=0)       //当数据为0时,表示插入结束
        {
            node=new ListNode(x);
            p->next=node;
            p=node;
        }
        else
            cycle=0;
    }
    p->next=NULL;  //尾节点指针置为空
    //head=head->next;
    return head;
}
/***************************************************************************************************/
//单链表的打印
void printList(ListNode *head)  //note:这里head为单链表的头节点,而非第一个节点
{
    if(head->next==NULL)
    {
        cout<<"list is empty!"<<endl;
        return ;
    }
    ListNode *p=head->next;
    int index=0;
    while(p!=NULL)
    {
        cout<<"第"<<++index<<"个元素为:"<<p->val<<endl;
        p=p->next;
    }
}
/****************************************************************************************************/
//单链表的测长
int getListLength(ListNode *head)  //note:这里head为单链表的头节点,而非第一个节点
{
    int len=0;
    ListNode *p=head->next;
    while(p!=NULL)
    {
        ++len;
        p=p->next;
        
    }
    return len;
}

/****************************************************************************************************/
//单链表的插入
//将值为data的新节点插入到链表的第i(i=pos)个节点上
//单链表插入算法的时间耗费主要在查找第i-1个节点上;故单链表插入操作的复杂度为O(n);

//注;在链表中第i(i从0开始取值)个位置插入,分三种情况:插入到链表首部、中间、尾部
ListNode *insertList(ListNode *head,int pos,int data)//note:这里head为单链表的头节点,而非第一个节点
{
     ListNode *newNode=new ListNode(data);
     
     ListNode *p=head;
     int index=1;
     while(p!=NULL&&index<pos)
     {
         p=p->next;
         ++index;
     }
     newNode->next=p->next;
     p->next=newNode;
     return head;
}
/**************************************************************************************************/
//单链表的删除:将单链表中第i个节点删除
//删除单链表的头元素、中间元素、尾元素
//注:单链表的长度为n,则单链表删除第i个节点时,必须保证1=<i<=n,否则不合法。而当i=n+1
//时。虽然被删节点不存在,但其前驱节点存在,它是终端节点。因此被删节点的直接前驱存在并不
//意味着被删节点就一定存在,仅当p存在,且p不是终端节点同时满足index<i时,才能确定被删节点存在;
//算法的时间复杂度是O(n);

ListNode *deleteNode(ListNode *head,int pos)//pos从1开始,1表示删除链表的头元素
    
{
        ListNode *p=head;
        if(p->next==NULL)  //链表为空时
        {
             cout<<"链表为空"<<endl;
            return NULL;
        }
        ListNode *node=NULL;
        int index=1;
        while(p!=NULL&&index<pos)
        {
            p=p->next;
            ++index;
        }
        if(p!=NULL&&p->next!=NULL)  
        {
        node=p->next;
        p->next=node->next;
        delete node;
        }
        return head;
}

/***************************************************************************************************/
int main()
{
    ListNode *head=new ListNode(0);
    head=creat();
    cout<<"输入的链表为:"<<endl;
    printList(head);
    int len=getListLength(head);
    cout<<"单链表的长度为:"<<len<<endl;
    
    int pos=1;
    int data=50;
    insertList(head,pos,data);
    cout<<"插入后的链表为:"<<endl;
    printList(head);
    
    
    int i=5;
    deleteNode(head, i);
    cout<<"删除后的链表为:"<<endl;
    printList(head);
    
    system("pause");
    return 0;


}

猜你喜欢

转载自blog.csdn.net/muhehhh/article/details/81265777