單項鍊表

C語言實現鏈表的創建,鏈表的遍歷,鏈表的節點刪除,鏈表的節點插入。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct User{
    char name[20];
    int age;
    int id;
    struct User *next;
};
int n = 0;
//指針
void logIntA(int *);
void exchangeA(int *,int *);
void exchangeB(int *,int *);
//函數指針
int min(int , int);
int max(int , int);
//指針函數
int *fun(int a , int b);
//單向鏈表
struct User * CreateSinglyLinkedList();
void logLinkedList(struct User *head);
void deleteNode(struct User *head, int id);
struct User *insertNode(struct User *head , int id , struct User *node);
struct User* flipLikedList(struct User *head);
int main(int argc, const char * argv[]) {
//    int a = 11;
//    int b = 22;
//    printf("a:%p --- b:%p\n",&a ,&b);
//    exchange(&a, &b);
//    printf("a:%d \t b:%d\n",a,b);
//    printf("a:%p --- b:%p\n",&a ,&b);
//    int *p = NULL;
//    int (*f)(int a,int b);//函數指針
//    f = min;
//    *p = (*f)(a , b);
//    printf("p:%p \t *p:%d\n",p,*p);
//    f=max;
//    *p = (*f)(a , b);
//    printf("p:%p \t *p:%d\n",p,*p);
//    p = fun(a, b);
//    printf("p:%p \t *p:%d\n",p,*p);

    int id = -1;
    struct User *head = CreateSinglyLinkedList();
    printf("*************\n");
    logLinkedList(head);
//    printf("delete User id:\n");
//    scanf("%d",&id);
//    deleteNode(head, id);
//    logLinkedList(head);
//    while (1) {
//        struct User *node = malloc(sizeof(struct User));
//        node->id = ++n;
//        printf("please input name age:\n");
//        scanf("%s %d",node->name , &node->age);
//        printf("please input insertNode where id:\n");
//        scanf("%d",&id);
//        head = insertNode(head, id, node);
//        logLinkedList(head);
//    }
    head = flipLikedList(head);
    printf("*************\n");
    logLinkedList(head);
    return 0;
}
//指針
void exchangeA(int *p1,int *p2){
    int a = *p1;
    *p1 = *p2;
    *p2 = a;
}
void exchangeB(int *p1,int *p2){
    int *p = p1;
    printf("p1:%p --- p2:%p\n",p1 ,p2);
    p1 = p2;
    p2 = p;
    printf("p1:%p --- p2:%p\n",p1 ,p2);
}
void logIntA(int *p){
    printf("p:%p --- *p:%d\n",p , *p);
    *p = *p + 10;
}
// 指針函數
int *fun(int a , int b){
    int *p = (int *)malloc(sizeof(int));
    *p = a + b;
    return p;
}
int min(int a,int b){
    return a<b ? a : b;
}
int max(int a, int b){
    return a>b ? a : b;
}

/**
 創建鏈表

 @return 鏈表
 */
struct User * CreateSinglyLinkedList(){
    struct User *newP;
    struct User *lastP = NULL;
    struct User *head = NULL;
    //
    newP = malloc(sizeof(struct User));
    if (newP == NULL) {
        printf("create error");
        return NULL;
    }else{
        printf("please input name age:\n");
        scanf("%s %d",newP->name , &newP->age);
    }

    while (newP->age != 0) {
        n++;
        if (head == NULL) {
            head = newP;
        }else{
            lastP->next = newP;
        }
        newP->id = n;
        lastP = newP;
        newP = malloc(sizeof(struct User));
        printf("please input name age:\n");
        scanf("%s %d",newP->name , &newP->age);
    }
    free(newP);
    if (head != NULL) {
        lastP->next = NULL;
    }
    newP = NULL;
    return head;
}

/**
 打印鏈表

 @param head 鏈表
 */
void logLinkedList(struct User *head){
    struct User *p;
    p = head;
    while (p != NULL) {
        printf("p:%p \t name:%s \t age:%d \t id:%d\n",p,p->name,p->age,p->id);
        p = p->next;
    }
}

/**
 刪除節點

 @param head 鏈表
 @param id 節點值
 */
void deleteNode(struct User *head, int id){
    if (head == NULL) {
        printf("not find");
        return;
    }
    struct User *p = head;
    struct User *lastP = NULL;
    while (p->id != id && p->next != NULL) {
        lastP = p;
        p = p->next;
    }
    if (p->next == NULL) {
        printf("not find\n");
        return;
    }else{
        if (p == head) {
            *head = *p->next;
        }else{
            lastP->next = p->next;
        }
    }
    free(p);
    p = NULL;
}

/**
 插入節點

 @param head 鏈表
 @param id 節點值 位置
 @param node 節點
 @return 新鏈表
 */
struct User *insertNode(struct User *head , int id , struct User *node){
    struct User *p = head;
    if (head == NULL) {
        head = node;
        node->next = NULL;
        return head;
    }
    while (p->id != id && p->next != NULL) {
        p = p->next;
    }
    if (p->id != id) {
        printf("not find\n");
        return head;
    }else{
        //不可顛倒
        node->next = p->next;//插入節點的next指向p下一個節點,也就是p後面的node
        p->next = node;//pnext指向node
    }
    return head;
}

/**
 翻轉鏈表

 @param head 鏈表
 @return 翻轉後的鏈表
 */
struct User* flipLikedList(struct User *head){
    if (head == NULL) {
        return head;
    }
    struct User *cur_node;
    struct User *new_node;
    struct User *tmp_node;
    new_node = NULL;
    cur_node = head;
    while (cur_node != NULL) {
        tmp_node = cur_node->next;//記錄下一個node的指針
        cur_node->next = new_node;//把當前節點的next指向新的節點
        new_node = cur_node;//把當前節點賦值給新節點
        cur_node = tmp_node;//當前節點移動到下一個節點
    }
    head = new_node;
    return head;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章