赞
踩
双向链表是链表的一种,与单链表不同的shi,它的每个数据结点中都有两个指针,分别指向直接前驱和直接后继。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。下图为双向链表的结构图。
typedef struct DoubleLinkNode
{
char data;
struct DoubleLinkNode *previous;
struct DoubleLinkNode *next;
}DLNode,*DLNodeptr;
DLNodeptr initLinkList()
{
DLNodeptr tempHeader=(DLNodeptr)malloc(sizeof(DLNode));
tempHeader->data='\0';
tempHeader->previous=NULL;
tempHeader->next=NULL;
return tempHeader;
}
与单链表基本一致
void printList(DLNodeptr pHeader)
{
DLNodeptr p=pHeader->next;
while(p)
{
printf("%c",p->data);
p=p->next;
}
printf("\r\n");
}
同单链表类似
如图所示
void insertElement(DLNodeptr pHeader,char pchar,int pos) { DLNodeptr p,q,r; p=pHeader; int i; for(i=0;i<pos;i++) { p=p->next; if(p==NULL) { printf("该位置不在链表范围内!"); return; } } q=(DLNodeptr)malloc(sizeof(DLNode)); q->data=pchar; r=p->next; q->next=p->next; q->previous=p; p->next=q; if(r!=NULL) { r->previous=q; } }
void deleteElement(DLNodeptr pHeader,char Achar) { DLNodeptr p,q,r; p=pHeader; while((p->next) && (p->next->data!=Achar)) { p=p->next; } if(p->next==NULL) { printf("该数据不存在于链表中!\n"); return; } q=p->next; r=q->next; p->next=r; if(r) { r->previous=p; } free(q); }
void locateElement(DLNodeptr pHeader,char Achar) { DLNodeptr p,q; p=pHeader; int i=0; while((p->next) && (p->next->data!=Achar)) { p=p->next; i++; } if(p->next ==NULL) { printf("该元素(%c)不存在于链表中!\n",Achar); return; } printf("The element(%c)’s location is %d.\n",Achar,i); }
void destroy_list(DLNodeptr tempList)
{
DLNodeptr p,q;
p=tempList;
if(p)
{
q=p;
p=p->next;
free(q);
}
printf("清空成功!\n");
}
void insert_delete_locate_destroy_list_Text() { DLNodeptr tempList=initLinkList(); printList(tempList); insertElement(tempList, 'H', 0); insertElement(tempList, 'e', 1); insertElement(tempList, 'l', 2); insertElement(tempList, 'l', 3); insertElement(tempList, 'o', 4); insertElement(tempList, '!', 5); printList(tempList); deleteElement(tempList, 'H'); deleteElement(tempList, 'a'); deleteElement(tempList, '!'); printList(tempList); insertElement(tempList, 'o', 1); printList(tempList); locateElement(tempList,'p'); locateElement(tempList,'o'); destroy_list(tempList); }
#include<stdio.h> #include<malloc.h> typedef struct DoubleLinkNode { char data; struct DoubleLinkNode *previous; struct DoubleLinkNode *next; }DLNode,*DLNodeptr; DLNodeptr initLinkList() { DLNodeptr tempHeader=(DLNodeptr)malloc(sizeof(DLNode)); tempHeader->data='\0'; tempHeader->previous=NULL; tempHeader->next=NULL; return tempHeader; } void printList(DLNodeptr pHeader) { DLNodeptr p=pHeader->next; while(p) { printf("%c",p->data); p=p->next; } printf("\r\n"); } void insertElement(DLNodeptr pHeader,char pchar,int pos) { DLNodeptr p,q,r; p=pHeader; int i; for(i=0;i<pos;i++) { p=p->next; if(p==NULL) { printf("该位置不在链表范围内!"); return; } } q=(DLNodeptr)malloc(sizeof(DLNode)); q->data=pchar; r=p->next; q->next=p->next; q->previous=p; p->next=q; if(r!=NULL) { r->previous=q; } } void deleteElement(DLNodeptr pHeader,char Achar) { DLNodeptr p,q,r; p=pHeader; while((p->next) && (p->next->data!=Achar)) { p=p->next; } if(p->next==NULL) { printf("该数据不存在于链表中!\n"); return; } q=p->next; r=q->next; p->next=r; if(r) { r->previous=p; } free(q); } void locateElement(DLNodeptr pHeader,char Achar) { DLNodeptr p,q; p=pHeader; int i=0; while((p->next) && (p->next->data!=Achar)) { p=p->next; i++; } if(p->next ==NULL) { printf("该元素(%c)不存在于链表中!\n",Achar); return; } printf("The element(%c)’s location is %d.\n",Achar,i); } void insert_delete_locate_destroy_list_Text() { DLNodeptr tempList=initLinkList(); printList(tempList); insertElement(tempList, 'H', 0); insertElement(tempList, 'e', 1); insertElement(tempList, 'l', 2); insertElement(tempList, 'l', 3); insertElement(tempList, 'o', 4); insertElement(tempList, '!', 5); printList(tempList); deleteElement(tempList, 'H'); deleteElement(tempList, 'a'); deleteElement(tempList, '!'); printList(tempList); insertElement(tempList, 'o', 1); printList(tempList); locateElement(tempList,'p'); locateElement(tempList,'o'); destroy_list(tempList); } void destroy_list(DLNodeptr tempList) { DLNodeptr p,q; p=tempList; if(p) { q=p; p=p->next; free(q); } printf("清空成功!\n"); } void main() { insert_delete_locate_destroy_list_Text(); }
运行结果:
Hello!
该数据不存在于链表中!
ello
eollo
该元素(p)不存在于链表中!
The element(o)’s location is 1.
清空成功!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。