双向链表的练习代码
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int database;
typedef struct double_link_list{union{database data;int len;};struct double_link_list* pre;struct double_link_list* next;}dbLinkList,*dbLinkListp;dbLinkListp head_create();
dbLinkListp create(database data);
int head_insert(dbLinkListp head,database data);
int tail_insert(dbLinkListp head,database data);
int output(dbLinkListp head);
int head_delete(dbLinkListp head);
int tail_delete(dbLinkListp head);
int pos_insert(dbLinkListp head,int pos,database data);
int pos_delete(dbLinkListp head,int pos);#endif
fun.c
#include "head.h"
/** function: 头结点创建* @param [ in] * @param [out] * @return */
dbLinkListp head_create(){dbLinkListp head=(dbLinkListp)malloc(sizeof(dbLinkList));if(NULL==head){puts("malloc is NULL,failed");return NULL;}head->pre=NULL;head->next=NULL;head->len=0;return head;
}
/** function: 创建节点* @param [ in] * @param [out] * @return */
dbLinkListp create(database data){dbLinkListp new=(dbLinkListp)malloc(sizeof(dbLinkList));if(NULL==new){puts("malloc is NULL,failed");return NULL;}new->data=data;new->pre=NULL;new->next=NULL;puts("node create success");return new;
}
/** function: 判空* @param [ in] * @param [out] * @return */
int void_point(dbLinkListp head){if(NULL==head){puts("----why give me a null head point----");return -1;}return 0;
}
/** function: 头插* @param [ in] * @param [out] * @return */
int head_insert(dbLinkListp head,database data){if(void_point(head))return -1;//头插dbLinkListp new=create(data);new->next=head->next;if(head->next!=NULL)new->next->pre=new;head->next=new;new->pre=head;head->len++;puts("head insert success");return 0;
}
/** function: 尾插* @param [ in] * @param [out] * @return */
int tail_insert(dbLinkListp head,database data){if(void_point(head))return -1;//尾插dbLinkListp p=head;while(p->next!=NULL)p=p->next;dbLinkListp new=create(data);new->next=p->next;new->pre=p;p->next=new;head->len++;puts("tail insert success.");return 0;
}
/** function: 输出* @param [ in] * @param [out] * @return */
int output(dbLinkListp head){if(void_point(head))return -1;while(head->next!=NULL){printf("%d\t",head->next->data);head=head->next;}puts("output done");return 0;
}
/** function: 头删* @param [ in] * @param [out] * @return */
int head_delete(dbLinkListp head){if(void_point(head))return -1;if(head->next==NULL){puts("there is no assignment to delete.");return -1;}dbLinkListp del=head->next;if(head->next->next!=NULL)head->next->next->pre=head;head->next=head->next->next;free(del);del=NULL;head->len--;puts("head_delete success");return 0;
}
/** function: 尾删* @param [ in] * @param [out] * @return */
int tail_delete(dbLinkListp head){if(void_point(head))return -1;if(head->next==NULL){puts("there is no assignment to delete.");return -1;}dbLinkListp p=head;//找到倒数第一个while(p->next!=NULL){p=p->next;}p->pre->next=p->next;free(p);p=NULL;head->len--;puts("tail delete success");return 0;
}
/** function: 指定位置插入* @param [ in] * @param [out] * @return */
int pos_insert(dbLinkListp head,int pos,database data){if(void_point(head))return -1;//判断位置合理性if(pos<1||pos>head->len+1){puts("your position is illegal");return -1;}dbLinkListp p=head;//找到指定位置前一位while(pos--!=1){p=p->next;}dbLinkListp new=create(data);if(p->next!=NULL){new->next=p->next->next;p->next->next->pre=new;}new->pre=p;p->next=new;head->len++;puts("pos insert success");return 0;
}
/** function: 指定位置删除* @param [ in] * @param [out] * @return */
int pos_delete(dbLinkListp head,int pos){if(void_point(head))return -1;if(head->next==NULL){puts("there is no assignment to delete.");return -1;}//判断位置合理性if(pos<1||pos>head->len){puts("your position is illegal");return -1;}dbLinkListp p=head;//找到指定位置前一位while(pos--!=1){p=p->next;}printf("pos-1=%d\t",p->data);//删除dbLinkListp del=p->next;p->next=p->next->next;if(p->next!=NULL)p->next->pre=p;free(del);del=NULL;puts("pos delete success");return 0;
}
main.c
#include "head.h"
int main(int argc, const char *argv[])
{dbLinkListp head=head_create();head_insert(head,9);head_insert(head,8);head_insert(head,6);head_insert(head,5);tail_insert(head,7);output(head);//head_delete(head);//tail_delete(head);//pos_insert(head,6,66);//pos_delete(head,5);output(head);return 0;
}
今日思维导图哈