单链表的建立,插入,显示,查找删除以及反转

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stack>    //二叉树遍历时使用栈
#include <queue>    //二叉树层次遍历时使用
using namespace std;

//单链表操作
class Node
{
public:
    Node *next;
    Node *front;    //双向链表时指向前驱节点的指针
    int data;
};

Node *head=NULL;    //头指针 全局变量
Node *rear=NULL;    //队列的尾指针

void create_list()
{
    Node *p1,*p2;
    int index=0;
    int data;
    while(1)
    {
        cout<<"请输入链表的第"<<++index<<"个数据(输入负数表示结束链表创建):";
        cin>>data;
        if (data<0&&index==1)    //空链表时输入负数
        {
            return;
        }
        else if (data<0)        //非空链表时输入负数
        {
            break;
        }
        p1=new Node;
        p1->data=data;
        if (index==1)            //空链表时输入有效数据
        {
            head=p1;
        }
        else
            p2->next=p1;
        p2=p1;
    }
    p2->next=NULL;
}

void show_list(Node *head)
{
    int index=0;
    Node *p=head;
    if (head==NULL)
    {
        cout<<"链表为空!"<<endl;
        return;
    }
    while(p)
    {
        cout<<"链表的第"<<++index<<"个数据为:"<<p->data<<endl;
        p=p->next;
    }
}

int length_list(Node *head)
{
    Node *p=head;
    int length=0;
    if (head==NULL)
    {
        cout<<"链表为空!"<<endl;
        return length;
    }
    while(p)
    {
        length++;
        p=p->next;
    }
    return length;
}

//按照位置查找数据
void find_data_list(Node *head,int pos)    //pos表示要查找的位置  从1开始算
{
    Node *p=head;
    if (head==NULL)
    {
        cout<<"链表为空!"<<endl;
        return;
    }
    if (pos<1||pos>length_list(head))
    {
        cout<<"pos位置超出链表长度或者小于1!"<<endl;
        return;
    }
    cout<<"链表第"<<pos<<"个位置的值为:";
    while(--pos)
    {
        p=p->next;
    }
    cout<<p->data<<endl;
}

//按照数据查找位置
void find_pos_list(Node *head,int data)
{
    Node *p=head;
    int pos=0;        //记录数据在链表中的位置
    if (head==NULL)
    {
        cout<<"链表为空!"<<endl;
        return;
    }
    while(p)
    {
        pos++;
        if (p->data==data)
        {
            cout<<data<<"在链表的第"<<pos<<"个位置"<<endl;
            return;
        }
        p=p->next;
    }
    cout<<data<<"不在链表中"<<endl;
}

//删除指定位置的数据
void delete_pos_list(Node *head,int pos)
{
    Node *p=head;
    Node *temp;
    if (head==NULL)
    {
        cout<<"链表为空!"<<endl;
        return ;
    }
    if (pos<1||pos>length_list(head))
    {
        cout<<"pos位置超出链表长度或者小于1!"<<endl;
        return ;
    }
    
    if (pos==1)        //删除头结点
    {
        ::head=head->next;    //更新全局头指针
        delete p;
        p=NULL;
    }
    else
    {
        pos=pos-1;    //保证while循环找到的是pos结点的上一个结点
        while(--pos)    //循环结束时p指向pos位置的上一个位置
        {
            p=p->next;
        }
        temp=p->next;
        p->next=p->next->next;
        //p=temp->next;
        delete temp;
        temp=NULL;
    }
}

//在pos位置插入一个元素  插入在pos-1位置后面
void insert_pos_list(Node *head,int pos,int data)
{
    Node *temp;
    Node *p=head;
    if (head==NULL)
    {
        cout<<"链表为空!"<<endl;
        return ;
    }
    if (pos<1||pos>length_list(head))
    {
        cout<<"pos位置超出链表长度或者小于1!"<<endl;
        return ;
    }
    if (data<0)
    {
        cout<<"插入的数据小于0!"<<endl;
        return ;
    }
    if (pos==1)    //插入的头结点
    {
        temp=new Node;
        temp->data=data;
        temp->next=head;
        ::head=temp;
    }
    else
    {
        pos=pos-1;
        while(--pos)    //while循环结束时p指针指向pos-1个位置
        {
            p=p->next;
        }
        temp=new Node;
        temp->data=data;
        temp->next=p->next;
        p->next=temp;
    }
}

void reverse_list(Node *head)        //翻转单链表
{
    Node *p1,*p2,*p3;                //借助三个指针完成链表节点的依次反转  指向反转
    if (head==NULL||head->next==NULL)    //头结点为空 或者 只有头结点而没有其他节点  此时直接返回空
    {
        cout<<"链表为空或只有头结点而无其他数据节点!"<<endl;
        return ;
    }

    p1=head;                        //p1保存头结点地址
    p2=head->next;                    //
    head->next=NULL;        
    while(p2)
    {
        p3=p2->next;                //p3保存剩下的旧的链表
        p2->next=p1;
        p1=p2;                        //指针依次后移
        p2=p3;
    }
    ::head=p1;                        //更新全局head指针
}

//找出单链表的中间元素
Node *search_middle(Node *head)
{
    if (head==NULL)
    {
        cout<<"链表为空"<<endl;
        return NULL;
    }
    Node *p;
    p=head;
    int i=0,j=0;
    Node *mid;
    mid=head;
    while(p)
    {
        if (i/2>j)
        {
            j++;
            mid=mid->next;
        }
        i++;
        p=p->next;
    }
    return mid;
}

//创建环形链表(单向)
//参数n表示有n个人围成的的环
void create_sysle_list(int n)
{
    if (n<1)
    {
        cout<<"参数错误,人数不能小于1"<<endl;
        return;
    }
    Node *p1,*p2;
    int index=0;    //计数
    while(index++<n)
    {
        p1=new Node;
        p1->data=index;
        if (index==1)    //头结点
        {
            head=p1;
            p1=p1->next;
        }
        else
            p2=p1;
        p2=p1;
    }
}
单链表的反转,图解理解是最好的了,参考网址http://www.2cto.com/kf/201110/106607.html
原文地址:https://www.cnblogs.com/audi-car/p/4638874.html