zoukankan      html  css  js  c++  java
  • 链表的基本操作实现

    任务描述 :前几个的实现是线性表的基本操作 现在实现的是链表基本操作的实现。基本上是建立新结点 结点的长度 删除结点 插入结点 合并结点 显示结点的功能。

    #include<iostream>
    
    using namespace std;
    
    typedef int status;
    
    
    //存储结构的类型定义 返回函数的状态结果代码
    typedef int ElemType;
    //数据元素/結点的表示 这个是用户自定义的数据类型 用于结点
    
    typedef struct LNode{
    
    	ElemType data;//值域
    
    	struct LNode *next;//这个链表类中的指针域
    
    }*LinkList;//*LinkList用于定义头指针
    
    
    
    void CreatList(LinkList &L,int n)
    {
    
    
    
    	L =(LinkList)malloc(sizeof(LNode));//系统动态生成一个LNode的结点 同时将该结点的起始位置赋给指针变量
    
    	L->next = NULL;//很明显 这是一个头结点
    
    
    
    	LinkList p;
    
    
    	LinkList s;
    
    	s = L;
    
    	for(int i =n;i>0;--i)
    
    	{
    		p = (LinkList)malloc(sizeof(LNode));//生成新结点
    
    		cin>>p->data;
    
    		p->next =s->next;
    
    		s->next =  p;
    
    		s = s->next;
    	}
    }
    
    int listlength(LinkList &L)
    
    {
    
    	LinkList p;
    
    	p=L;
    
    	int j =0;
    
    
    while(p->next!=NULL)
    {
    
    	p = p->next;
    
    	++j;
    
    }
    
    return j;
    }
    
    void uniform(LinkList &La,LinkList &Lb,LinkList &Lc)
    {
    
    	LinkList pa ;
    
    	LinkList pb ;
    
    	LinkList pc ;
    
    	Lc =(LinkList)malloc(sizeof(LNode));
    
    	pa = La->next;//将头结点指向的下一个结点的指针给pa
    
    	pb = Lb->next;
    
    	pc = La;//这里比较关键 由于刚开始Lc链表没有值 那么首先它的指针指向是La
    
    	Lc = pc;//这时的指针也是指向Lc的
    
    	while(pa&&pb)
    
    	{
    	
    		if(pa->data<=pb->data)
    		{
    		
    		
    			pc->next = pa;//pa的指针指向pc的下一个结点
    
    
    			pc = pa;
    
    			pa = pa->next;
    
    		}
    
    		else
    		{
    
    			pc->next = pb;
    
    
    			pc = pb;
    
    			pb = pb->next;
    		
    		}
    	
    	}
    
    	pc->next =pa?pa:pb;
    
    
    
    }
    
    void ListDelete(LinkList &L,int i)
    
    {
    
    
    	LinkList p;
    
    	p=L;//头结点的指针给p
    
    	int j =0;
    
    	while(p&&j<i-1)
    
    	{
    		p = p->next;
    
    		++j;
    	}
    
    	p->next = p->next->next;
    }
    
    void ListInsert(LinkList &L,int i,ElemType e)
    {//在带头结点的单链表L中第i个位置之前插入元素e
    
    	LinkList p;
    
    	LinkList s;
    	p=L;
    
    	int j = 0;
    
    	while(p&&j<i-1)
    	{
    		p=p->next;
    
    		++j;
    	
    	}//此时的p已经定好要插入的位置了
    
    		s = (LinkList)malloc(sizeof(LNode));
    
    		s->data =e;
    
    		s->next = p->next;
    
    		p->next =s;
    
    
    
    }
    
    
    
    void display(LinkList &L)
    
    {
    
    	LinkList p;
    
    	p= L;
    
    	int j = 0;
    
    	while(j<listlength(L))
    
    	{
    	
    		p = p->next;
    
    		cout<<p->data<<"  ";
    
    	}
    
    }
    
    
    
    void  main()
    {
    
    	LinkList La;
    
    	LinkList Lb;
    
    	LinkList Lc;
    	
    	cout<<"来吧 输入数据吧!"<<endl;
    
    	CreatList(La,4);
    
    
    
    	
    	cout<<"不够爽 在来一波"<<endl;
    
    
    	CreatList(Lb,7);
    
    	
    	uniform(La,Lb,Lc);
    
    	
    
        cout <<endl;
    
    
    	display(Lc);
    
    
    
    
    }


  • 相关阅读:
    #背包方案 ——整数划分(Acwing900)
    #分组背包 #背包方案 ——Acwing 1013 机器分配
    #背包 #二进制优化 ——Acwing 5. 多重背包问题 II(二进制优化)
    #背包方案 AcWing 532. 货币系统
    #背包方案 ——AcWing 1021. 货币系统2
    背包问题求方案数
    有依赖的背包问题
    分组背包问题
    二维费用的背包问题
    混合背包问题
  • 原文地址:https://www.cnblogs.com/riskyer/p/3317956.html
Copyright © 2011-2022 走看看