zoukankan      html  css  js  c++  java
  • 链表

    #include <iostream>
    using namespace std;

    struct MyList
    {
        int data;
        MyList*next;
    };

    void Init(MyList&head)
    {
        head.data=0;
        head.next=NULL;
    }
    void ListInsert(MyList&head,int i,int data)
    {
        int j=1;
        MyList*p,*s;
        p=&head;
        while(p&&j<i)
        {
            p=p->next;
            j++;
        }
        if(!p||j>i)
        {
            return;
        }
        s=new MyList();
        s->data=data;
        s->next=p->next;
        p->next=s;
    }

    void update(MyList&head,int i,int data)
    {
        int j=1;
        MyList*p=&head;
        while(p)
        {
            p=p->next;
            if(j==i)
            {
                break;
            }
            j++;
        }
        if(!p||j>i)
        {
            return;
        }
        p->data=data;
    }

    void del(MyList&head,int i)
    {
        int j=1;
        MyList*p=&head;
        MyList*s;
        while(p&&j<i)
        {
            p=p->next;
            j++;
        }
        if(!p||j>i)
        {
            return;
        }
        s=p->next;
        p->next=s->next;
        delete s;
    }

    int query(MyList&head,int i,int *e)
    {
        int j=1;
        MyList*p=&head;
        while(p)
        {
            p=p->next;
            if(j==i)
            {
                break;
            }
            j++;
        }
        if(!p||j>i)
        {
            return -1;
        }
        *e=p->data;
        return 0;
    }

    void show(MyList head)
    {
        MyList* p=head.next;
        while(p!=NULL)
        {
            cout<<p->data<<endl;
            p=p->next;
        }
    }

    int main()
    {
        MyList head;
        Init(head);
        for(int i=0;i<10;i++)
        {
            ListInsert(head,i+1,20+i);
        }
        cout<<"update"<<endl;
        update(head,5,200);
        show(head);
        del(head,1);
        cout<<"delete"<<endl;
        show(head);
        cout<<"query"<<endl;
        int result=0;
        query(head,2,&result);
        cout<<result<<endl;
        cin.get();
        
    }

  • 相关阅读:
    调用外部 DLL 中的函数(显示调用)
    模式窗体与非模式窗体
    使用PChar和string类型时的内存分配技术
    保密卡程序的编写
    Dll 使用 PChar 参数的小例子
    delphi动态创建组件的颜色
    Dll 模式窗口与非模式窗口
    调用外部 DLL 中的函数(隐式调用)
    内核读写只读内存方法总结[Delphi描述][转帖]
    delphi资源文件制作及使用详解
  • 原文地址:https://www.cnblogs.com/cppfans140812/p/5816257.html
Copyright © 2011-2022 走看看