C语言双向链表的内存释放,C语言拆分双向链表

#include

#include

#include

#include

struct node {

int data;

struct node *prev;

struct node *next;

};

struct node *list = NULL;

struct node *list_last = NULL;

struct node *even = NULL;

struct node *even_last = NULL;

struct node *odd = NULL;

struct node *odd_last = NULL;

struct node *current = NULL;

//Create Linked List

void insert(int data) {

// Allocate memory for new node;

struct node *link = (struct node*) malloc(sizeof(struct node));

link->data = data;

link->prev = NULL;

link->next = NULL;

// If head is empty, create new list

if(list == NULL) {

list = link;

return;

}

current = list;

// move to the end of the list

while(current->next!=NULL)

current = current->next;

// Insert link at the end of the list

current->next = link;

list_last = link;

link->prev = current;

}

//display the list

void print_backward(struct node *head) {

struct node *ptr = head;

printf("

[last] <=>");

//start from the beginning

while(ptr != NULL) {

printf(" %d <=>",ptr->data);

ptr = ptr->prev;

}

printf(" [head]\n");

}

//display the list

void printList(struct node *head) {

struct node *ptr = head;

printf("

[head] <=>");

//start from the beginning

while(ptr != NULL) {

printf(" %d <=>",ptr->data);

ptr = ptr->next;

}

printf(" [last]\n");

}

void split_list() {

// Allocate memory for new node;

struct node *listp;

struct node *link;

struct node *current;

listp = list;

while(listp->next != NULL) {

struct node *link = (struct node*) malloc(sizeof(struct node));

link->data = listp->data;

link->prev = NULL;

link->next = NULL;

if(listp->data%2 == 0) {

if(even == NULL) {

even = link;

even_last = link;

listp = listp->next;

continue;

} else {

current = even;

while(current->next != NULL) {

current = current->next;

}

// Insert link at the end of the list

current->next = link;

even_last = link;

link->prev = current;

listp = listp->next;

}

} else {

if(odd == NULL) {

odd = link;

odd_last = link;

listp = listp->next;

continue;

} else {

current = odd;

while(current->next!= NULL) {

current = current->next;

}

// Insert link at the end of the list

current->next = link;

odd_last = link;

link->prev = current;

listp = listp->next;

}

}

}

// Lets handle the last node

if(listp!=NULL) {

link = (struct node*) malloc(sizeof(struct node));

link->data = listp->data;

link->prev = NULL;

link->next = NULL;

if(listp->data%2 == 0) {

current = even;

while(current->next != NULL) {

current = current->next;

}

// Insert link at the end of the list

current->next = link;

even_last = link;

link->prev = current;

} else {

current = odd;

while(current->next!= NULL) {

current = current->next;

}

// Insert link at the end of the list

current->next = link;

odd_last = link;

link->prev = current;

}

}

}

int main() {

int i;

for(i = 1; i <= 10; i++)

insert(i);

printf("Complete List : ");

printList(list);

printf("List in reverse: ");

print_backward(list_last);

split_list();

printf("

After splitting list - \n");

printf("Even : ");

printList(even);

printf("Odd : ");

printList(odd);

printf("

Splitted lists in reverse - \n");

printf("Even : ");

print_backward(even_last);

printf("Odd : ");

print_backward(odd_last);

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值