#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;
}
数据结构||c语言链表实现 指针结构体练习
最新推荐文章于 2024-11-11 09:41:37 发布