数据结构C语言实现系列——双向链表

来源:互联网 发布:国网总部五位一体优化 编辑:程序博客网 时间:2024/04/24 16:12
// pointer.cpp : 定義主控台應用程式的進入點。//#include "stdafx.h"#include <stdio.h>#include <stdlib.h>typedef int elemType ;/************************************************************************//*             以下是关于线性表链接存储(双向链表)操作的16种算法        *//************************************************************************/typedef struct DulNode{elemType data;struct DulNode *pre;struct DulNode *next;}dNode;/* 1.初始化线性表,即置单链表的表头指针为空 */void initList(dNode* *hl){*hl=NULL;return;}/* 3.返回双链表的长度 */int sizeList(dNode *hl){int len=1;dNode *pre_node,*next_node,*list;list=hl;if(list==NULL){printf("Error----------> Empty List!\n");return 0;}while(list->next!=hl)   //顺序遍历{if(list==NULL){printf("Error----------> Broken List!\n");return -1;}len++;list=list->next;}return len;}/* 6.遍历一个双向链表 */void traverseList(dNode *hl){dNode *list;int len=0;list=hl;if(list==NULL){printf("Error----------> Empty List!\n");return;}do{printf("%d  ",list->data);if(list==NULL){printf("Error----------> Broken List!\n");return;}list=list->next;len++;}while(list!=hl);printf("\nlen=%d\n",len);}/* 9.向双向链表的表头插入一个元素 */void insertFirstList(dNode* *hl, elemType x){dNode *list,*new_node;new_node=(dNode *)malloc(sizeof(dNode));if(new_node==NULL){printf("Error----> Mallco Fail!\n");return;}new_node->data=x;list=*hl;          //存储头指针if(list==NULL)     //空链表{new_node->next=new_node;new_node->pre=new_node;*hl=new_node;}else{new_node->next=list;new_node->pre=list->pre;list->pre->next=new_node;      //尾结点list->pre=new_node;*hl=new_node;}}/* 10.向双向链表的末尾添加一个元素 */void insertLastList(dNode* *hl, elemType x){dNode *list,*new_node;new_node=(dNode *)malloc(sizeof(dNode));if(new_node==NULL){printf("Error----> Mallco Fail!\n");return;}new_node->data=x;new_node->next=new_node;new_node->pre=new_node;list=*hl;if(list==NULL) //空链表{*hl=new_node;}else{new_node->next=list;new_node->pre=list->pre;list->pre->next=new_node;list->pre=new_node;}}/* 11.向双链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0 */int insetPosList(dNode* *hl, int pos, elemType x){dNode *list,*new_node,*last;int len=0;int count=0;new_node=(dNode *)malloc(sizeof(dNode));if(new_node==NULL){printf("Error----> Mallco Fail!\n");return 0;}new_node->data=x;new_node->next=new_node;new_node->pre=new_node;list=*hl;len=sizeList(list);if(pos<1||pos>len){printf("Error----> Out range!\n");return 0;}else{if(pos==1){insertFirstList(hl,x);return 1;}else if(pos<len){while(1){count++;if(count==pos){list->pre->next=new_node;new_node->pre=list->pre;new_node->next=list;list->pre=new_node;return 1;}list=list->next;}}else if(pos==len){insertLastList(hl,x);return 1;}}}/* 13.从双链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行 */elemType deleteFirstList(dNode* *hl){dNode *list;elemType temp;int len=0;list=*hl;len=sizeList(list);temp=list->data;if(len==0){printf("Error----------> Empty List!\n");return 0;}else if(len==1){free(*hl);return temp;}else{list->next->pre=list->pre;printf("%d\n",list->pre->data);printf("%d\n",list->next->data);list->pre->next=list->next;(*hl)=list->next;free(list);return temp;}}/* 14.从双链表中删除表尾结点并返回它的值,若删除失败则停止程序运行 */elemType deleteLastList(dNode* *hl){dNode *list,*last;elemType temp;int len=0;list=*hl;len=sizeList(list);temp=list->pre->data;if(len==0){printf("Error----------> Empty List!\n");return 0;}else if(len==1){free(*hl);return temp;}else{last=list->pre;last->pre->next=list;list->pre=last->pre;free(last);return temp;}}/* 15.从双向链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行 */elemType deletePosList(dNode* *hl, int pos){dNode *list;elemType temp;int len=1;list=*hl;if(pos==1&&list!=NULL){temp=list->data;free(*hl);return temp;}while(list->next!=*hl){if(len==pos){temp=list->data;list->pre->next=list->next;list->next->pre=list->pre;free(list);return temp;}len++;list=list->next;}return 0;}int main(int argc, char* argv[]){int i;int a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; dNode List,*p;  p=&List;initList(&p);for(i = 0;i<10;i++){          insertLastList(&p, a[i]);    }  printf("链表长度=%d\n",sizeList(p));traverseList(p);insetPosList(&p,3,1);traverseList(p);printf("删除头指针=%d\n",deleteFirstList(&p));printf("删除头指针=%d\n",deleteLastList(&p));traverseList(p);deletePosList(&p,2);traverseList(p);while(1);}


原创粉丝点击