单链表/双向链表/单链表反转/双向循环链表

单链表

#include <bits/stdc++.h>
using namespace std;

struct Node
{
    int val;
    Node* next;
};
void Create(Node* head)
{
    int n;
    cin >> n;
    Node *head1 = head;
    for(int i = 0 ; i < n ; i++)
    {
        int x;
        cin >> x;
        if(i==0)
        {
            head1->val = x;
        }
        else
        {
            Node *temp = new Node();
            temp->val = x;
            head1->next = temp;
            head1 = head1->next;
        }
    }
}
void show(Node *head)
{
    while(head!=NULL)
    {
        cout << head->val << endl;
        if(head->next != NULL) head = head->next;
        else break;
    }
}
int main()
{
    Node* head = new Node();
    Create(head);
    show(head);
}

双向链表:

#include <bits/stdc++.h>
using namespace std;

struct Node
{
    int val;
    Node* next;
    Node* pre;
};
int n;
Node* Create(Node* head)
{
    cin >> n;
    Node *head1 = head;
    for(int i = 0 ; i < n ; i++)
    {
        int x;
        cin >> x;
        if(i==0)
        {
            head1->val = x;
        }
        else
        {
            Node *temp = new Node();
            temp->val = x;
            temp->pre = head1;
            head1->next = temp;
            head1 = head1->next;
        }
    }
    return head1;
}
void show(Node *last)
{
    if(!n) return;
    while(last!=NULL)
    {
        cout << last->val << endl;
        if(last->pre != NULL) last = last->pre;
        else break;
    }
}
int main()
{
    Node* head = new Node();
    Node* last = new Node();
    last = Create(head);
    show(last);
}

单链表就地反转:

#include <bits/stdc++.h>
using namespace std;

struct Node
{
    int val;
    Node* next;
};
int n;
Node * Create(Node* head)
{
    cin >> n;
    Node *head1 = head;
    for(int i = 0 ; i < n ; i++)
    {
        int x;
        cin >> x;
        if(i==0)
        {
            head1->val = x;
        }
        else
        {
            Node *temp = new Node();
            temp->val = x;
            head1->next = temp;
            head1 = head1->next;
        }
    } 
    return head;
}
void show(Node *head)
{
    if(!n) return ;
    while(head != NULL)
    {
        cout << head->val << endl;
        head = head -> next;
    }
}
Node* Reverse(Node *head)
{
    Node *p = head; // 上一个点
    Node *q = head->next; // 正在处理的点
    Node *r = head;//下一个点
    head->next = NULL;//反转后尾指针的next为空
    while(q != NULL)
    {
        r = q->next;
        q->next = p;
        p = q;
        q = r;
    }
    return p;
}

int main()
{
    Node* head = new Node();
    Node* last = new Node();
    last = Create(head);
    show(Reverse(last));
}

双向循环链表:

#include <bits/stdc++.h>
using namespace std;

struct Node
{
    int val;
    Node* next;
    Node* pre;
};
int n;
Node* Create(Node* head)
{
    cin >> n;
    Node *head2 = head;
    Node *head1 = head;
    for(int i = 0 ; i < n ; i++)
    {
        int x;
        cin >> x;
        if(i==0)
        {
            head1->val = x;
        }
        else
        {
            Node *temp = new Node();
            temp->val = x;
            temp->pre = head1;
            head1->next = temp;
            head1 = head1->next;
            if(i==n-1) // 首位相接
            {
                head2->pre = head1;
                head1->next = head2;
            }
        }

    }

    return head1;
}
void show(Node *last)
{
    if(!n) return;
    while(last!=NULL)
    {
        cout << last->val << endl;
        if(last->pre != NULL) last = last->pre;
        else break;
    }
}
int main()
{
    Node* head = new Node();
    Node* last = new Node();
    last = Create(head);
    show(last);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值