zoukankan      html  css  js  c++  java
  • 数据结构自己实现——Linklist

    //单???链???表???
    #include <iostream>
    using namespace std;
    
    typedef char datatype;
    typedef struct node{
                    datatype data;
                     struct node* next;
    }listnode;
    typedef listnode* linklist;
    listnode *p;
    
    //建??立???链???表???
    linklist createlist()
    {
                    linklist head = (listnode*)malloc( sizeof(listnode));
                    listnode *p,*r;
                    r = head;
                     char ch;
                     while((ch=getchar())!='
    ' )
                    {
                                    p = (listnode*)malloc( sizeof(listnode));
                                     if(p==NULL)
                                                     return NULL;
                                    p->data = ch;
                                    r->next = p;
                                    r = p;
                    }
                    r->next = NULL;
                     return(head);
    }
    
    //查??找??单???链???表???
    linklist getnode(linklist head , int i)
    {
                     int j;
                    listnode* p;
                    p = head;
                    j = 0;
                     while(p->next && j<i)
                    {
                                    p = p->next;
                                    j++;
                    }
                     if(i==j)
                                     return p;
                     else
                                     return NULL;
    }
    
    //打???印??输??出?链???表???内??容?Y
    void printlist(linklist head)
    {
                    listnode* p = head;
                     while(p->next)
                    {
                                    cout<<p->next->data<<endl;
                                    p = p->next;
                    }
    }
    //插?入??
    void insertlist(linklist head,datatype m,int i)
    {
                    listnode* p = getnode(head,i);
                    listnode* s = (listnode*)malloc( sizeof(listnode));
                    s->data = m;
                     if(p!=NULL)
                    {
                                    s->next = p->next;
                                    p->next = s;
                    }
    }
    //删??除y一??个?结??点??,删??除y第???i个?后??面?的??那?个?
    void deletelistnode(linklist head,int i)
    {
                     int j = 0;
                    listnode *p,*r;
                    p = head;
                     while(p&&j<i)
                    {
                                    p = p->next;
                                    j++;
                    }
                     if(p==NULL)
                                    exit(1);
                    
                    r = p->next;
                    p->next = r->next;
                    free(r);
                    
    }
    int main()
    {
                    linklist mylist = createlist();
     //   listnode * tmp = getnode(mylist,3);
                     //cout<<tmp->data<<endl;
                     //printlist(mylist);
                     //insertlist(mylist,'a',2);
                     //printlist(mylist);
                    deletelistnode(mylist,2);
                    printlist(mylist);
                     return 0;
    }
  • 相关阅读:
    C# Path 目录
    Maxscript 窗体与结构体this的传递
    python---文件操作
    python---数据类型---集合
    python---购物车---更新
    python---三级菜单
    python---数据类型---字典
    python---数据类型---字符串
    python---购物车
    python---数据类型---列表
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3435842.html
Copyright © 2011-2022 走看看