fundamental operations to a list.

本文探讨了两种删除链表中指定值的方法:一种是通过找到第一个不等于目标值的节点替换头部,另一种是在头部插入一个特殊节点。作者提供了创建链表、显示链表、删除节点的实现,并展示了如何使用两种方法进行操作和维护链表顺序。
摘要由CSDN通过智能技术生成
//the first way-----find the first entry whose num is not key,and make it to be head

#include<stdio.h>
#include<stdlib.h>
struct link{
	int num;
	struct link*next;
};
struct link* creat(int n);
void show(struct link*);
struct link* del(struct link* head,int key);
int main(void){
	int n;
	struct link*head;
	scanf("%d",&n);
	head=creat(n);
	show(head);
	head=del(head,1);
	show(head);
}
struct link* creat(int n){
	struct link* p,*p0,*head;
	int i;
	for(i=0;i<n;i++){
		p=(struct link*)malloc(sizeof(struct link));
		scanf("%d",&p->num);
		p->next=NULL;
		if(i==0){
			head=p;
			p0=p;
		}
		else{
			p0->next=p;
			p0=p;
		}
	}
	return head;
}
void show(struct link* p){
	while(p!=NULL){
		printf("%5d",p->num);
		p=p->next;
	}
	printf("\n");
}
struct link* del(struct link* head,int key){
	while(head->num==key&&head!=NULL){
		head=head->next;
	}
	struct link*p,*p0,*t;
	p=head;p0=NULL;
	while(p!=NULL){
		if(p->num!=key){
			p0=p;
			p=p->next;
		}
		else{
			t=p;
			p=p->next;
			p0->next=p;
            free(t);
		}
	}
	return head;
	}
//second way----set a guard entry to be head

struct link* del(struct link* head,int key){
	struct link* guard,*t;
	guard=(struct link*)malloc(sizeof(struct link));
	guard->next=head;
	head=guard;
	struct link*p=head->next,*p0=head;
	while(p!=NULL){
		if(p->num!=key){
			p0=p;
			p=p->next;
		}
		else{
			t=p;
			p=p->next;
			p0->next=p;
			free(t);
		}
	}
	head=guard->next;
	free(guard);
	return head;
	}
//other operations
#include<stdio.h>
#include<stdlib.h>
struct link{
	int num;
	struct link*next;
};
struct link* creat(int n);
void show(struct link*);
struct link* del(struct link* head,int key);
void des(struct link* head);
struct link* reorder(struct link* head);
struct link* sort(struct link* head);
int main(void){
	int n;
	struct link*head;
	scanf("%d",&n);
	head=creat(n);
	show(head);
	head=sort(head);
	show(head);
//	head=reorder(head);
//	show(head);
//	head=del(head,1);
//	show(head);
	des(head);
	
}
struct link* creat(int n){
	struct link* p,*p0,*head;
	int i;
	for(i=0;i<n;i++){
		p=(struct link*)malloc(sizeof(struct link));
		scanf("%d",&p->num);
		p->next=NULL;
		if(i==0){
			head=p;
			p0=p;
		}
		else{
			p0->next=p;
			p0=p;
		}
	}
	return head;
}
void show(struct link* p){
	while(p!=NULL){
		printf("%5d",p->num);
		p=p->next;
	}
	printf("\n");
}
struct link* del(struct link* head,int key){
	while(head->num==key&&head!=NULL){
		head=head->next;
	}
	struct link*p,*p0,*t;
	p=head;p0=NULL;
	while(p!=NULL){
		if(p->num!=key){
			p0=p;
			p=p->next;
		}
		else{
			t=p;
			p=p->next;
			p0->next=p;
            free(t);
		}
	}
	return head;
	}
void des(struct link* head){
	struct link* t;
	while(head!=NULL){
		t=head;
		head=head->next;
		free(t);
	}
}
struct link* reorder(struct link* head){
	if(head==NULL||head->next==NULL) return head;
	struct link* p,*p0;
	p=head->next;
	head->next=NULL;
	do{
		p0=head;
		head=p;
		p=head->next;
		head->next=p0;
	}while(p!=NULL);
	return head;
}
struct link* sort(struct link* head){
	struct link *p,*p0,*guard,*r,*r0,*q;
	guard=(struct link*)malloc(sizeof(struct link));
	guard->next=head;
	head=guard;
	p0=guard;
	p=guard->next;
	while(p!=NULL){
		r0=guard;
		r=guard->next;
		while(r->num<p->num&&r!=p){
			r0=r;
			r=r->next;
		}
		if(r==p){
			p0=p;
			p=p->next;
		}
		else{
			q=p;
			p=p->next;
			p0->next=p;
			q->next=r;
			r0->next=q;
		}
	}
	head=guard->next;
	free(guard);
	return head;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值