1.1 单链表

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 带头节点的单向链表,使用头插法进行创建,并使用冒泡排序进行排序
// 单链表的头节点一般不存储元素。
typedef struct _Node{
    int data;
    struct _Node *next;
}Node;                                      // 书上的例子具有头指针,仅用来指向头节点,该程序直接使用头节点的地址。

// 创建头结点
// 本程序 head->next=NULL; 改成头结点head就是循环链表了
Node * createList(){
    Node * head=(Node *)malloc(sizeof(Node));
    head->next=NULL;
    return head;
}

// 1.头插法  时间复杂度为O(n)
void headInsert(Node *head,int data){
    Node *p1=(Node *)malloc(sizeof(Node));  // 传了个头进来,头的下一个给新节点,头指向新节点,依次循环。断开头身插入其中
    p1->data=data;
    p1->next=head->next;
    head->next=p1;
}

// 2.尾插法  时间复杂度同上
void rearInsert(Node *head,int data){       // 若用scanf函数,可以设置一个表尾指针,每次记录表尾的位置,下次插入,不用再遍历
    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);  //return...
            flag=0;
        }
    }
    if(flag) printf("Con not find data %d\n",find);
}

// 冒泡排序单链表
void BubbleSortCD(Node *head){             //exchange data
    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);
}
                               //    h    t
void reverseList(Node *head){  // #  *    *    *   *   *   *
    Node *h=head->next;
    head->next=NULL;           // 一分两半,将头置空
    Node *t;
    while(h){
        t=h->next;             // t在h后面
        h->next=head->next;    // 节点 3 -> 2,原来是 3->4 倒过来了
        head->next=h;          // 
        h=t;                   // 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;
}

 

posted @ 2019-05-03 20:02  aascn  阅读(142)  评论(0编辑  收藏  举报