首页 > 其他分享 >shiyan2

shiyan2

时间:2024-10-11 17:25:30浏览次数:7  
标签:Lc next LList shiyan2 data ptr LNode

#include<stdio.h>
#include<malloc.h>

#define SIZE 100
#define INCREMENT_SIZE 10

#define TRUE 1
#define FALSE -1
#define OK 1
#define ERROR -1

typedef int Status;
typedef int ElemType;

typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LList;

Status create(LList &L,int n)
{
LList p;
L=(LList)malloc(sizeof(LNode));
L->next=NULL;
for(int i=n;i>0;--i)
{
p=(LList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=L->next;
L->next=p;
}
return OK;
}

Status MergeList_L(LList &La,LList &Lb,LList &Lc)
{
LList pa,pb,pc;
pa=La->next;
pb=Lb->next;
Lc=pc=La;
while(pa&&pb)
{
if(pa->data<=pb->data)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
else
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
}
pc->next=pa?pa:pb;
free(Lb);
return OK;
}

Status Purge(LList &L)
{
LList p,q,r,s;
p=L->next;
while(p)
{
r=p;
q=p->next;
while(q)
{
if(q->data!=p->data)
{
r=q;
q=q->next;
}
else
{
s=q;
q=q->next;
r->next=q;
delete s;
}
p=p->next;
}
}
}

Status Print(LList L){
LNode* p = L->next;
while(p){
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return OK;
}

int main()
{
LList La, Lb, Lc;

create(La,5);
create(Lb,6);
MergeList_L(La,Lb, Lc);
Print(Lc);
Purge(Lc);

}

 

 

 

 

#include<stdio.h>

#include<malloc.h>

 

#define SIZE  100

#define INCREMENT_SIZE 10

 

#define TRUE  1

#define FALSE  -1

#define OK     1

#define ERROR  -1

 

typedef int Status;

typedef int ElemType;

 

typedef struct LNode{

       ElemType data;

       struct LNode* next;

}LNode, *LinkList;

 

Status CreateList(LinkList* L, int length){

       // 输入length个值,用空格分开

       *L = (LNode*)malloc(sizeof(LNode));

       (*L)->next = NULL;

       LNode* ptr = *L;

       for(int i=1;i<=length;i++){

              ElemType data;

              scanf("%d",&data);

              ptr->next = (LNode*)malloc(sizeof(LNode));

              ptr->next->data = data;

              ptr = ptr->next;

       }

       ptr->next = NULL;

       return OK;

}

 

Status PrintList(LinkList L){

       // 有头结点,所以ptr跳到L的下一个结点

       LNode* ptr = L->next;

       while(ptr){

              printf("%d ",ptr->data);

              ptr = ptr->next;

       }

       printf("\n");

       return OK;

}

 

Status ListAppend(LinkList L){

       // 在链表尾部插入一个结点

       LNode* ptr = L;

       while(!ptr->next){

              ElemType data;

              scanf("%d",&data);

              LNode* newNode = (LNode*)malloc(sizeof(LNode));

              newNode->data = data;

              newNode->next = NULL;

              ptr->next = newNode;

       }

       return OK;

}

 

Status ListDel(LinkList L,int index){

       // index从1开始,index=i代表欲删除第i个结点

       LNode* ptr = L;

       int count = 1;

       while(count < index){

              ptr = ptr->next;

       }

       LNode* temp = ptr->next;

       ptr->next = temp->next;

       free(temp);

       return OK;

}

 

Status MergeList(LinkList La,LinkList Lb,LinkList* Lc){

       LNode* ptr_a = La->next;

       LNode* ptr_b = Lb->next;

       // 先初始化Lc,创建头结点

       *Lc = (LNode*)malloc(sizeof(LNode));

       (*Lc)->next = NULL;

       LNode* ptr_c = *Lc;

       while(ptr_a&& ptr_b){

              if(ptr_a->data <= ptr_b->data){

                     ptr_c->next = ptr_a;

                     ptr_a = ptr_a->next;

                     ptr_c = ptr_c->next;

              }else{

                     ptr_c->next = ptr_b;

                     ptr_b = ptr_b->next;

                     ptr_c = ptr_c->next;

              }

       }

       if(ptr_a){

              ptr_c->next = ptr_a;

       }else{

              ptr_c->next = ptr_b;

       }

       return OK;

}

 

Status PurgeList(LinkList* L){

       // 删除重复元素

       LNode* ptr1 = (*L)->next;

       while(ptr1->next){

              LNode* ptr2 = ptr1;

              if(ptr2->data == ptr2->next->data){

                     ptr2 = ptr2->next;

                     ptr1->next = ptr2->next;

                     free(ptr2);

              }else{

                     ptr1 = ptr1->next;

              }

       }

       return OK;

}

 

int main(){

       LinkList La,Lb,Lc;

       CreateList(&La,5);

       PrintList(La);

       CreateList(&Lb,6);

       PrintList(Lb);

       MergeList(La,Lb,&Lc);

       PrintList(Lc);

       PurgeList(&Lc);

       PrintList(Lc);

       return 0;

}

标签:Lc,next,LList,shiyan2,data,ptr,LNode
From: https://www.cnblogs.com/JUVBUFFON/p/18458902

相关文章

  • shiyan2
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;srand(time(......