数据结构作业三

算法1

#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{  
    datatype data;
    struct node *next;  
}Lnode;
Lnode *init_LinkList()
{  
    Lnode *list=(Lnode*)malloc(sizeof(Lnode));
    if(list==NULL){
        printf("创建失败,退出程序");
        exit(0);
    }else{
        list->next=NULL;
        return list;
    }
}
void delete_prior(Lnode *list)
{
    Lnode *q,*p1,*p2;
//    q=init_LinkList();p1=init_LinkList();p2=init_LinkList();
    //q是搜索s前驱的前驱,p1是搜索s前驱,p2是搜索s
    q = list;
    p1 = q->next;
    p2 = p1->next;
    while(p2 != list)
    {
        p2 = p2->next;
        p1 = p1->next;
        q = q->next;
    }
    q->next = p2;
    delete(p1);
}
void printList(Lnode *node)
{
     Lnode *next;
     if (!node){return;}
     printf("%d", node->data);
     next = node->next;
     while (node != next)
       {
          printf("-->%d", next->data);
          next = next->next;
       }
          printf("\n");
}
int main()
{
	
   Lnode *lis1,*lis2,*lis3,*lis4,*lis5;
   lis1=init_LinkList();lis2=init_LinkList();lis3=init_LinkList();lis4=init_LinkList();lis5=init_LinkList();
   lis1->data=1;lis2->data=2;lis3->data=3;lis4->data=4;lis5->data=5;
   lis1->next=lis2;lis2->next=lis3;lis3->next=lis4;lis4->next=lis5;lis5->next=lis1;
   printList(lis2);
   delete_prior(lis2);
   printList(lis2);
}

image-20220422203513366

算法2

#include<stdio.h>
typedef int datatype;
typedef struct LNode
{
	datatype data;
	LNode *next;
}LNode,*LinkList;
int CreateList(LinkList &L,int n)
{
	LNode *p,*r;int i;
	L=new LNode;
	L->next=NULL;
	r=L;
	for(i=0;i<n;i++)
	{
		p=new LNode;
		scanf("%d",&p->data);
		p->next=NULL;
		r->next=p;
		r=p;
	}
	return 0;
}
void display(LinkList L)
{
	LNode *p;
	p=L->next;
	printf("{");
	while(p)
	{
	 printf("%d ",p->data);
	 p=p->next;
	}
	printf("}\n");
}
void MergeList_L(LinkList &LA,LinkList &LB,LinkList &LC)
{
    LNode *pa;LNode *pb;LNode *pc;LNode *p;
    pa=LA->next;pb=LB->next;
    LC=LA;
    pc=LC;
    while(pa&&pb)
    {
        if(pa->data==pb->data)
        {
            pc->next=pa;
            pc=pa;
            pa=pa->next;
            pb=pb->next;
        }
        else if (pa->data<pb->data)
            pa=pa->next;
        else
            pb=pb->next;
    }
    pc->next=NULL;
    LA=LC;
    delete LB;
}
int main()
{
	LinkList LA;LinkList LB;LinkList LC;
	int m,n;
	scanf("%d",&m);
	CreateList (LA,m);
	scanf("%d",&n); 
	CreateList (LB,n);
	display(LA);
	display(LB);
	MergeList_L(LA,LB,LC);
	display(LC);
	return 0;
}

image-20220422223452092

算法3

#include <stdio.h>
#include <stdlib.h>
typedef struct Lnode {
    char s;
    struct Lnode *next;
    struct Lnode *prior;
    int freq;
}Lnode,*LinkList;
LinkList create(LinkList tail,char c)
{
    LinkList p=(LinkList)malloc(sizeof(Lnode));
    p->s=c;
    p->freq=0;
    tail->next=p;
    p->prior=tail;
    tail=p;
    return tail;
}
LinkList locate(LinkList l,int n,int m)
{
   char c;
   LinkList p,q,h;
   p=l->next;
   int i=0;
   while(1)
   {
      c = getchar();
        if(c != ' '&&c != '\n')
        {
            while(p)
            {
                if(p->s == c)
                    (p->freq)++;
                  p = p->next;
            }
 
        p=l->next;
        i++;
        if(i==n) break;
        }
   }
    for(i = 0;i < m;i++)
    {
        p = l->next;
        h = p;
        q = l->next;
        while(q)
        {
            if(h->freq< q->freq)h = q;
            q = q->next;
        }
        printf("%c ",h->s);
        if(h->next == NULL)h->prior->next = NULL;
        else
        {
            if(h->prior == NULL)
            {
                l = l->next;
                l->prior = NULL;
            }
            else
            {
                h->next->prior = h->prior;
                h->prior->next = h->next;
            }
        }
        free(h);
    }
    printf("\n");
    return 0;
}
int main()
{
    LinkList l,tail;
    l=(LinkList)malloc(sizeof(Lnode));
    l->prior=NULL;
    tail=l;
    int m,n;
    scanf("%d%d",&m,&n);
    int i = 0;
    char c;
    while(1)
    {
        c = getchar();
        if(c != ' '&&c != '\n')
        {
            tail= create(tail,c);
            i++;
            if(i == m)break;
        }
    }
    tail->next = NULL;
    locate(l,n,m);
    return 0;
}

image-20220422234447432

posted @ 2022-04-22 23:55  m1m0ry  阅读(34)  评论(0编辑  收藏  举报