链表输出、插入(头、中、尾)、删除(头、中、尾)、转置、输出奇/偶数节点、相邻两节点互换、打印约瑟夫环

#include <stdio.h>
#include <stdlib.h>
typedef struct stu
{
    int num;
    struct stu *next;
}stu;
int output(stu*);
int insert_head(stu*);
int insert_mid(stu*);
int insert_tail(stu*);

int delete_head(stu*);
int delete_mid(stu*);
int delete_tail(stu*);

int reverse(stu*); 
int jishu(stu*);
int swap(stu*);
int select_sort(stu*);    //不是temp冒泡。。。链表的选择交换
//int output_min_mid_max(stu*);
void yuesefu();

int main()
{
    int n;
    struct stu *head,*p,*q;
    head=(struct stu*)malloc(sizeof(struct stu));
    head->next=NULL;
    q=head;
    printf("请输入数字,以零结束:\n");
 	scanf("%d ",&n);
   	while(n!=0)
    {
        p=(struct stu*)malloc(sizeof(struct stu));
        p->num=n;
        p->next=NULL;
        q->next=p;
        q=p;
		scanf("%d",&n);
    }
    p=head->next;
	insert_head(head);
	output(head);
	
    insert_mid(head);
 	output(head);
 	
	insert_tail(head);
	output(head);
	
	delete_head(head);
	output(head);
	
  	delete_mid(head);
	output(head);
	
    delete_tail(head);
 	output(head);
 	
	reverse(head); 
	output(head);
	
	jishu(head);
		
	printf("交换前的链表为:");
	output(head);
	swap(head);
	output(head);
	
	select_sort(head);
//	output_min_mid_max(head);
	yuesefu();
return 0;
}
int output(stu*head)
{ 	stu* p; 
 	p = head->next;  
	while(p!= NULL)
	{ 	
		printf("%d ", p->num); 
		p = p->next; 
	}	
	printf("\n");

 }
int insert_head(stu*head)
{	
	int a;
	stu * p,*q;
	p=(stu *)malloc(sizeof(stu));
	printf("请输入你想从头部插入的数:\n");
	scanf("%d",&a);
	p->num = a;
	p->next = head->next;
	head->next = p;
	printf("插入节点后的链表为:\n");	
}
	
int insert_mid(stu*head)
{	
	int n;
	printf("请输入你想插入节点的位置:\n");
	stu * p;
	scanf("%d",&n);
	p= (stu*)malloc(sizeof(stu));
	for (int i=0;i<n-1;i++)
		head = head->next;
	printf("请输入节点的值:\n");
	scanf("%d",&p->num);
	p->next = head->next;
	head->next=p;
	printf("插入节点后的链表为:\n");
return 0;
}
int insert_tail(stu*head)
{
	int n;
	while(head->next!=NULL) 
		head=head->next; 
	stu *p;
	p=(stu *)malloc(sizeof(stu)); 
	printf("请输入你想从尾部插入的数:\n");
	scanf("%d",&p->num); 	
	p->next=NULL; 
	head->next=p;
	printf("插入尾部后的链表为:\n");
}

int delete_head(stu*head)
{
	stu *p;
	p = (stu*)malloc(sizeof(stu));
	p = head->next;
	head->next = p->next;
	free(p);
	printf("删除头部后的链表为:\n");
return 0;
}
int delete_mid(stu*head)
{
	stu *p,*q;
	int n;
	printf("请输入你想删除的节的位置:\n");
	scanf("%d",&n);
	for (int i=1;i<n;i++)
		head = head->next;
	p = head->next;
	q = p->next;
	head->next = q;
	free(p);
	printf("删除此节点后的链表为:\n");
return 0;
}
int delete_tail(stu*head)
{
	stu *p;
	p = (stu*)malloc(sizeof(stu));
	while(head->next->next!=NULL)
		head = head->next;
	p = head->next;
	head->next=NULL;
	free(p);
	printf("删除尾部后的链表为:\n");
}
int reverse(stu*head)
{
	if(head==NULL)
 	{
		printf("error!\n");
	}
	stu *pre,*curr,*ne;
	pre=head->next;
	curr=pre->next;
	pre->next=NULL;
	while(curr)
 	{
		ne=curr->next;
		curr->next=pre;
		pre=curr;
		curr=ne;
	}
	head->next=pre;
	printf("转置后的链表为:\n");
}
int jishu(stu*head)
{
	int length=0,i;
	stu*p;
	p=head->next;
	while(p->next!=NULL) 
	{
		length++;
		p=p->next;
	}
	length++;
	printf("链表的长度为:%d\n",length);
	p=head->next;
	if(length%2!=0)
	{
		printf("奇数号节点的数据为:");
		printf("%d ",p->num);
		for(i=0;i<(length-1)/2;i++)
		{	
			p=p->next->next;
			printf("%d ",p->num);
		}
		printf("\n");
	} 
	else
	{	
		printf("奇数号节点的数据为:");
		for(i=0;i<=(length-1)/2;i++)
		{
			printf("%d ",p->num);
			p=p->next->next;
		}
		printf("\n");
	}
}
int swap(stu*head)
{
	int temp;
	int length=0,i;
	stu*p,*q;
	p=head->next;
	while(p->next!=NULL) 
	{
		length++;
		p=p->next;
	}
	length++;
	p=head->next;
	q=p->next;
	if(length%2!=0)
	{
		for(i=0;i<(length-1)/2;i++)
		{
		temp=p->num;
		p->num=q->num;
		q->num=temp;
    
		p=q->next;
    	q=q->next->next;
		}
	}
	else
	{
		for(i=0;i<length/2-1;i++)
		{
		temp=p->num;
		p->num=q->num;
		q->num=temp;
    
		p=q->next;
    	q=q->next->next;
		}
		temp=p->num;
		p->num=q->num;
		q->num=temp;
	}	
	printf("奇数号节点和偶数号节点互换后为:\n");
}
int select_sort(stu *head)  
{  
	stu *p,*q,*m,*n;
	stu *temp1,*temp2;
	if(head->next==NULL)
		printf("NO LINKLIST!!!");
	else
	{
		p=head;q=head->next;
		while(q->next!=NULL)
		{
			m=p->next;
			n=q->next;
			temp1=m;
			/*序列的最小的节点*/
			while(temp1->next!=NULL)
			{
				if(temp1->next->num<q->num && temp1->next->num<n->num)
				{
					m=temp1;n=temp1->next;
				}
				temp1=temp1->next;
			}
			/*交换两个节点*/
			if(m!=p->next || (m==p->next && m->num > n->num))
			{
				p->next=n;
				p=n;
				m->next=q;
				m=q;
				q=q->next;
				n=n->next;
				p->next=q;
				m->next=n;
			}
			/*没有找到最小值时的p,q后移操作*/
			else
			{
				p=p->next;
				q=q->next;
			}
		}
		temp2=head->next;
	} 
	printf("节点排序后为:\n");
	output(head);
}
//int output_min_mid_max(stu*head)
//{
//	stu*p;
//	int temp;
//	int length=0,i,min,max;
//	float mid;
//	p=head->next;
//	min=p->num;
//	printf("Here1\n");
//	while(p->next!=NULL) 
//	{
//		length++;
//		p=p->next;
//	}
//	length++;
//	printf("链表的长度为:%d\n",length);
//	if(length%2!=0)
//	{	printf("Here2\n");
//		for(i=0;i<(length+1)/2-1;i++)
//			p=p->next;
//		mid=p->num;
//		for(i=0;i<length;i++)
//			p=p->next;
//		max=p->num;
//		printf("最小值为%d,中间值为%f,最大值为%d",min,mid,max); 
//	} 
//	else
//	{	printf("Here3\n");
//		for(i=0;i<length-1;i++)
//			p=p->next;
//		mid=(p->num+p->next->num)*1.0/2;
//			printf("Here4\n");
//		for(i=0;i<length;i++)
//			p=p->next;
//		max=p->num;
//		printf("最小值为%d,中间值为%f,最大值为%d",min,mid,max); 
//	} 
//		printf("Here5\n");
//}
void yuesefu()
{
	struct node
	{
		int no;
		struct node*next;
	};
	int i,k;
	struct node*head,*p,*q;
	head=(struct node*)malloc(sizeof(struct node));
	head->no=-1;
	head->next=head;
	for(i=30;i>0;i--)
	{
		p=(struct node*)malloc(sizeof(struct node));
		p->next=head->next;
		p->no=i;
		head->next=p;
	}
	printf("约瑟夫环:\n");
	while(p->next!=head)
		p=p->next;
	p->next=head->next;
	printf("扔到海里的人序号依次为:\n");
	for(i=0;i<15;i++)
	{
		for(k=1;k<9;k++)
			p=p->next;
		q=p->next;
		p->next=q->next;
		printf("%d\n",q->no);
		free(q);	
	}	
	
}

猜你喜欢

转载自blog.csdn.net/dovis233/article/details/79629374
今日推荐