双向链表

原创 2012年03月26日 23:08:28
[web@localhost d2]$ gcc --version
gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)

Copyright (C) 2010 Free Software Foundation, Inc.

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

struct node{
	int value;
	struct node *next;
	struct node *pre;
};

typedef struct node LNode;
typedef struct node *SeqList;

void SeqListInit(SeqList *L){
	*L=NULL;
}

int SeqListLength(SeqList L){
	int l;
	l=0;
	//SeqList *t;
	//t=&L;
	while(L!=NULL){
		l++;
		L=L->next;
	}
	//L=*t;
	return l;
}
int SeqListGet(SeqList L,int i){
	int l;
	l=SeqListLength(L);
	if(l==0 || i>l) return -1;
	int h;
	h=1;
	while(h!=i){
		L=L->next;
		h++;
	}
	return L->value;
}
int SeqListLocate(SeqList L,int i){
	int l;
	l=SeqListLength(L);
	if(l==0) return -1;
	int h=1;
	while(L!=NULL){
		if(L->value==i) return h;
		L=L->next;
		h++;
	}
	return -1;
}
int SeqListPrior(SeqList L,int i){
	int l;
	l=SeqListLocate(L,i);
	if(l>1){
		return SeqListGet(L,l-1);
	}else{
		return -1;
	}
}
int SeqListNrior(SeqList L,int i){
	int l,r;
	l=SeqListLocate(L,i);
	r=(l>1 && l<SeqListLength(L))?SeqListGet(L,l+1):-1;
	return r;
}
SeqList SeqListPreValue(SeqList L,int i){
	while(L!=NULL){
		if(L->value!=i){ L=L->next;}else{return L->pre;}
	}
	return NULL;	
}
SeqList SeqListNextValue(SeqList L,int i){
	while(L!=NULL){
		if(L->value!=i){ L=L->next;}else{return L->next;}
	}
	return NULL;	
}
void SeqListInsert(SeqList *L,int i,int j){
	LNode *n;
	printf("in%X\n",*L);
	SeqList lt;
	lt=&(**L);
	printf("in%X\n",&(**L));
	printf("in%X\n",lt);
	printf("in%X\n",*L);
	n = (LNode*)malloc(sizeof(LNode));
	if(n==NULL) exit(0);
	n -> value = i;
	n -> next  = NULL;
	if(*L==NULL){
		*L=n;
	}else{
		int l;
		l=SeqListLength(*L);
		if(j>l){
			printf("%d>%d\n",j,l);
			
			while((**L).next!=NULL){
				printf("1");
				*L=(**L).next;
			}
			(**L).next=n;
			n->pre=&(**L);
			*L=lt;
			
		}else if(j>0 && j<=l){
			printf("%d<=%d\n",j,l);
			int h=1;
			if(j==1){
			n->next=lt;
			lt->pre=n;
			*L=n;
			}else if(j>0 && j!=1){
				while(h<j-1){
				*L=(**L).next;	
				h++;
				}
			n->next=(**L).next;
			((**L).next)->pre=n;
			n->pre=&(**L); 
			(**L).next=n;
			 *L=lt;
			}
		}
	}
	printf("in%X\n",lt);
	printf("out%X\n",*L);
}
void SeqListDel(SeqList *L,int i){
	int l=SeqListLength(*L);
	SeqList lt;
	lt=&(**L);
	if(i>0 && l>0 && i<=l){
		int h;
		if(i==1){
		LNode *t;
		t=lt;
		(**L).pre=NULL;
		*L=(**L).next;	
		free(t);
		}else{
		for(h=1;h<i;h++){
			*L=(**L).next;
		}
		LNode *t;
		t=(**L).next;
		(**L).next=t->next;
		t->pre=&(**L);
		*L=lt;
		free(t);
		}
	}
}
bool SeqListIsEmpty(SeqList L){
	bool r;
	r=(L==NULL)?true:false;	
	return r;
}
void SeqEmpty(SeqList *L){
	*L=NULL;
}
void SeqListTraverse(SeqList L){
	while(L!=NULL){
		printf("addr[%X]value[%d]->",&L,L->value);
		L=L->next;
	}
}
void main(){
	SeqList L,h;
	printf("init:");
	SeqListInit(&L);
	printf("L\n");
	printf("length:%d\n",SeqListLength(L));
	printf("add iterm 1\n");
	SeqListInsert(&L,1,1);
	printf("length:%d\n",SeqListLength(L));
	SeqListTraverse(L);
	printf("\n");
	printf("add iterm 2\n");
	SeqListInsert(&L,2,2);
	printf("length:%d\n",SeqListLength(L));
	SeqListTraverse(L);
	printf("\n");
	printf("add iterm 3\n");
	SeqListInsert(&L,3,3);
	printf("length:%d\n",SeqListLength(L));
	SeqListTraverse(L);
	printf("\n");
	printf("add iterm 4\n");
	SeqListInsert(&L,4,4);
	printf("length:%d\n",SeqListLength(L));
	SeqListTraverse(L);
	SeqListInsert(&L,5,1);
	printf("\n");
	SeqListTraverse(L);
	printf("\n");
	SeqListInsert(&L,6,2);
	printf("\n");
	SeqListTraverse(L);
	printf("\n");
	SeqListInsert(&L,8,8);
	printf("\n");
	SeqListTraverse(L);
	printf("\n");
	printf("the last value of 3 is %d\n",SeqListPrior(L,3));
	printf("the last value of 5 is %d\n",SeqListPrior(L,5));
	printf("the last value of 3 is %d\n",(SeqListPreValue(L,3))->value);
	printf("the last value of 2 is %d\n",(SeqListPreValue(L,2))->value);
	printf("the last value of 6 is %d\n",(SeqListPreValue(L,6))->value);
	if(SeqListPreValue(L,5)!=NULL)printf("the last value of 5 is %d\n",(SeqListPreValue(L,5))->value);
	printf("the next value of 2 is %d\n",SeqListNrior(L,2));
	printf("the next value of 4 is %d\n",SeqListNrior(L,4));
	printf("the next value of 4 is %d\n",(SeqListNextValue(L,4))->value);
	if(SeqListNextValue(L,8))printf("the next value of 8 is %d\n",(SeqListNextValue(L,8))->value);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListTraverse(L);
	printf("\n");
	SeqListDel(&L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListTraverse(L);
	printf("\n");
	SeqListDel(&L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListTraverse(L);
	printf("\n");
	SeqListDel(&L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListTraverse(L);
	printf("\n");
	SeqListDel(&L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListTraverse(L);
	printf("\n");
	SeqListDel(&L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListTraverse(L);
	printf("\n");
	SeqListDel(&L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListTraverse(L);
	printf("\n");
	SeqListDel(&L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListTraverse(L);
	printf("\n");
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

c语言实现的双向链表

  • 2016-07-31 15:45
  • 21KB
  • 下载

排序树 变成双向链表

  • 2014-09-14 20:58
  • 521B
  • 下载

剑指offer-面试题27.二叉搜索树与双向链表

题目:输入一颗二叉搜索树,将该二叉搜索树转换为一个排序的双向链表。要求不能创建任何新的结点,只能调整树种结点指针的指向。比如输入下图的二叉搜索树,则输出转换后的双向排序链表。 1 ...

双向链表——1

  • 2014-05-02 17:02
  • 524B
  • 下载

剑指offer刷题之c实现的二叉搜索树转换成双向链表

题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 参考文献:剑指offer 何海涛老师 P152 思路:将...

双向链表操作

  • 2014-10-28 11:22
  • 36KB
  • 下载

多重双向链表

  • 2015-06-18 09:13
  • 26KB
  • 下载

双向链表的简单实现

代码实现 #include #include using namespace std; typedef int DataType; struct ListNode { ListNode* _n...

双向链表的实现

  • 2015-05-16 10:40
  • 374KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)