数据结构||c语言链表实现 指针结构体练习

#include <stdio.h>
#include <stdlib.h>

typedef struct _int_list_node {
    struct _int_list_node * next;
    struct _int_list_node * prev;
    int value;
} * int_list_node;

typedef struct _int_list {
    int_list_node head;
    int_list_node tail;
    int length;
} * int_list;

int_list creat(void);
void push(int_list p, int value);
void print(int_list p);
void unshit(int_list p, int value);
int Linkedlist_eq(int_list p, int index);
void int_list_pop(int_list p);
void Linkdelist_shift(int_list p);
void Linkedlist_insert(int index, int_list p_old, int_list p_new);
void Linkedlist_del (int_list p, int start, int end);
void Linkedlist_free(int_list p);

int_list creat(void) {
    int_list p = (int_list) malloc(sizeof(struct _int_list_node));
    p->head = NULL;
    p->tail = NULL;
    p->length = 0;
    return p;
}

void push(int_list p, int value) {
    if(p->length) {
        p->tail->next = (int_list_node) malloc(sizeof(struct _int_list_node));
        p->tail->next->prev = p->tail;//新尾巴的pre是旧尾巴
        p->tail = p->tail->next;

    } else {
        p->tail = (int_list_node) malloc(sizeof(struct _int_list_node));
        p->head = p->tail;
    }
    p->tail->value = value;
    p->length++;
    p->tail->next = NULL;
}

void unshit(int_list p, int value) {
    if(p->length) {
        int_list_node temp = (int_list_node) malloc(sizeof(struct _int_list_node));
        temp->next = p->head;
        p->head->prev = temp;
        p->head = temp;
    } else {
        p->tail = (int_list_node) malloc(sizeof(struct _int_list_node));
        p->head = p->tail;
        p->tail->next = NULL;
    }
    p->head->value = value;
    p->length++;
}

void print(int_list p) {
    int_list_node current = p->head;
    while(current != NULL) {
        printf("%d ", current->value);
        current = current->next;
    }
    printf("\n");
}

int Linkedlist_eq(int_list p, int index) {
    int_list_node current = current = p->head;
    int i = 0;
    while(current != NULL) {
        if(i == index) {
            return current->value;
        }
        i++;
        current = current->next;
    }
    return -1;  
}

int_list_node Linkedlist_eq2(int_list p, int index) {
    int_list_node current = current = p->head;
    int i = 0;
    while(current != NULL) {
        if(i == index) {
            return current;
        }
        i++;
        current = current->next;
    }
    return NULL;  
}

void Linkdelist_shift(int_list p) {
    if(p->length > 1) {
        int_list_node temp = p->head;
        p->head = p->head->next;
        temp->next = NULL;
        free(temp);
    } else {
        free(p->head);
        p->head = NULL;
        p->tail = NULL;
    }
    p->length--;
}

void int_list_pop(int_list p) {
    if(p->length > 1) {
        int_list_node temp = p->tail;
        int_list_node pre = Linkedlist_eq2(p, p->length - 2);
        p->tail = pre;
        p->tail->next = NULL;
        free(temp);
    } else {
        free(p->head);
        p->head = NULL;
        p->tail = NULL;
    }
    p->length--;
}

//是原先的链表和之后的链表
//新链表加入旧链表
void Linkedlist_insert(int index, int_list p_old, int_list p_new) {
    int i = 0;
    int_list_node current = p_old->head;
    while (i != index) {
        i++;
        current = current->next;
    }
    if(i == 0) {
        p_new->tail->next = p_old->head;
        p_old->head = p_new->head;
    } else if(i == p_old->length - 1){
        p_old->tail->next = p_new->head;
        p_old->tail = p_new->tail;
    } else {
        p_new->tail->next = current->next;
        current->next = p_new->head;
    }
}

void Linkedlist_del (int_list p, int start, int end) {
    int i = 0;
    int_list_node pre_current = p->head;
    int_list_node next_current = p->head;
    int_list_node current = p->head;
    while(i != start){
        current = current->next;
        i++;
    }
    pre_current = current->prev;
    while(i != end){
        i++;
        current = current->next;
        printf("next is %d\n", current->value);
        free(current->prev);   
    }
    next_current = current;
    free(current);
    if(start == 0) {
        p->head = next_current;
    } else {
        pre_current->next = next_current;
    }
}
void Linkedlist_free(int_list p) {
    while (p->head != NULL) {
        int_list_node temp = p->head;
        free(p->head);
        p->head = temp->next;
    }
}
int main() {
    int_list p = creat();
    int_list p2 = creat();
    unshit(p, 1);
    push(p, 2);
    push(p,5);
    Linkedlist_del(p,1,2);
    print(p);
    Linkedlist_free(p);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值