SDUT程序二链表题目

  • 数据结构实验之链表一:顺序建立链表
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 

struct node 
{
	int mode;
	struct node* pnext;
 };
 
 int main()
{
    int n,i;
    struct node*  head;
    struct node* tail;
    struct node* p;
	scanf("%d",&n);
	head=(struct node*)malloc(sizeof(struct node));
	head->pnext=NULL;
	tail=head;
	for(i=1;i<=n;i++)
	{
		p=(struct node*)malloc(sizeof(struct node));
		scanf("%d",&p->mode);
		p->pnext=NULL;
	    tail->pnext=p;
	    tail=p;
	 } 
	 p=head->pnext;
	 while(p!=NULL)
	 {
	 	printf("%d",p->mode);
	 	if(p->pnext!=NULL)
	 	{
	 		printf(" ");
		 }
		 else 
		 {
		 	printf("\n");
		 }
		 p=p->pnext;
	 }
	 return 0;
} 
  • 数据结构实验之链表二:逆序建立链表
#include <stdio.h>
#include <stdlib.h>

struct node 
{
	int mode;
	struct node* pnext;
}*head,*p;

int main()
{
	int n,i;
	scanf("%d",&n);
	p=(struct node*)malloc(sizeof(struct node));
	p->pnext=NULL;
	for(i=0;i<n;i++)
	{
		head=(struct node*)malloc(sizeof(struct node));
	     scanf("%d",&p->mode);
	     head->pnext=p;
	     p=head;
	}
	while(p->pnext!=NULL)
	{
		printf("%d",p->pnext->mode);
		if(p->pnext->pnext!=NULL)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
		p=p->pnext;
	}
	return 0; 
}
  • 师–链表的结点插入
#include <iostream>

using namespace std;

struct node {
	int data;
	node *pnext;
};

void Insert(node* head, int m,int data1)
{
	node *p=head;
     while(m--&&p->pnext!=NULL)
     {
     	p=p->pnext;
	 }
	 node *ptr=new node;
	 ptr->data=data1;
	 ptr->pnext=p->pnext;
	 p->pnext=ptr;
}

void show(node *head)
{
	node *pe=head->pnext;
	while(pe!=NULL)
	{
		printf("%d ",pe->data);
		pe=pe->pnext;
	}
}

int main()
{
	int m,x;
	int n; 
	while(cin>>n){
		node *head=new node;
		head->pnext=NULL;
	    for(int i=1;i<=n;i++){
	    	scanf("%d%d",&m,&x);
	    	Insert(head,m,x);
		}show(head);
	}
	
	return 0;
}
  • 数据结构实验之链表七:单链表中重复元素的删除
#include <iostream>

using namespace std;

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


node *create(int n){
	node *head=new node;
	head->next=NULL;
	node* tail;
	tail=head;
	for(int i=1;i<=n;i++){
		node* p=new node;
		scanf("%d",&p->data);
		p->next=tail->next;
		tail->next=p;       //尾插法建立链表 
	}
	return head; 
}

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

node * cut(node *head, int n){
	node *front;   //第一个指针用于外循环 
	node *back;    //第二个指针用于内循环 
	node *preback; //用作内循环指针的前一个元素的指针 便于元素的删除 
	front=head;
	back=front->next;
	preback=front;
	int flag;  //标记是否删除元素 
	while(front->next!=NULL)
	{
		
		while(back!=NULL)
		{flag=0; 
			if(back->data==front->data)
			{
				preback->next=back->next; //元素的删除 
				n--;    //元素总数-1 
				flag=1;
			}
			if(flag){
				back=back->next;
			}
			else{
			back=back->next;  
			preback=preback->next; 
			} //内循环元素向后推一个 
		}
		if(front->next==NULL) break;
		else{
		front=front->next;
		back=front->next;  //让内循环指针回归外循环指针的next 
		preback=front;
	}
	}
	cout<<n<<endl;
	return head;
}
int main()
{
	int n;
	cin>>n;
	node* head = create(n);
	printf("%d\n",n);
	show(head);
	node *head1=cut(head,n); 
	show(head1);
	return 0;
}
  • 数据结构实验之链表三:链表的逆置
#include <iostream>

using namespace std;

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

node * create()
{
	int n;
	node *head,*tail,*p;
	head=new node;
	head->next=NULL;
	tail = head;
	while(~scanf("%d",&n)){
		if(n==-1) break;
		p= new node;
		p->data=n;
		p->next=tail->next;
		tail->next=p;
	}
	return head;
}

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

int main(){
	node *head= new node;
	head=create();
	show(head);
	return 0;
    
} 
  • 数据结构实验之链表四:有序链表的归并
#include <iostream>

using namespace std;

struct node {
	int data;
    node *next;
};
node *create(int n)
{
	int x;
	node *head=new node;
	node *tail;
	head->next=NULL;
	tail=head;
	for(int i=0;i<n;i++){
		cin>>x;
		node *p=new node;
		p->data=x;
		p->next=tail->next;
		tail->next=p;
		tail=p;
	}
	return head;
}

node *together_sort(node* head_1,node* head_2)
{
	node *tail;
	node * p=head_1->next;
	node * q=head_2->next; 
	tail=head_1;
	while(p!=NULL&&q!=NULL)
	{
		if(p->data>=q->data)
		{
			tail->next=q;
			tail=q;
			q=q->next;
		}
	    else if(p->data<q->data)
		{
			tail->next=p;
			tail=p;
			p=p->next;
		}
		if(q) tail->next=q;
		else tail->next=p;
	}
	return head_1;
	
}

void show(node *head)
{
	node *p=head->next;
	while(p!=NULL)
	{
		if(p->next==NULL)
		{
			printf("%d\n",p->data);
			p=p->next;
		}
		else{
			printf("%d ",p->data);
			p=p->next;
		}
	 } 
}
	
int main()
{
	int n,m;
	cin>>n>>m;
	node *head_1=create(n);
	node *head_2=create(m);
	head_1=together_sort(head_1,head_2);
	show(head_1);
	return 0;
}
  • 数据结构实验之链表五:单链表的拆分
#include <iostream>

using namespace std;

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

node *create(int n){
	node *head=new node;
	node *tail;
	head->next=NULL;
	tail=head;
	for(int i=0;i<n;i++)
	{
		node *p=new node;
		scanf("%d",&p->data);
		p->next=tail->next;
		tail->next=p;
		tail=p;
	}
	return head;
} 
node * dou(node * head1)
{
	int n=0;
	node* head=new node;
	node* tail;
	head->next=NULL;
	tail=head;
	node* p=head1->next;
	while(p!=NULL)
	{
		if(p->data%2==0)
		{
			node *q=new node;
			q->data=p->data;
			q->next=tail->next;
			tail->next=q;
			tail=q;
			n++;
		}
		p=p->next;
	}
	cout<<n<<' ';
	return head;
}

node * you(node * head1)
{
	int n=0; 
	node* head=new node;
	node* tail;
	head->next=NULL;
	tail=head;
	node* p=head1->next;
	while(p!=NULL)
	{
		if(p->data%2!=0)
		{
			node *q=new node;
			q->data=p->data;
			q->next=tail->next;
			tail->next=q;
			tail=q;
			n++;
		}
		p=p->next;
	}
	cout<<n<<endl;
	return head;
}

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

int main()
{
	int n;
	cin>>n;
	node * head=create(n);
	node * head1,*head2;
	head1=dou(head);
	head2=you(head);
	show(head1);
	show(head2);
	return 0;
}
  • 数据结构实验之链表九:双向链表
#include <iostream>

using namespace std;

struct node{
	int data;
	node* front,*back;
};

node *creat(int n)
{
	node* head=new node;
	node * tail;
	head->front=NULL;
	head->back=NULL;
	tail=head;
	for(int i=0;i<n;i++){
		node * p=new node;
		scanf("%d",&p->data);
		p->back=NULL;
		tail->back=p;
		p->front=tail;
		tail=p;
	}
	return head;
}

void search(int x,node *head){
	 node *p=head->back;
        while(p->data!=x)
            p=p->back;
        if(p->front==head)
            printf("%d\n",p->back->data);
        else if(p->back==NULL)
            printf("%d\n",p->front->data);
        else
            printf("%d %d\n",p->front->data,p->back->data);
}

int main(){
	int n,m;
	int x;
	cin>>n>>m;
	node* head=creat(n);
	for(int i=0;i<m;i++)
	{
	cin>>x;
		 search(x,head);
		}
	return 0;
}
  • 约瑟夫问题
#include <iostream>
#include <malloc.h>

using namespace std;

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

node * create(int n)
{
	node *head=new node ;
	node *tail;
	head->data=1;
	head->next=NULL;
	tail=head;
	for(int i=2;i<=n;i++)
	{
		node *p=new node;
		p->data=i;
		p->next=NULL;
		tail->next=p;
		tail=p;
	 } 
	 tail->next=head;
	 return head;
 } 
 
int main()
{
	int n,m;
	cin>>n>>m;
	node *head=create(n);
	node *q,*p;
	q=head;
	while(q->next!=head)
	q=q->next;
	int con=0;
	while(q->next!=q)
	{
		p=q->next;
		con++;
		if(con==m)
		{
			q->next=p->next;
			free(p);
			con=0;
		}
		else q=p;
		
	}
	printf("%d",q->data);
	return 0;
}
  • 不敢死队问题
#include <iostream>
#include <malloc.h>
using namespace std;

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

node * create(int n)
{
	node *head=new node;
	node *tail;
	head->next=NULL;
	head->data=1;
	tail=head;
	for(int i=2;i<=n;i++)
	{
		node *p=new node;
		p->data=i;
		p->next=NULL;
		tail->next=p;
		tail=p;
	}
	tail->next=head;
	return head;
}

int main()
{
	int n;

	while(~scanf("%d",&n)&&n){
		node *head;
	    node *p,*q;
	    head=create(n);
		q=head;
		while(q->next!=head)
		q=q->next;
		int con=0,ans=0;
		while(q->next!=q)
		{
			p=q->next;
			con++;
			if(con==5){
				if(p->data==1)
				break;
				q->next=p->next;
				free(p);
				con=0;
				ans++;
			}
			else q=p;
		}
		printf("%d\n",ans+1);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值