#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct _Node{
int data;
struct _Node *next;
}Node;
Node * createList(){
Node * head=(Node *)malloc(sizeof(Node));
head->next=NULL;
return head;
}
void headInsert(Node *head,int data){
Node *p1=(Node *)malloc(sizeof(Node));
p1->data=data;
p1->next=head->next;
head->next=p1;
}
void rearInsert(Node *head,int data){
Node *p1,*p2=head;
while(p2->next!=NULL)
p2=p2->next;
p1=(Node *)malloc(sizeof(Node));
p1->next=NULL;
p1->data=data;
p2->next=p1;
}
void traverseList(Node *head){
Node * p=head->next;
printf("Traverse:");
while(p){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void searchList(Node *head,int find){
int flag=1;
for(Node *p=head->next;p!=NULL;p=p->next){
if(p->data==find){
printf("Find data %d\n",find);
flag=0;
}
}
if(flag) printf("Con not find data %d\n",find);
}
void BubbleSortCD(Node *head){
int flag;
Node *p1,*p2;
p1=head->next;
for(p1=p1->next;p1!=NULL;){
flag=1;
for(p2=head->next;p2!=p1;p2=p2->next){
if(p2->data > p2->next->data){
p2->data^=p2->next->data;
p2->next->data^=p2->data;
p2->data^=p2->next->data;
flag=0;
}
}
if(flag)p1=p1->next;
}
}
Node * createList(int n){
int i;
Node* head=(Node *)malloc(sizeof(Node));
head->next=NULL;
Node *p1,*p2=head;
srand(time(NULL));
for(i=0;i<n;i++){
p1=(Node *)malloc(sizeof(Node));
p1->next=NULL;
p1->data=rand()%100;
p2->next=p1;
p2=p2->next;
}
return head;
}
void DeleteNode(Node *head,int data){
Node *p1,*p2;
p1=head->next;
while(p1->data!=data){
p2=p1;
p1=p1->next;
}
p2->next=p1->next;
free(p1);
}
void reverseList(Node *head){
Node *h=head->next;
head->next=NULL;
Node *t;
while(h){
t=h->next;
h->next=head->next;
head->next=h;
h=t;
}
}
int main(){
Node *head=createList();
headInsert(head,1);
headInsert(head,3);
headInsert(head,5);
headInsert(head,7);
headInsert(head,9);
headInsert(head,2);
headInsert(head,4);
headInsert(head,6);
headInsert(head,8);
headInsert(head,10);
rearInsert(head,99);
searchList(head,8);
searchList(head,16);
BubbleSortCD(head);
DeleteNode(head,4);
traverseList(head);
reverseList(head);
traverseList(head);
return 0;
}