单链表基本操作

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>

using namespace std;

typedef struct Node
{
    int m_idata;
    struct Node * m_pnext;
}Node,*pLinkList;

pLinkList CreateFromHead()
{
    pLinkList L;
    L = (pLinkList)malloc(sizeof(Node));
    L->m_pnext = NULL;

    bool bflag = true;
    while(bflag)
    {
        cout <<"Please input the number(int),end of 0: "<<endl;
        int _in;
        cin >>_in;
        if( 0 == _in)
        {
            bflag = false;
            break;
        }

        Node *pstNode = (Node *)malloc(sizeof(Node));
        pstNode->m_pnext = L->m_pnext;
        pstNode->m_idata = _in;
        L->m_pnext = pstNode;
    }
    return L;
}

void PrintList(pLinkList L)
{
    if(NULL == L)
    {
        return ;
    }
    Node *p = L->m_pnext;
    while(NULL != p)
    {
        cout <<p->m_idata<<"    ";
        p = p->m_pnext;
    }
    cout<<endl<<endl;
    return ;
}

pLinkList CreateFromTail()
{
    pLinkList L = (pLinkList)malloc(sizeof(Node));
    L->m_pnext = NULL;
    Node *r = L;
    bool bcontinue = true;

    while(bcontinue)
    {
        cout <<"Please input the number(int),end of 0: "<<endl;
        int _in;
        cin >>_in;
        if( 0 == _in)
        {
            bcontinue = false;
            break;
        }
        Node *s = (Node *)malloc(sizeof(Node));
        s->m_idata = _in;
        s->m_pnext = NULL;
        r->m_pnext = s;
        r = r->m_pnext;
    }

    return L;
}

pLinkList InsNode(pLinkList L)
{
    if(NULL == L)
    {
        cout <<"The Link is empty,create it first."<<endl;
        return NULL;
    }

    int ipos;
    int idata;
    cout <<"Please input the poszition :";
    cin >>ipos;
    cout <<"Please input the data:";
    cin >>idata;

    Node *pins = (Node*)malloc(sizeof(Node));
    pins->m_pnext = NULL;
    pins->m_idata = idata;

    Node *p = L;
    int i = 0;
    while(i++ < ipos-1 && NULL != p)
    {
        p = p->m_pnext;
    }
    if(NULL == p || i != ipos)
    {
        cout<<"the input poszition is error"<<endl;
        return L;
    }

    pins->m_pnext = p->m_pnext;
    p->m_pnext = pins;

    return L;
}


pLinkList DelNode(pLinkList L)
{
    if(NULL == L)
    {
        return NULL;
    }

    int ipos;
    cout <<"Please input the poszition :";
    cin >>ipos;

    int i = 0;
    Node *p = L;
    while(i++ <  ipos - 1 && NULL != p->m_pnext)
    {
        p = p->m_pnext;
    }

    if(NULL == p->m_pnext || i != ipos )
    {
        cout<<"the input poszition is error"<<endl;
        return L;
    }

    Node *pDel = p->m_pnext;
    p->m_pnext = pDel->m_pnext;
    free(pDel);
    return L;
}


int main()
{
    pLinkList L = NULL;
    bool bContinue = true;
    while(bContinue)
    {
         cout <<endl<<"************************************************************************"<<endl;
         cout <<"Please input 1:CreateFromHead    2:CreateFromTail    3:Print  "<<endl;
         cout <<"             4:InsNode           5:DelNode           6:Sort      0:Exit";
         cout <<endl<<"************************************************************************"<<endl;
        int _inSelect;
        cin>>_inSelect;

        switch(_inSelect)
        {
            case 0:
                bContinue = false;
                break;
            case 1:
                L = CreateFromHead();
                break;
            case 2:
                L = CreateFromTail();
                break;
            case 3:
                PrintList(L);
                break;
            case 4:
                InsNode(L);
                break;
            case 5:
                DelNode(L);
                break;
            case 6:
                break;
            default:
                break;
        }

    }



    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szwm1010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值