1.2数据结构瑞格单链表

本文涵盖了链表数据结构的各种操作,包括初始化链表、插入元素、删除最小值并打印、查找第K个元素、链表排序、反转链表以及输出链表内容。这些操作涉及到了链表的基本操作和高级应用,是理解链表和数据结构的重要实例。
摘要由CSDN通过智能技术生成

nefu瑞格单链表

–题目来源nefu-瑞格

4331

在这里插入图片描述

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

typedef struct Node
{
    int data;
    struct Node *next;
}Node;

Node *first;

void List()
{
	first = (struct Node *)malloc(sizeof(struct Node));
	first->next = first;
}

void Insert(int val)
{
	Node *n = (struct Node *)malloc(sizeof(struct Node));
	n->data = val;
	Node *temp=first;
	while (temp->next!=first)
		temp = temp->next;
	n->next = temp->next;
	temp->next = n;
}

// 删除并打印
void DeleteAndPrint()
{
    // write your code here
    Node *p,*r,*lmin1,*lmin2;
    int min,i;
    while(first->next!=first)
    {
         r=first;
         p=first->next;
         min=p->data;
         lmin1=r;
         lmin2=p;
         while(p->next!=first)
         {
             r=p;
             p=p->next;
             if(p->data<min)
             {
                 min=p->data;
                 lmin1=r;
                 lmin2=p;
             }
         }
         printf("%d ",min);
         lmin1->next=lmin2->next;
         free(lmin2);

    }
    free(first);
}

int main()
{
	List();
	int val;
	scanf("%d", &val);
	while (val!=-1)
	{
		Insert(val);
		scanf("%d", &val);
	}

	DeleteAndPrint();
	return 0;
}

4329

在这里插入图片描述

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

#define TRUE 1
#define FALSE 0

typedef int Status;

typedef struct Node {
        int data;
    struct Node *next;
}Node;

Node *first;

void Insert(int x)
{
    // rite your code here
    Node *n = (struct Node *)malloc(sizeof(struct Node));
	n->data = x;
	Node *temp=first;
	while (temp->next!=NULL)
		temp = temp->next;
	n->next = NULL;
	temp->next =n;
}
Status SearchK(int k,int *val)
{
    // write your code here
    Node *p;
    int i,t=0;
    p=first;
    while(p!=NULL)
    {
        t++;
        p=p->next;
    }
    p=first;
    for(i=0;i<t-k;i++)
    {
        p=p->next;
    }
    *val=p->data;
}

int main()
{
	first = (struct Node*)malloc(sizeof(struct Node));
	first->next = NULL;
	int i;
	scanf("%d", &i);
	while (i!=-1) {
		Insert(i);
		scanf("%d", &i);
	}
	int val;
	if (SearchK(4, &val))
		printf("%d\n", val);
	return 0;
}

4892

在这里插入图片描述
A

4891

在这里插入图片描述A

4890

在这里插入图片描述
D

4889

在这里插入图片描述
B

4888

在这里插入图片描述
C

4320

在这里插入图片描述

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

typedef struct Node {
    int data;
    struct Node *next;
}Node, *pNode;

pNode first;

int Length(pNode first)
{
    int i=0;
	Node *temp = first->next;
	while (temp!=NULL) {
		i++;
		temp = temp->next;
	}
	return i;
}

Node *getHead()
{
	return first;
}

void create(int a[], int n)
{
	Node *temp = first;
	int i;
	for (i=0;i<n;i++) {
		pNode t = (struct Node *)malloc(sizeof(struct Node));
		t->data = a[i];
		t->next = NULL;
		temp->next = t;
		temp = temp->next;
	}
}


void output()
{
    // write your code here
    Node *p=first->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }

}

void reverseList()
{
    // write your code here
    Node *n=first->next;
    Node *o=n->next;
    Node *t=o->next;
    n->next=NULL;
    while(t!=NULL)
    {
        o->next=n;
        n=o;
        o=t;
        t=t->next;
    }
    o->next=n;
    first->next=o;


}

int main()
{
	first = (struct Node *)malloc(sizeof(struct Node));
	int a[5];
	int i;
	for (i=0;i<5;i++)
		scanf("%d", a+i);
	create(a, 5);
	reverseList();
	output();
	return 0;
}

4319

在这里插入图片描述

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

typedef struct Node{
    int data;
    struct Node *next;
}Node, *pNode;

pNode first;

int Length(pNode first)
{
    int i=0;
	pNode temp = first->next;
	while (temp!=NULL) {
		i++;
		temp = temp->next;
	}
	return i;
}

pNode getHead()
{
	return first;
}

void create(pNode first, int a[], int n)
{
	Node *temp = first;
	int i;
	for (i=0;i<n;i++) {
		Node *t = (struct Node*)malloc(sizeof(struct Node));
		t->data = a[i];
		t->next = NULL;
		temp->next = t;
		temp = temp->next;
	}
}

void Insert(pNode first, int x)
{
	pNode n = (struct Node*)malloc(sizeof(struct Node));
	n->data = x;
	n->next = first->next;
	first->next = n;
}

void output(pNode first, int n)
{
	pNode temp = first->next;
	while (n--) {
		printf("%d ", temp->data);
		temp = temp->next;
	}
}

void reverseList(pNode first)
{
    // write your code here
    Node *n=first->next;
    Node *o=n->next;
    Node *t=o->next;
    n->next=NULL;
    while(t!=NULL)
    {
        o->next=n;
        n=o;
        o=t;
        t=t->next;
    }
    o->next=n;
    first->next=o;
}

int main()
{
	first = (struct Node *)malloc(sizeof(struct Node));
	first->next = NULL;
	pNode ha = (struct Node *)malloc(sizeof(struct Node));
	ha->next = NULL;
	int a[5];
	int i;
	for (i=0;i<5;i++)
		scanf("%d", a+i);
	create(ha, a, 5);
	reverseList(ha);
	output(ha, 5);
	return 0;
}

4885

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
typedef struct list{
    int data;
    struct list *next;
}node;
void creatlist(node *first,int n)
{
    int i;
    node *p,*r;
    r=first;
    for(i=0;i<n;i++)
    {
        p= (node *)malloc(sizeof(node));
        scanf("%d",&p->data);
        r->next=p;
        p->next=NULL;
        r=p;
    }
}
void link(node *first,node *second)
{
    node *p,*r;
    p=first;
    r=second->next;
    while(p->next!=NULL)
    {
        p=p->next;
    }
    p->next=r;
}
void paixu(node *first)
{
    int a[14];
    int i=0,j,t;
    node *p;
    p=first->next;
      while(p!=NULL)
    {
        a[i]=p->data;
        p=p->next;
        i++;
    }
    for(i=0;i<13;i++)
       for(j=i+1;j<14;j++)
       {
       if(a[i]<a[j])
           {
               t=a[i];
               a[i]=a[j];
               a[j]=t;
           }
        }
      p=first->next;
      i=0;
      while(p!=NULL)
    {
        p->data=a[i];
        p=p->next;
        i++;
    }
}
void printlist(node *first)
{
    node *p;
    p=first->next;
      while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}
int main()
{node *first,*second;
first=(node *)malloc(sizeof(node));
second=(node *)malloc(sizeof(node));
creatlist(first,8);
creatlist(second,6);
link(first,second);
paixu(first);
printlist(first);
    return 0;
}

4317

在这里插入图片描述

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

typedef struct Node {
        int data;
        struct Node *next;
}Node, *pNode;

Node *first;

int Length(pNode head)
{
	int i=0;
	pNode first = head;
	pNode temp = first->next;
	while (temp!=NULL) {
		i++;
		temp = temp->next;
	}
	return i;
}

pNode Locate(pNode head, int i)
{
    // write your code here
    Node *p=head->next;
    int j;
    for(j=1;j<i;j++)
    {
        if(p==NULL)
           return NULL;
        else
           p=p->next;
    }
    return p;
}

pNode max(pNode head)
{
    // write your code here
    Node *p=head->next;
    Node *m=p;
    while(p!=NULL)
    {
       if(p->data>m->data)
           m=p;
        p=p->next;
    }
    return m;
}

int number(pNode head, int x)
{
    // write your code here
    Node *p=head->next;
    int i=0;
    while(p!=NULL)
    {
        if(p->data==x)
           i++;
        p=p->next;
    }
    return i;
}

void create(pNode head, int a[],int n)
{
    // write your code here
    Node *p,*r=head;
    int i=0;
    while(i<n)
    {
        p=(Node *)malloc(sizeof(Node));
        p->data=a[i];
        r->next=p;
        p->next=NULL;
        r=p;
        i++;
    }
    
}
	

void output(pNode head)
{
	pNode first = head;
	pNode temp = first->next;
	while (temp!=NULL) {
		printf("%d ", temp->data);
		temp = temp->next;
	}
}

int main()
{
	pNode head = (struct Node*)malloc(sizeof(struct Node));
	int a[10];
    int i;
    for(i=0;i<10;i++)
	{
		scanf("%d", a+i);
	}
	create(head, a, 10);
	printf("%d\n", Locate(head, 3)->data);
	printf("%d\n", max(head)->data);
	printf("%d\n", number(head, 7));
	output(head);
	getchar();
	return 0;
}

4883

在这里插入图片描述

#include<iostream>
#include<stdlib.h>
using namespace std;

struct Node {
    int data;
	Node *next;
};

class List {
private:
	Node *first;
public:
	List();
	~List();
	void makeEmpty();
	void inputFront(int endTag);
	void output();
};

List::List()
{
	first = new Node();
	first->next = NULL;
}

List::~List()
{
	makeEmpty();
}

void List::makeEmpty()
{
	Node *q;
	while (first->next!=NULL) {
		q = first->next;
		first->next = q->next;
		delete q;
	}
}

void List::inputFront(int endTag)
{
	//write your code here
    first=(Node *)malloc(sizeof(Node));
    int x;
    Node *p,*r;
    r=NULL;
    scanf("%d",&x);
    while(x!=endTag)
    {
        p = new Node();
        p->data=x;
        p->next=r;
        first->next=p;
        r=p;
        scanf("%d",&x);
    }
}

void List::output()
{
	Node *temp = first->next;
	while (temp!=NULL) {
		cout << temp->data << " ";
		temp = temp->next;
	}
}

int main()
{
	List l;
	l.inputFront(-1); //结束符为-1
	l.output();
	return 0;
}

4867

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list
{  int data;
   struct list *next;
};
void creatlist(int n,struct list *first)
{
    int i=0,x;
    struct list *p,*r;
    r=(struct list *)malloc(sizeof(struct list));
    r=NULL;
    while(i<n)
    {   scanf("%d",&x);
        p=(struct list *)malloc(sizeof(struct list));
        p->data=x;
        p->next=r;
        first->next=p;
        r=p;
        i++;
    }
}
void get(struct list *first,int x,int n)
{   struct list *p=first;
    int i;
    if(x>=n)
       printf("get fail\n");
    for(i=0;i<x;i++)
    {   p=p->next;
    }
    printf("%d\n",p->data);
}
void insert(struct list *first,int x,int y,int *n)
{   struct list *p=first,*r=first->next,*t;
    int i=1;
    if(x>*n||x<=0)
       printf("insert fail\n");
    else
    {
       while(i<x)
       {
         p=p->next;
         r=r->next;
         i++;
       }
      t=(struct list *)malloc(sizeof(struct list));
      t->data=y;
      p->next=t;
      t->next=r;
      *n=*n+1;
      printf("insert OK\n");
    }
}
void delete1(struct list *first,int x,int *n)
{   struct list *p=first,*r=first->next;
    int i=1;
    if(x>*n||x<=0)
       printf("delete fail\n");
    else
    {
       while(i<x)
       {
         p=p->next;
         r=r->next;
         i++;
       }
      p->next=r->next;
      *n=*n-1;
      printf("delete OK\n");
    }
}
void show(struct list *first,int n)
{ if(n==0)
  {
      printf("Link list is empty\n");
  }
  else
  {
      struct list *p;
      p=first->next;
      while(p!=NULL)
      {
          printf("%d ",p->data);
          p=p->next;
      }
      printf("\n");
  }
}
int main()
{   int m,n,i,x,y;
    char a[4],b[7],c[7],d[5];
    strcpy(a,"get");
    strcpy(b,"insert");
    strcpy(c,"delete");
    strcpy(d,"show");
    scanf("%d",&n);
    struct list *first;
    first=(struct list *)malloc(sizeof(struct list));
    creatlist(n,first);
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
      char *p;
      p=(char *)malloc(sizeof(char)*6);
      scanf("%s",p);
      if(strcmp(p,a)==0)
      {    scanf("%d",&x);
           get(first,x,n);
      }
      if(strcmp(p,b)==0)
      {    scanf("%d %d",&x,&y);
           insert(first,x,y,&n);
      }
      if(strcmp(p,c)==0)
      {    scanf("%d",&x);
           delete1(first,x,&n);
      }
      if(strcmp(p,d)==0)
      {   show(first,n);
      }
    }

    return 0;
}

4865

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list
{  int data;
   struct list *next;
};
void creatlist(int n,struct list *first)
{
    int i=0,x;
    struct list *p,*r;
    r=(struct list *)malloc(sizeof(struct list));
    r=NULL;
    while(i<n)
    {   scanf("%d",&x);
        p=(struct list *)malloc(sizeof(struct list));
        p->data=x;
        p->next=r;
        first->next=p;
        r=p;
        i++;
    }
}
void show(struct list *first)
{
      struct list *p;
      p=first->next;
      while(p!=NULL)
      {
          printf("%d ",p->data);
          p=p->next;
      }
      printf("\n");
}

void delete1(struct list *first,int x,int n)
{   struct list *p=first,*r=first->next;
    int i=1;
    if(x>n||x<=0)
    {  show(first);
       printf("INPUT ERROR\n");
       show(first);
    }
    else
    {  show(first);
       while(i<x)
       {
         p=p->next;
         r=r->next;
         i++;
       }
     printf("%d\n",r->data);
     p->next=r->next;
     show(first);
      
    }
}

int main()
{   int m,n;
    scanf("%d",&m);
    struct list *first;
    first=(struct list *)malloc(sizeof(struct list));
    creatlist(m,first);
    scanf("%d",&n);
    delete1(first,n,m);
    

    return 0;
}

4864

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list
{  int data;
   struct list *next;
};
void creatlist(int n,struct list *first)
{
    int i=0,x;
    struct list *p,*r;
    r=(struct list *)malloc(sizeof(struct list));
    r=NULL;
    while(i<n)
    {   scanf("%d",&x);
        p=(struct list *)malloc(sizeof(struct list));
        p->data=x;
        p->next=r;
        first->next=p;
        r=p;
        i++;
    }
}
void show(struct list *first)
{ 
      struct list *p;
      p=first->next;
      while(p!=NULL)
      {
          printf("%d ",p->data);
          p=p->next;
      }
      printf("\n");
}
void insert(struct list *first,int x,int y,int n)
{   struct list *p=first,*r=first->next,*t;
    int i=1;
    if(x>n||x<=0)
    {  show(first);
       printf("INPUT ERROR");
    }
    else
    {  show(first);
       while(i<x)
       {
         p=p->next;
         r=r->next;
         i++;
       }
      t=(struct list *)malloc(sizeof(struct list));
      t->data=y;
      p->next=t;
      t->next=r;
      show(first);
    }
}
int main()
{   int m,x,y;
    struct list *first;
    scanf("%d",&m);
    first=(struct list *)malloc(sizeof(struct list));
    creatlist(m,first);
    scanf("%d %d",&x,&y);
    insert(first,x,y,m);
    

    return 0;
}

4863

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list
{  int data;
   struct list *next;
};
void creatlist(int n,struct list *first)
{
    int i=0,x;
    struct list *p,*r;
    r=(struct list *)malloc(sizeof(struct list));
    r=NULL;
    while(i<n)
    {   scanf("%d",&x);
        p=(struct list *)malloc(sizeof(struct list));
        p->data=x;
        p->next=r;
        first->next=p;
        r=p;
        i++;
    }
}
void show(struct list *first)
{ 
      struct list *p;
      p=first->next;
      while(p!=NULL)
      {
          printf("%d ",p->data);
          p=p->next;
      }
      printf("\n");
}
void get(struct list *first,int x,int n)
{   struct list *p=first;
    int i;
    if(x>=n)
    {  show(first);
       printf("INPUT ERROR");
    }
    else
    {
      for(i=0;i<x;i++)
      {     p=p->next;
      }
      show(first);
      printf("%d\n",p->data);
    }
}
int main()
{   int m,x,y;
    struct list *first;
    scanf("%d",&m);
    first=(struct list *)malloc(sizeof(struct list));
    creatlist(m,first);
    scanf("%d",&x);
    get(first,x,m);
    

    return 0;
}

4862

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list
{  int data;
   struct list *next;
};
void creatlist(int n,struct list *first)
{
    int i=0,x;
    struct list *p,*r;
    r=(struct list *)malloc(sizeof(struct list));
    r=NULL;
    while(i<n)
    {   scanf("%d",&x);
        p=(struct list *)malloc(sizeof(struct list));
        p->data=x;
        p->next=r;
        first->next=p;
        r=p;
        i++;
    }
}
void show(struct list *first)
{ 
      struct list *p;
      p=first->next;
      while(p!=NULL)
      {
          printf("%d ",p->data);
          p=p->next;
      }
      printf("\n");
}
int main()
{   int m,x,y;
    struct list *first;
    scanf("%d",&m);
    first=(struct list *)malloc(sizeof(struct list));
    creatlist(m,first);
    show(first);
    

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值