其他分享
首页 > 其他分享> > SDUT ACM OJ 实验二链表

SDUT ACM OJ 实验二链表

作者:互联网

A :顺序建立链表

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

struct node
{
    int data;
    struct node *next;
}*head,*tail,*p,*q;

int main ()
{
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    int n,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    head=head->next;
	while(head->next!=NULL)
	{
		printf("%d ",head->data);
		head=head->next;
	}
	printf("%d\n",head->data);
}

B :逆序建立链表

#include <stdio.h>
#include <stdlib.h>
struct node
{
	int data;
	struct node *next;
}*head,*tail,*q,*p;
int main()
{
	int n,i;
	scanf("%d",&n);
	head=(struct node *)malloc(sizeof(struct node));
	head->next=NULL;
	tail=head;
	for(i=0;i<n;i++)
	{
		p=(struct node *)malloc(sizeof(struct node));
		scanf("%d",&p->data);
		p->next=tail->next;
		tail->next=p;
	}
	head=head->next;
	while(head->next!=NULL)
	{
		printf("%d ",head->data);
		head=head->next;
	}
	printf("%d\n",head->data);
}

C : 链表的结点插入

#include<stdio.h>
#include<stdlib.h>
struct node
{
   int data;
   struct node *next;
}*head,*tail,*p,*q;
int main()
{
	int n,i,a;
	while(scanf("%d",&n)!=EOF)
	{
	head=(struct node *)malloc(sizeof(struct node));
	head->next=NULL;
	for(i=0;i<n;i++)
	{
		p=(struct node *)malloc(sizeof(struct node));
		scanf("%d %d",&a,&p->data);
		q=head;
		while(a--&&q->next!=NULL)
		{
			q=q->next;
		}
		p->next=q->next;
		q->next=p;
	}
	head=head->next;
	while(head->next!=NULL)
	{
		printf("%d ",head->data);
		head=head->next;
	}
	printf("%d\n",head->data);
	}
}

D :单链表中重复元素的删除

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
 
};
struct node*creat()
{
   struct node *tail,*p,*head;
   head = (struct node*)malloc(sizeof(struct node));
   head->next=NULL;
   tail = head;
   while(1)
   {
 
       p = (struct node*)malloc(sizeof(struct node));
 
       scanf("%d",&p->data);
        if(p->data==-1)break;
        p ->next = NULL;
       tail->next = p;
       tail = p;
   }
  return (head);
}

struct node*ncreat(int n)
{   
   struct node *head,*p;
   head = (struct node*)malloc(sizeof(struct node));
   head->next = NULL;
   for(int i=1;i<=n;i++)
   {
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next = head->next;
        head->next = p;
   }
   return (head);
}

void show(struct node*head)
{
  struct node *p;
  p =head->next;
  while(p)
  {
    if(p->next!=NULL)
    {
       printf("%d ",p->data);
    }
    else
    {
       printf("%d\n",p->data);
    }
    p = p->next;
  }
}

int del(struct node *head,int n)
{
    struct node *p,*q,*t;
    p = head->next;
    while(p)
    {
        q = p;
        t = q->next;
        while(t)
        {
           if(p->data==t->data)
           {
                q->next = t->next;
                free(t);
                t = q->next;
                n--;
           }
           else
           {
              q = t;
              t = q->next;
           }
        }
        p = p->next;
    }
  return n;
}

void nizhi(struct node*head)
{
    struct node *p,*q;
    p = head->next;
    head ->next = NULL;
    q = p->next;
    while(p)
    {
        p->next = head->next;
        head->next = p;
        p = q;
        if(q)
        {
          q = q->next;
        }
    }
}

int main()
{
    struct node *head;
    head = (struct node*)malloc(sizeof(struct node));
    int n;
    scanf("%d",&n);
    head=ncreat(n);
    printf("%d\n",n);
    show(head);
    int m = del(head,n);
    printf("%d\n",m);
    show(head);
   return 0;
}

E :链表的逆置

#include <stdio.h>
#include <stdlib.h>
struct node
{
	int data;
	struct node *next;
}*head,*tail,*q,*p;

int main()
{
	int x;
	head=(struct node *)malloc(sizeof(struct node));
	head->next=NULL;
	tail=head;
	while(~scanf("%d",&x)&&x!=-1)
	{
		p=(struct node *)malloc(sizeof(struct node));
		p->data=x;
		p->next=tail->next;
		tail->next=p;
	}
	head=head->next;
	while(head->next!=NULL)
	{
		printf("%d ",head->data);
		head=head->next;
	}
	printf("%d\n",head->data);
}

F :有序链表的归并

#include<stdio.h>
#include<stdlib.h>
struct node
{
   int data;
   struct node *next;
}*head1,*head,*tail,*p,*q;

int main()
{
	int m,n;
	scanf("%d %d",&m,&n);
	head=(struct node *)malloc(sizeof(struct node));
	head1=(struct node *)malloc(sizeof(struct node));
	head->next=head1->next=NULL;
	tail=head;
	while(m--)
	{
		p=(struct node *)malloc(sizeof(struct node));
		scanf("%d",&p->data);
		p->next=tail->next;
		tail->next=p;
		tail=p;
	}
	tail=head1;
	while(n--)
	{
		p=(struct node *)malloc(sizeof(struct node));
		scanf("%d",&p->data);
		p->next=tail->next;
		tail->next=p;
		tail=p;
	}
	p=head->next;
	tail=head;
	head->next=NULL;
	free(head1);
	while(p&&q)
	{
		if(p->data>q->data)
		{
			tail->next=q;
			tail=q;
			q=q->next;
		}
		else
		{
			tail->next=p;
			tail=p;
			p=p->next;
		}
	}
	if(p)
	{
		tail->next=p;
	}
	else
	{
		tail->next=q;
	}
	p=head->next;
	printf("%d",p->data);
	p=p->next;
	while(p)
	{
		printf(" %d",p->data);
		p=p->next;
	}
	printf("\n");
}

G :单链表的拆分

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int b[100002],c[200002],i,x=0,y=0,n,a;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a);
		if(a%2==0)
		{
			b[x]=a;
			x++;
		}
		else
		{
			c[y]=a;
			y++;
		}
	}
	printf("%d %d\n",x,y);
	for(i=0;i<x-1;i++)
	{
		printf("%d ",b[i]);
	}
	printf("%d\n",b[x-1]);
	for(i=0;i<y-1;i++)
	{
		printf("%d ",c[i]);
	}
	printf("%d\n",c[y-1]);
}

H :双向链表

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

struct node
{
    int data;
    struct node *next,*pre;
}*head,*r,*p,*q;

int main ()
{
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    head->pre=NULL;
    r=head;
    int m,n,x,i;
    scanf("%d %d",&n,&m);
    for(i=0;i<n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=r->next;
        p->pre=r;
        r->next=p;
        r=p;
    }
    for(i=0;i<m;i++)
	{
		scanf("%d",&x);
		p=head->next;
		while(p->data!=x){p=p->next;}
		if(p->pre==head){printf("%d\n",p->next->data);}
		else if(p->next==NULL){printf("%d\n",p->pre->data);}
		else printf("%d %d\n",p->pre->data,p->next->data);
	}
}

I : 约瑟夫问题

//这道题我没有用链表写,因为链表太冗长繁杂
//放心考试一样过
#include<stdio.h>
int main()
{
  int n,m,i,p=0;
  scanf("%d %d",&n,&m);
  for(int i=2;i<=n;i++)
  {p=(p+m)%i;}
  printf("%d",p+1);
}

J : 不敢死队问题

//这道题我也没有用链表写,你懂的
#include<stdio.h>
#include<stdlib.h>
int main ()
{
    int n;
    while(~scanf("%d",&n))
    {
        int m=5,i=0,p,q,l=0;
        while(++i<=n)
        {
            p=i*m;
            while(p>n)
            p=p-n+(p-n-1)/(m-1);
            l++;
            if(p==1)
            printf("%d\n",l);
            q=p;
        }
    }
}

如果学习了c++,就可以轻松的完成这些题目,虽然考试不一定能用,但是提前学习有助于减轻大二数据结构课业负担。
比如说这几道题就可以用c++简单优美的书写:
1.建立顺序链表

#include<bits/stdc++.h>
using namespace std;

int main()
{
	list<int> l;//建立一个链表
	int n,x;
	cin>>n;
	while(n--)
	{
		cin>>x;
		l.emplace_back(x);//在链表的头节点后面依次添加数据
	}
	list<int>::iterator it=l.begin();//定义一个迭代器来遍历链表
	for(;it!=l.end();it++)
	{
		if(it!=l.begin()) cout<<" ";
		cout<<*it;
	}
}

2.链表的逆序(建立逆序链表,这俩一样)

#include<bits/stdc++.h>
using namespace std;

int main()
{
	list<int> l;
	int n,x;
	cin>>n;
	while(n--)
	{
		cin>>x;
		l.emplace_back(x);
	}
	l.reverse(); //可以看到除了这里其他的地方都跟正序没什么区别
	list<int>::iterator it=l.begin();
	for(;it!=l.end();it++)
	{
		if(it!=l.begin()) cout<<" ";
		cout<<*it;
	}
}

3.有序链表的归并

#include<bits/stdc++.h>
using namespace std;

int main()
{
	list<int> l;
	int n,m,x;
	list<int>::iterator it;
	cin>>n>>m;
	n+=m;
	while(n--)
	{
		cin>>x;
		l.push_front(x);
	}
	l.sort(); //排序就这样简单,一行完成
	for(it=l.begin();it!=l.end();it++)
	{
		if(it!=l.begin()) cout<<" ";
		cout<<*it;
	}
}

其它题目想研究的同学可以自己学习学习,研究研究。
不懂得可以私信我,随时解答。
程序有错误请私信我,以及时改正。感谢!

标签:node,head,struct,int,ACM,next,链表,SDUT,data
来源: https://blog.csdn.net/weixin_52604835/article/details/122813809