#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}node;
typedef struct{
node head;
node tail;
}link;
void link_init(link *p)
{
p->head.next = &(p->tail);
p->tail.next = NULL;
}
void link_delete(link *p)
{
node *first = NULL, *mid = NULL, *last = NULL;
while(p->head.next != &(p->tail)){
first = &(p->head);
mid = first->next;
last = mid->next;
if(mid != &(p->tail)){
first->next = last;
free(mid);
mid = NULL;
}
}
}
int link_size(const link *p)
{
int cnt = 0;
node *tmp = p->head.next;
while(tmp != &(p->tail)){
cnt++;
tmp = tmp->next;
}
return cnt;
}
int link_empty(const link *p)
{
return p->head.next == &(p->tail);
}
int link_add_head(link *p, int val)
{
node *new = NULL;
new = (node *)malloc(sizeof(node));
if(!new)
return 0;
new->data = val;
new->next = p->head.next;
p->head.next = new;
return 1;
}
int link_append(link *p, int val)
{
node *new = NULL, *tmp = NULL;
new = (node *)malloc(sizeof(node));
if(!new)
return 0;
new->data = val;
new->next = NULL;
tmp = &(p->head);
while(tmp->next != &(p->tail)){
tmp = tmp->next;
}
tmp->next = new;
new->next = &(p->tail);
return 1;
}
int link_insert(link *p, int val)
{
node *new = NULL, *tmp = NULL;
node *first = NULL, *mid = NULL, *last = NULL;
new = (node *)malloc(sizeof(node));
if(!new)
return 0;
new->data = val;
new->next = NULL;
for(tmp = &(p->head); tmp != &(p->tail); tmp = tmp->next){
first = tmp;
mid = first->next;
last = mid->next;
if(mid == &(p->tail) || mid->data > val){
first->next = new;
new->next = mid;
return 1;
}
}
}
int link_remove_head(link *p)
{
node *tmp = NULL;
if(p->head.next == &(p->tail))
return 0;
tmp = p->head.next;
p->head.next = tmp->next;
free(tmp);
tmp = NULL;
return 1;
}
int link_remove_tail(link *p)
{
node *tmp = NULL;
node *first = NULL, *mid = NULL, *last = NULL;
if(p->head.next == &(p->tail))
return 0;
for(tmp = &(p->head); tmp != &(p->tail); tmp = tmp->next){
first = tmp;
mid = first->next;
last = mid->next;
if(last == &(p->tail)){
first->next = last;
free(mid);
mid = NULL;
return 1;
}
}
}
int link_remove(link *p, int val)
{
node *tmp = NULL;
node *first = NULL, *mid = NULL, *last = NULL;
if(p->head.next == &(p->tail))
return 0;
for(tmp = &(p->head); tmp != &(p->tail); tmp = tmp->next){
first = tmp;
mid = first->next;
last = mid->next;
if(mid != &(p->tail) && mid->data == val){
first->next = last;
free(mid);
mid = NULL;
return 1;
}
}
return 0;
}
int link_get_head(const link *p, int *val)
{
if(p->head.next == &(p->tail))
return 0;
*val = p->head.next->data;
return 1;
}
int link_get_tail(const link *p, int *val)
{
const node *tmp = NULL;
if(p->head.next == &(p->tail))
return 0;
tmp = p->head.next;
while(tmp->next != &(p->tail)){
tmp = tmp->next;
}
*val = tmp->data;
return 1;
}
int link_get(const link *p, int pos, int *val)
{
int cnt = 0;
const node *tmp = NULL;
if(p->head.next == &(p->tail))
return 0;
tmp = p->head.next;
while(tmp != &(p->tail)){
if(cnt == pos){
*val = tmp->data;
return 1;
}
cnt++;
tmp = tmp->next;
}
return 0;
}
int main()
{
int i = 0, size = 0;
int ret = 0, val = 0;
link lk = {0};
link_init(&lk);
link_insert(&lk, 20);
link_insert(&lk, 30);
link_insert(&lk, 50);
link_add_head(&lk, 10);
link_append(&lk, 60);
link_insert(&lk, 40);
//link_remove_head(&lk);
//link_remove_tail(&lk);
link_remove(&lk, 30);
size = link_size(&lk);
for(i = 0; i < size; ++i){
ret = link_get(&lk, i, &val);
printf("%d ", val);
}
printf("\n");
link_delete(&lk);
return 0;
}
单链表的实现
最新推荐文章于 2018-06-26 15:10:59 发布