C 양 방향 링크 조작
7870 단어 datastruct
/*
*/
#ifndef __LINK_0505
#define __LINK_0505
typedef struct node{
int num;
struct node* p_next;
struct node *p_pre;
}node;
typedef struct
{
node head,tail;
struct node *p_cur;
}link;
//
void link_init(link *);
//
void link_deinit(link *);
//
int link_empty(link *);
//
int link_full(link *);
//
int link_size(link *);
//
int link_add_head(link *, int );
//
void append(link *, int );
//
int link_insert(link *, int);
//
int link_remove_head(link *);
//
int link_remove_tail(link *);
//
int link_remove(link *, int );
//
int link_get_head(link *, int *);
//
int link_get_tail(link *, int *);
//
int link_get(link *, int *, int );
//
void link_begin(link *);
//
int link_next(link *, int *);
//
void link_rbegin(link *);
//
int link_prev(link *, int *);
#endif
link_0505.cpp
/*
*/
#include "stdlib.h"
#include "link_0505.h"
//
void link_init(link *p_link)
{
p_link->head.p_next = &(p_link->tail);
p_link->tail.p_next = NULL;
p_link->tail.p_pre = &(p_link->head);
p_link->head.p_pre = NULL;
p_link->p_cur = NULL;
}
//
void link_deinit(link *p_link)
{
while(p_link->head.p_next != &(p_link->tail))
{
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
p_last->p_pre = p_first;
free(p_mid);
p_mid = NULL;
}
p_link->p_cur = NULL;
}
//
int link_empty(link *p_link)
{
return p_link->head.p_next == &(p_link->tail);
}
//
int link_full(link *p_link)
{
return 0;
}
//
int link_size(link *p_link)
{
int cnt = 0;
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail))
{
cnt++;
}
}
return cnt;
}
//
int link_add_head(link *p_link, int num)
{
node *p_temp = (node *)malloc(sizeof(node));
if (!p_temp)
{
return 0;
}
p_temp->num = num;
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_temp;
p_temp->p_next = p_mid;
p_mid->p_pre = p_temp;
p_temp->p_pre = p_first;
return 1;
}
//
int append(link *p_link, int num)
{
node *p_first = NULL,*p_mid = NULL,*p_last = NULL;
node *p_tmp = (node *)malloc(sizeof(node));
if (!p_tmp)
{
return 0;
}
p_tmp->num = num;
p_tmp->p_pre = NULL;
p_tmp->p_next = NULL;
p_first = p_link->tail.p_pre;
p_mid = p_first->p_next;
p_last = p_mid->p_next;
p_first->p_next = p_tmp;
p_tmp->p_next = p_mid;
p_mid->p_pre = p_tmp;
p_tmp->p_pre = p_first;
return 1;
}
//
int link_insert(link *p_link, int num)
{
node* p_temp = (node *)malloc(sizeof(node));
node* p_node = NULL;
if (!p_temp)
{
return 0;
}
p_temp->num = num;
p_temp->p_next = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid == &(p_link->tail) || p_mid->num > p_temp->num)
{
p_first->p_next = p_temp;
p_temp->p_next = p_mid;
p_mid->p_pre = p_temp;
p_temp->p_pre = p_first;
break;
}
}
return 0;
}
//
int link_remove_head(link *p_link)
{
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
p_first->p_next = p_last;
p_last->p_pre = p_first;
free(p_mid);
p_mid = NULL;
return 0;
}
//
int link_remove_tail(link *p_link)
{
node *p_first = NULL,*p_mid = NULL,*p_last = NULL;
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
p_last = &(p_link->tail);
p_mid = p_last->p_pre;
p_first = p_mid->p_pre;
p_first->p_next = p_last;
p_last->p_pre = p_first;
free(p_mid);
p_mid = NULL;
return 0;
}
//
int link_remove(link *p_link, int num)
{
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && p_mid->num == num)
{
p_first->p_next = p_last;
p_last->p_pre = p_first;
free(p_mid);
p_mid = NULL;
return 1;
}
}
return 0;
}
//
int link_get_head(link *p_link, int *p_num)
{
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
*p_num = p_mid->num;
return 1;
}
//
int link_get_tail(link *p_link, int *p_num)
{
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_last == &(p_link->tail))
{
*p_num = p_mid->num;
return 1;
}
}
return 0;
}
//
int link_get(link *p_link, int *p_num, int num)
{
int cnt = 0;
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && cnt == num)
{
*p_num = p_mid->num;
return 1;
}
cnt++;
}
return 0;
}
//
void link_begin(link *p_link)
{
p_link->p_cur = &(p_link->head);
}
//
int link_next(link *p_link, int *p_num)
{
if (!p_link->p_cur)
{
return 0;
}
p_link->p_cur = p_link->p_cur->p_next;
if (p_link->p_cur == &(p_link->tail))
{
p_link->p_cur = NULL;
return 0;
}
else{
*p_num = p_link->p_cur->num;
return 1;
}
}
//
void link_rbegin(link *p_link)
{
p_link->p_cur = &(p_link->tail);
}
//
int link_prev(link *p_link, int *p_num)
{
if (!p_link->p_cur)
{
return 0;
}
p_link->p_cur = p_link->p_cur->p_pre;
if (p_link->p_cur == &(p_link->head))
{
p_link->p_cur = NULL;
return 0;
}
else{
*p_num = p_link->p_cur->num;
return 1;
}
}
main.cpp
/*
*
* */
#include
#include "link_0505.h"
int main() {
int num;
link lnk = {0};
link_init(&lnk);
append(&lnk,4);
append(&lnk,9);
append(&lnk,14);
append(&lnk,21);
append(&lnk,35);
link_add_head(&lnk,2);
link_insert(&lnk,17);
link_begin(&lnk);
while(link_next(&lnk,&num))
{
printf("%d ",num);
}
printf("
");
link_remove_head(&lnk);
link_remove_tail(&lnk);
link_remove(&lnk,9);
link_rbegin(&lnk);
while(link_prev(&lnk,&num))
{
printf("%d ", num);
}
printf("
");
printf(" %d
",link_size(&lnk));
link_deinit(&lnk);
return 0;
}