ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SDUT ACM OJ 实验二链表

2022-02-07 21:03:08  阅读:131  来源: 互联网

标签:node head struct int ACM next 链表 SDUT data


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有