• 线性单链表


    #include<iostream>
    using namespace std;
    struct student
    {
        long num;
        float score;
        struct student *next;
    };
    
    int n;
    //初始化一个链表
    void create(struct student **head)
    {
        struct student *p1,*p2;
        n = 0;
        p1 = p2 = new struct student;
        cin>>p1->num;
        cin>>p1->score;
        while (p1->num != 0 )
        {
            n = n+1;
            if (n == 1)
            {
                *head = p1;
            }
            else
            {
                p2->next = p1;
            }
            p2 = p1;
            p1 = new struct student;
            cin>>p1->num;
            if (p1->num == 0 )
            {
                break;
            }
            cin>>p1->score;
        }
        p2->next = NULL;
    }
    //找到第i个元素
    bool find(struct student **head, struct student **ptarget,int i)
    {
        struct student *p = *head;
        int n = 1;
        while (p && (n <i) )
        {
            p = p->next;
            n++;
        }
    
        if (p && (n == i))
        {
            *ptarget = p;
            return true;
        }
        else
            return false;
    }
    //在第i个节点后插入node
    bool insert(struct student **head,struct student *node ,int i)
    {
        struct student *pre;
        if (find(head,&pre,i))
        {
            node->next = pre->next;
            pre->next = node;
            return true;
        }
        return false;
    }
    //删除第i个节点
    bool del(struct student **head,int i)
    {
        struct student *pre,*target;
        if (find(head,&pre,i-1)&&find(head,&target,i))
        {
            pre->next = target->next;
            delete target;
            return true;
        }
        return false;
    }
    void print(struct student *head)
    {
        while (head)    
        {
            cout<<head->num<<"  "<<head->score<<endl;
            head = head->next;
        }
    }
    int main()
    {
        struct student *head ,*target=0;
        create(&head);
        print(head);
        
        find(&head,&target,2);
        if (target)
        {
            cout<<"find !  "<<target->num<<"  "<<target->score <<endl;
        }
        cout<<"--------insert--------------------------------------------"<<endl;
        struct student *node = new struct student;
        node->num = 20;
        node->score=100;
        insert(&head,node,2);
        
        print(head);
        cout<<"----------del------------------------------"<<endl;
        del(&head,2);
        print(head);
        cout<<""<<endl;
        return 0;
    }

    另:

     1 #include "iostream"
     2 using namespace std;
     3 
     4 typedef struct LinkList
     5 {
     6     int datainfo;
     7     LinkList *pnext;
     8 }LinkList;
     9 
    10 //动态创建n个节点
    11 void initial(LinkList **head,int n)
    12 {
    13     
    14     LinkList *p1,*p2;
    15     if (n<=0)
    16     {
    17         cout<<"please input a positive number !"<<endl;
    18         return;
    19     }
    20     *head = p1 =p2 = new LinkList;
    21     cout<<"input datainfo:  ";
    22     cin>>p2->datainfo;
    23     cout<<endl;
    24     for (int i = 0 ; i < n -1;i++)
    25     {
    26         p2 = new LinkList;
    27         cout<<"input datainfo:  ";
    28         cin>>p2->datainfo;
    29         cout<<endl;
    30         p1->pnext = p2;
    31         p1 = p2;
    32     }
    33     p2->pnext=0;//不设置会抛出异常
    34 }
    35 //查找第i个结点
    36 bool find(LinkList **head,LinkList **ptarget,int i)
    37 {
    38     LinkList *p = *head;
    39     int np = 1;
    40     while ( (p != NULL) && (np < i) )
    41     {
    42         p = p->pnext;
    43         np++;
    44     }
    45     
    46 //    if (np != i)
    47 //    {
    48 //        return false;
    49 //    }
    50     if (p == NULL)
    51     {
    52         return false;
    53     }
    54     
    55     if (np == i )
    56     {
    57         *ptarget = p ;
    58     }
    59     
    60     return true;
    61 }
    62 //在指定的节点后插入node
    63 bool insert(LinkList **head,LinkList *node,int i)
    64 {
    65     LinkList *p =NULL;
    66 
    67     if (find(head,&p,i))
    68     {
    69         
    70         node->pnext = p->pnext;
    71         p->pnext = node;    
    72         return true;
    73     }
    74     else
    75         return false;
    76 }
    77 
    78 void print(LinkList *head)
    79 {
    80     LinkList *p = head;
    81     while (p)
    82     {
    83         cout<<p->datainfo<<endl;
    84         p = p->pnext;
    85     }
    86 }
    87 int main()
    88 {
    89     LinkList *head;
    90     initial(&head,5);
    91 
    92     //LinkList *node = new LinkList;
    93     //node->datainfo = 6;
    94 //    insert(head,node,1);
    95     print(head);
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    摄影中的曝光补偿、白加黑减
    Excel表格中如何实现多列的同时筛选
    Excel表格中如何实现多列的同时筛选
    2013深圳茶博会
    2013深圳茶博会
    DiskTool 分区助手 - 免费易用的中文版“无损分区魔术师”!(完美支持Win7/32与64位系
    来自法国的山寨苹果系统——梨子系统PearOS,精美仿苹果风格的免费Linux操作系统(颇有iOS和OSX的神
    来自法国的山寨苹果系统——梨子系统PearOS,精美仿苹果风格的免费Linux操作系统(颇有iOS和OSX的神
    顺序stack的实现
    内核链表和普通链表的理解
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3303385.html
Copyright © 2020-2023  润新知