//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;
}