双向有头节点链表
2016-03-03 22:16
225 查看
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdbool.h>
#define MALLOC(name,type1,type2)\
if(!((name) = (type1)malloc(sizeof(type2)))){\
fprintf(stderr,"Insufficient_memmory");\
exit(EXIT_FAILURE);\
}
typedef enum {OK,ERROR}Status;
typedef int elemtype;
typedef struct Node* lInkedlist;
struct Node{
elemtype data;
struct Node* next;
struct Node* prior;
};
lInkedlist Listinit(lInkedlist head);
/*不带头节点链表初始化操作
*input:指向头节点的指针 head
return:初始化后的head
*/
unsigned int Listlength(lInkedlist head);
/*计算链表长度
*input:指向头节点的指针 head
*返回链表的长度
*/
lInkedlist Listget(lInkedlist head,unsigned int i);
/*取链表第i个节点
*input:指向头节点的指针 head,位置 i
*返回指向链表第i个节点的指针,否则返回NULL
*/
unsigned int Listlocate(lInkedlist head,elemtype e);
/* 定位函数
*若链表中存在其值和e相等的元素,则返回该元素在链表中的次序,
*若有多个返回次序最小的一个,没有返回0
*/
Status Listinsert(lInkedlist head,lInkedlist N,elemtype e);
/*向链表N节点之后插入元素为e的新的节点
*节点N,元素e
return:插入成功返回OK;
*/
Status Listdelete(lInkedlist head,lInkedlist N);
/*删除链表中N节点
*input:指向头节点的指针 head,节点N
return:删除成功返回OK,若链表中没有该节点返回ERROR
*/
void Listtraverse(lInkedlist head);
/*链表遍历打印所有节点
*input:指向头节点的指针 head
*/
bool Listempty(lInkedlist head);
lInkedlist Listinit(lInkedlist head){
MALLOC(head,lInkedlist,struct Node);
head->next = NULL;
return head;
}
unsigned int Listlength(lInkedlist head){
unsigned int length = 0;
head=head->next;
for(;head;head=head->next){
length++;
}
return length;
}
lInkedlist Listget(lInkedlist head,unsigned int i){
head = head->next;
if(i == 0 || i > Listlength(head)){
return NULL;
}
for(;i>1;i--){
head = head->next;
}
return head;
}
unsigned int Listlocate(lInkedlist head,elemtype e){
unsigned int count = 0;
head = head->next;
for(;head;head = head->next){
count++;
if(e == head->data){
return count;
}
}
return 0;
}
Status Listinsert(lInkedlist head,lInkedlist N,elemtype e){
lInkedlist temp;
MALLOC(temp,lInkedlist,struct Node);
temp->data = e;
if(N == head){
temp->next = head->next;
head->next = temp;
temp->prior = head;
}
else{
temp->prior = N->prior;
N->prior->next = temp;
temp->next = N;
N->prior = temp;
}
return OK;
}
Status Listdelete(lInkedlist head,lInkedlist N){
unsigned int i = Listlocate(head,N->data);
if(i == 0){
printf("链表中不存在该节点无法删除");
return ERROR;
}
N->prior->next = N->next;
N->next->prior = N->prior;
free(N);
N = NULL;
return OK;
}
void Listtraverse(lInkedlist head){
head = head->next;
if(head == NULL){
printf("链表为空");
}
for(;head;head = head->next){
printf("%4d\n",head->data);
}
}
bool Listempty(lInkedlist head){
head = head->next;
return !head;
}
#include<malloc.h>
#include<stdlib.h>
#include<stdbool.h>
#define MALLOC(name,type1,type2)\
if(!((name) = (type1)malloc(sizeof(type2)))){\
fprintf(stderr,"Insufficient_memmory");\
exit(EXIT_FAILURE);\
}
typedef enum {OK,ERROR}Status;
typedef int elemtype;
typedef struct Node* lInkedlist;
struct Node{
elemtype data;
struct Node* next;
struct Node* prior;
};
lInkedlist Listinit(lInkedlist head);
/*不带头节点链表初始化操作
*input:指向头节点的指针 head
return:初始化后的head
*/
unsigned int Listlength(lInkedlist head);
/*计算链表长度
*input:指向头节点的指针 head
*返回链表的长度
*/
lInkedlist Listget(lInkedlist head,unsigned int i);
/*取链表第i个节点
*input:指向头节点的指针 head,位置 i
*返回指向链表第i个节点的指针,否则返回NULL
*/
unsigned int Listlocate(lInkedlist head,elemtype e);
/* 定位函数
*若链表中存在其值和e相等的元素,则返回该元素在链表中的次序,
*若有多个返回次序最小的一个,没有返回0
*/
Status Listinsert(lInkedlist head,lInkedlist N,elemtype e);
/*向链表N节点之后插入元素为e的新的节点
*节点N,元素e
return:插入成功返回OK;
*/
Status Listdelete(lInkedlist head,lInkedlist N);
/*删除链表中N节点
*input:指向头节点的指针 head,节点N
return:删除成功返回OK,若链表中没有该节点返回ERROR
*/
void Listtraverse(lInkedlist head);
/*链表遍历打印所有节点
*input:指向头节点的指针 head
*/
bool Listempty(lInkedlist head);
lInkedlist Listinit(lInkedlist head){
MALLOC(head,lInkedlist,struct Node);
head->next = NULL;
return head;
}
unsigned int Listlength(lInkedlist head){
unsigned int length = 0;
head=head->next;
for(;head;head=head->next){
length++;
}
return length;
}
lInkedlist Listget(lInkedlist head,unsigned int i){
head = head->next;
if(i == 0 || i > Listlength(head)){
return NULL;
}
for(;i>1;i--){
head = head->next;
}
return head;
}
unsigned int Listlocate(lInkedlist head,elemtype e){
unsigned int count = 0;
head = head->next;
for(;head;head = head->next){
count++;
if(e == head->data){
return count;
}
}
return 0;
}
Status Listinsert(lInkedlist head,lInkedlist N,elemtype e){
lInkedlist temp;
MALLOC(temp,lInkedlist,struct Node);
temp->data = e;
if(N == head){
temp->next = head->next;
head->next = temp;
temp->prior = head;
}
else{
temp->prior = N->prior;
N->prior->next = temp;
temp->next = N;
N->prior = temp;
}
return OK;
}
Status Listdelete(lInkedlist head,lInkedlist N){
unsigned int i = Listlocate(head,N->data);
if(i == 0){
printf("链表中不存在该节点无法删除");
return ERROR;
}
N->prior->next = N->next;
N->next->prior = N->prior;
free(N);
N = NULL;
return OK;
}
void Listtraverse(lInkedlist head){
head = head->next;
if(head == NULL){
printf("链表为空");
}
for(;head;head = head->next){
printf("%4d\n",head->data);
}
}
bool Listempty(lInkedlist head){
head = head->next;
return !head;
}
相关文章推荐
- C#的Reflection总结
- C#的Reflection总结
- 这不是一支烟斗 图灵链条 薛定谔的猫 [20160303]
- 自定义标签
- jquery easyui toolbar 分割线问题
- demo05
- 剑指offer-面试题5:从尾到头打印链表
- Git_Windows下怎么保存用户名和密码
- Ubuntu 14.04安装在Optiplex 9020 Dell台式机器上以太网连不上问题解决方案
- 全球最牛的105个博客
- [leetcode319] Bulb Switcher--判断约数的个数的奇偶性
- 学习Java第一卷--态度的转变
- file handling in python
- c#---指针
- modal or modeless
- Android学习笔记----自定义ContentProvider
- 唯一性索引(Unique Index)与普通索引(Normal Index)性能差异
- C#数据类型汇总
- org.eclipse.wst.xsl.jaxp.debug.invoker.TransformationException
- C#数据类型汇总