• 我真是日了 【单链表(线性表中按链式存储)】


    尾插法(无头结点头指针head里直接就有内容的)创建链表

    #include<iostream>
    #include<iomanip>
    #include<cmath>
    #include<cstring>
    #include<malloc.h>
    using namespace std;
    struct ListNode{
       int number;
       struct ListNode *next;
    
    };
    ListNode* CreateList(ListNode* head){
              ListNode *p,*q;
              int SIZE,i=0;
              head=NULL;
              q=head;
              cin>>SIZE;//输入链表元素的个数
              while(i<SIZE){
                 p=(ListNode*)malloc(sizeof(ListNode));
                 cin>>p->number;
                 if(head==NULL){ //空的一团 漂浮着 无实体 所以出现第一个新元素的时候直接等过去就行 head=p; 之后再是next
                    head=p;
                    q=p;
                 }
                 else{
                     q->next=p;
                     q=p;
                 }
                 i++;
              }
              q->next=NULL;
    
              return head;
         /*整个链表输入过程不用换行*/
    }
    void ShowList(ListNode *head){
    
         while(head!=NULL){
            cout<<head->number<<" ";
            head=head->next;
         }
         cout<<endl;
    }
    int main(){
    
       ListNode *head1,*head2;
       head1=CreateList(head1);
       head2=CreateList(head2);
       //链首指针要在主函数里
       ShowList(head1);
       ShowList(head2);
       return 0;
    }

    真他妈难以置信我这一个学期过了弄绷链表连接了  之前写的代码全他妈不对

    链表的合并 (相同元素只保留一个):ListNode *Merge(ListNode* head1,ListNode* head2){

            ListNode *p1=head1,*p2=head2;
            ListNode *temp;
            ListNode *head3, *c;
            head3=(ListNode*)malloc(sizeof(ListNode));//此时申请了一个指针空间 所以head不再是漂浮着了 里面有number还有指针 所以自然接下来c的操作就是next
            c=head3;///////////////////
            while(p1!=NULL&&p2!=NULL){
                if(p1->number<p2->number){
                      temp=p1;                           /*借助了中间指针*/
                      c->next=temp;
                      p1=p1->next;
                      c=c->next;
                }
                else if(p1->number>p2->number){
                      temp=p2;
                      c->next=temp;
                      p2=p2->next;
                      c=c->next;
                }
                else{
                      temp=p1;//借助中间变量
                      c->next=temp;
                      p1=p1->next;
                      p2=p2->next;//两者相同的话只出现一次 另一个也next
                      c=c->next;
                }
                if(p1){
                    c->next=p1;
                }
                else{
                    c->next=p2;
                }
            }
            return head3->next;/*
    为什么要返回head->next 就是因为根本head的number具体内容没有用也不保证是什么(我们当然没有输入它) 输出时指不定输出什么 92739236 1 2 3 4(后四个是合并)
    那么往其他函数传递的时候传的这个链表 是没有这个头结点的 就是传了一个一上来就是内容的链表 head就是贼普通的一个节点
    */

    删除链表相同元素只保留一个:

    NODE *delSame_2(NODE *head)
    {
        NODE *p,*q,*r;
        p = head->next; // 适用于有头节点的单链表;对于不带头节点的单链表,此处改为 p=head 即可。
        while(p != NULL)    // p用于遍历链表
        {
            q = p;
            while(q->next != NULL) // q遍历p后面的结点,并与p数值比较
            {
                if(q->next->data == p->data)
                {
                    r = q->next; // r保存需要删掉的结点
                    q->next = r->next;   // 需要删掉的结点的前后结点相接
                    free(r);
                }
                else
                    q = q->next;
            }
    
            p = p->next;
        }
    
        return head;
    }
  • 相关阅读:
    NPOI导出excel表格应用
    通过用户密码获取共享文件
    sql之left join、right join、inner join的区别
    C#日志编写
    C#预编译指令
    匿名类型(C# 编程指南)
    反射
    SQL通用查询
    Lambda 表达式(C# 编程指南)
    自定义用户控件编写——(文件夹目录选择)
  • 原文地址:https://www.cnblogs.com/yundong333/p/10481094.html
Copyright © 2020-2023  润新知