• 单链表之删除头结点,查找等于定值x的结点数,单链表的逆置


    /*
     * @Author: 一届书生
     * @Date: 2020-03-08 09:52:27
     * @LastEditTime: 2020-03-08 13:58:30
     */
    #include <iostream>
    using namespace std;
    #define elemType char
    
    typedef struct node
    {
        elemType data;
        struct node *next;
    } Lnode, *Pnode;
    
    // 带头结点-尾插法
    Pnode create1()
    {
        Pnode head, tail, p; //头结点 尾结点  临时结点
        elemType i;
        head = new node; //申请头结点
        tail = head;     //头尾指针指向同一个结点
        while ((cin >> i) && (i != '*'))
        {
            p = new node;   //申请新结点
            p->data = i;    //输入结点的数据域
            tail->next = p; //将新结点插入链表尾部
            tail = p;       //让链表的尾结点指向新结点
        }
        tail->next = NULL; //尾指针指向空
        return head;
    };
    
    // 带头结点-头插法
    Pnode create2()
    {
        Pnode head, p; //头结点 尾结点  临时结点
        head = new node;
        head->next = NULL;
        elemType x;
        while (cin >> x)
        {
            p = new node;
            p->data = x;
            p->next = head->next;
            head->next = p;
        }
        return head;
    };
    
    // 删除头结点
    Pnode deletHeadNode(Pnode &a)
    {
        if (a == NULL)
        {
            cout << "空表" << endl;
            return a;
        }
        Pnode t = a;
        a = a->next;
        t->next = NULL;
        free(t);
        return a;
    }
    
    // 添加头结点
    Pnode addHeadNode(Pnode &p){
        Pnode head;
        head=new node;
        head->next=p;
        return head;
    }
    
    // 统计出带头单链表中结点的值等于给定值X的结点数
    int getNumX(Pnode a,elemType x){
        int cnt=0;
        a=a->next;
        while(a){
            if(a->data==x)
                cnt++;
            a=a->next;
        }
        return cnt;
    }
    
    // 单链表逆置     头插法
    void reverse(Pnode &head) {
        Pnode t,p,q=head->next;     
        p=q->next;
        q->next=NULL;
        while(p){
            t=p->next;
            p->next=q;
            q=p;
            p=t;
        }
        head->next=q;
    }
    
    // 带头结点遍历输出
    void display(Pnode head){   //输出链表中的数据
        Pnode p;
        p = head->next;
        while (p != NULL){
            cout<< p->data;
            if (p->next != NULL)
            cout<<' ';
            p = p->next; //p指向下一个结点
        }
        cout<<endl;
    }
    
    int main()
    {
        // 样例:123456789111
        Pnode p;
        p = create1();
        display(p); // 带头结点遍历输出 
                    // 输出:1 2 3 4 5 6 7 8 9 1 1 1
    
        p = deletHeadNode(p);
        display(p); // 查看是否已经删除了头结点,若成功删除的话,应该从第二个结点开始输出
                    // 输出:2 3 4 5 6 7 8 9 1 1 1
    
        p=addHeadNode(p);
        display(p); //加上头结点后,再遍历一遍看看结果
                    // 输出:1 2 3 4 5 6 7 8 9 1 1 1
    
        elemType t='1';
        cout<<getNumX(p,t)<<endl;     //单链表:123456789111   输出:4
    
        reverse(p);
        display(p);     // 输出:1 1 1 9 8 7 6 5 4 3 2 1
    
        return 0;
    }
    

      

  • 相关阅读:
    广域网详解
    无线AP和无线路由器区别
    TRUNK的作用功能.什么是TRUNK
    name after, name for, name as
    让你的情商爆棚吧!
    综合布线系统之7个子系统构成
    网桥和交换机的工作原理及区别
    边界网关协议BGP
    OSPF协议详解
    路由信息协议(RIP)的防环机制
  • 原文地址:https://www.cnblogs.com/52dxer/p/12442393.html
Copyright © 2020-2023  润新知