【C站水库】带头结点的单链表的操作

题目:

代码:

#include<bits/stdc++.h>
#define ph print(head)
using namespace std;
struct node{
	int val;
	node* next;
};
node* create(){
	node *head,*tail,*temp;
	head=new node;
	head->next=NULL;
	tail=head;
	printf("请输入一串链表(以-1结尾):\n");
	while(1){
		temp=new node;
		int x; cin>>x;
		if(x==-1) break;
		temp->val=x;
		tail->next=temp;
		tail=temp;
	} 
	tail->next=NULL;
	return head;
}
void print(node* head){
	node* temp=head->next;
	printf("现在的链表是:\n");
	while(temp){
		printf("%d ",temp->val);
		temp=temp->next;
	}
	cout<<endl;
}
void deletefirst(node* head){
	printf("请输入你要删除的值(本次只删除第一个):\n");
	int x; cin>>x;
	node* pre=head;
	node* temp=head->next;
	while(temp){
		if(temp->val==x){
			pre->next=temp->next;
			return;
		}
		pre=temp;
		temp=temp->next;
	}
}
void reverse(node* head){
	printf("倒置中...\n"); 
	int num[100]={0},k=0;
	node *temp1=head->next,*temp2=head->next;
	while(temp1){
		num[k++]=temp1->val;
		temp1=temp1->next;
	}
	while(temp2){
		temp2->val=num[--k];
		temp2=temp2->next;
	}
}
void insert(node *head){
	printf("请输入你要插入的值:\n");
	int x; cin>>x;
	node *pre=head;
	node *temp=head->next;
	while(temp&&temp->val<=x){
		pre=temp;
		temp=temp->next;
	} 
	node *newnode=new node;
	newnode->val=x;
	if(temp==NULL){
		pre->next=newnode;
		pre=newnode;
		pre->next=NULL;
	}
	else{
		newnode->next=pre->next;
		pre->next=newnode;
	}
}
void deleteall(node* head){
	printf("请输入你要删除的值(本次删除所有该值):\n");
	int x; cin>>x;
	node *pre=head;
	node *temp=head->next;
	while(temp){
		if(temp->val==x){
			pre->next=temp->next;
			temp=temp->next;
		}
		else{
			pre=temp;
			temp=temp->next;
		}
	}
}
void mysort(node *head){
	printf("排序中...\n");
	node *pre,*ergodic,*temp;
	for(pre=head->next;pre->next!=NULL;pre=pre->next){
		for(ergodic=pre->next;ergodic!=NULL;ergodic=ergodic->next){
			if(ergodic->val<pre->val){
				int temp=ergodic->val;
				ergodic->val=pre->val;
				pre->val=temp;
			}
		}
	}
}
void merge(node* head1,node* head2){
	printf("合并前的两个单链表是:");
	node *tail=head1,*e1=head1->next,*e2=head2->next;
	while(e1&&e2){
		if(e1->val<e2->val){
			tail->next=e1;
			tail=e1;
			e1=e1->next;
		}
		else{
			tail->next=e2;
			tail=e2;
			e2=e2->next;
		}
	}
	tail->next=e1?e1:e2;
	printf("合并后:");
}
node* common(node* head1,node* head2)
{
  	node *head,*p,*q,*s,*pre;
    head=new node;
    head->next=NULL;
    pre=head;
    for(p=head1->next;p;p=p->next)
    {
        for(q=head2->next;q;q=q->next)
        {
            if(q->val==p->val)
            {
                s=new node;
                s->val=p->val;
                pre->next=s;
                pre=s;
                break;
            }
        }
    }
    pre->next=NULL;
    return head;
}

void partion(node* head)
{
    node *p,*s,*pre=head;
    p=head->next;
    while(p)
    {
        if(p->val%2==0)
        {
            pre=p;
            p=p->next;
        }
        else
        {
            s=p;
            pre->next=p->next;
            p=pre->next;
            s->next=head->next;
            head->next=s;
        }
    }
}
void search(node* head,int k)
{
    int cnt=0,i;
    node *p,*q;
    for(p=head->next;p;p=p->next) cnt++;
    p=head;
    for(i=1;i<=cnt-k+1;i++)
        p=p->next;
    printf("倒数第k个值是:%d\n",p->val);
    return;
}
int main(){
	node* head;
	head=create(); ph;
	deletefirst(head); ph;
	reverse(head); ph;
	deleteall(head); ph;
	mysort(head); ph;
	insert(head); ph;
	node* head2;
	head2=create();
	merge(head,head2); ph;
	node* newhead=common(head,head2); print(newhead);
	partion(head); ph;
	printf("请输入k:\n");
	int k; cin>>k;
	search(head,k);
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrl AC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值