链表的基本操作

链表的一些基本操作(想到了就写,慢慢更新)

#include <iostream>
#include<math.h>
#include<typeinfo>
using namespace std;

/*
单链表反转
链表中环的检测
两个有序链表的合并
删除链表倒数第n个节点
求链表的中间结点
*/
typedef struct node
{
    int data;
    struct node* next;
}ListNode;

//链表初始化
ListNode* ListInit()
{
    ListNode* head;
    head = (ListNode*)malloc(sizeof(ListNode));
    head->next=NULL;
    return head;
}

//链表插入
void ListInsert(ListNode* head,int n )
{
    ListNode* node = (ListNode*)malloc(sizeof(ListNode));
    node->data = n;
    node->next=head->next;
    head->next=node;
}
//链表打印
void ListPrint(ListNode* head)
{
    head = head->next;
    while(head!=NULL)
    {
        cout<<head->data<<endl;
        head=head->next;
    }
}
//单链表反转
void ListInverse(ListNode* head)
{
    ListNode* p = head->next;
    ListNode* q ;
    head->next=NULL;
    while(q!=NULL)
    {
        q=p->next;
        p->next = head->next;
        head->next=p;
        p=q;
    }
}
//创建链表环
void ListCreateLoop(ListNode*head,int arry[],int arrySize)
{
    ListNode* node=(ListNode*)malloc(sizeof(ListNode));
    node->data = arry[0];
    node->next = head;
    head->next = node;
    for(int i = 1;i<arrySize;++i)
    {
        ListNode* node=(ListNode*)malloc(sizeof(ListNode));
        node->data = arry[i];
        node->next = head->next;
        head->next = node;
    }
}
//检测链表环
bool ListInspectLoop(ListNode* head)
{
    ListNode* p,*q;
    p=head->next;
    q=p->next;
    while(q!=NULL)
    {
        if(q->next==p||q==p)
            return true;
        if(q->next==NULL)
            return false;
        q=q->next->next;
        p=p->next;
    }
    return false;

}
//两个有序链表合并
ListNode* ListMerge(ListNode* head1,ListNode* head2)
{
    ListNode* head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode *p1,*p2;
    p1=head1->next;
    p2=head2->next;
    while(p1!=NULL && p2!=NULL)
    {
        if(p1->data>p2->data)
        {
            ListNode* node = (ListNode*)malloc(sizeof(ListNode));
            node->data = p1->data;
            node->next = head->next;
            head->next = node;
            p1=p1->next;
        }
        else
        {
            ListNode* node = (ListNode*)malloc(sizeof(ListNode));
            node->data = p2->data;
            node->next = head->next;
            head->next = node;
            p2=p2->next;
        }
    }
    while(p1!=NULL)
    {
        ListNode* node = (ListNode*)malloc(sizeof(ListNode));
        node->data = p1->data;
        node->next = head->next;
        head->next = node;
        p1=p1->next;
    }
    while(p2!=NULL)
    {
        ListNode* node = (ListNode*)malloc(sizeof(ListNode));
        node->data = p2->data;
        node->next = head->next;
        head->next = node;
        p2=p2->next;
    }
    return head;
}
//删除链表倒数第n个节点
//修改了这里,添加了只有一个结点的链表删除倒数第一个节点(删除自己)的情况
ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(!head | !head -> next) 
		{
			free(head);
			return NULL;
		}
        ListNode * fast = head, *slow = head;
        for(int i = 0; i < n; i++){
            fast = fast -> next;
        }
        if(!fast){
			ListNode* pre = head;
            head =  head -> next;  
			free(pre);
			return head;			
        }
        
        while(fast -> next){
            fast = fast -> next;
            slow = slow -> next;
        }
		free(slow->next);
        slow -> next = slow -> next -> next;
        return head;
    }

//求链表的中间结点
ListNode* ListMiddleNode(ListNode*head)
{
    ListNode *middle,*end;
    middle = end = head->next;
    while(end!=NULL)
    {
        if(end->next!=NULL)
        {
            end = end->next->next;
            middle = middle->next;
        }
        else
            return middle;
    }
    return middle;
}
int main()
{
    //    ListNode* head = ListInit();
    //    for(int i =0;i<7;++i)
    //    {
    //        ListInsert(head,i);
    //    }
    //    ListPrint(head);
    //    ListInverse(head);
    //    cout<<"+++++++++"<<endl;
    //    ListPrint(head);


    //    int arry[8]={1,2,3,4,5,6,7,8};
    //    ListCreateLoop(head,arry,8);
    //    if(ListInspectLoop(head))
    //        cout<<"true"<<endl;
    //    else
    //        cout<<"false"<<endl;


    //    ListNode* head1 = ListInit();
    //    ListNode* head2 = ListInit();

    //    ListInsert(head1,1);
    //    ListInsert(head1,2);

    //    ListInsert(head2,3);

    //    ListInsert(head1,4);

    //    ListInsert(head2,5);
    //    ListInsert(head2,6);

    //    ListNode* head = ListMerge(head1,head2);
    //    ListPrint(head);

    //        ListNode* head = ListInit();
    //        for(int i =7;i>0;--i)
    //        {
    //            ListInsert(head,i);
    //        }
    //        ListPrint(head);
    //        ListDeleteNode(head,1);
    //        cout<<"+++++++++"<<endl;
    //        ListPrint(head);

    ListNode* head = ListInit();
    for(int i =5;i>0;--i)
    {
        ListInsert(head,i);
    }
    ListPrint(head);
    cout<<"--------"<<endl;
    ListNode* middle = ListMiddleNode(head);
    cout<<middle->data<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值