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;
}