循环双链表的各种基本运算的算法

编写一个程序,实现双链表的各种基本运算和整体建表算法(假设双 链表的元素类型 ElemType 为 char),并在此基础上设计一个程序 cdlink_main.c 完成以下功能方法。

(1)初始化循环双链表 h。

(2)依次采用尾插法插入 a、b、c、d、e 元素 。

(3)输出循环双链表 h。

(4)输出循环双链表 h 的长度。

(5)判断循环双链表 h 是否为空。

(6)输出循环双链表 h 的第 3 个元素。

(7)在第 4 个元素的位置上插入 f 元素。

(8)输出循环双链表 h。

(9)删除循环双链表 h 的第 3 个元素。

(10)输出循环双链表 h。

(11)释放循环双链表 h。

#include <stdio.h> 
#include <cstdlib> 
typedef char ElemType; 
typedef struct Dnode 
{ ElemType data; 
 struct Dnode *prior; 
 struct Dnode *next; 
}DLinkNode; 
 
DLinkNode * InitList() 
 { 
 DLinkNode *L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结
点 
 L->next=NULL; 
 return L; 
 } 
 
 void CreateListR(DLinkNode *&h,int n) //尾插法建立循环双
链表 
{ 
 DLinkNode *s,*r;int i; 
 h=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点 
 h->next=NULL; 
 r=h; 
 printf("请输入字符数组:\n"); //r 始终指向尾结点,开始时指向头结点 
 for (i=0;i<n;i++) 
 { 
 s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点 
 scanf("%s",&s->data); 
 r->next=s;s->prior=r; //将结点 s 插入结点
r 之后 
 r=s; 
 } 
 r->next=h; //尾结点 next 域指向头
结点 
 h->prior=r; //头结点的 prior 域
指向尾结点 
} 
 
 void DestroyList(DLinkNode **h) 
{ 
 DLinkNode *p=*h,*q=p->next; 
 while (q!=*h) 
 { 
 free(p); 
 p=q; 
 q=p->next; 
 } 
 free(p); 
} 
 
 bool ListEmpty(DLinkNode *h) 
 { 
 return(h->next==h); 
 } 
 
 int ListLength(DLinkNode *h) 
 { 
 DLinkNode *p=h; 
 int i=0; 
 while (p->next!=h) 
 { 
 i++; 
 p=p->next;  } 
 return(i); 
 } 
 
 void DispList(DLinkNode *h) 
 { 
 DLinkNode *p=h->next; 
 while (p!=h) 
 { 
 printf("%c",p->data); 
 p=p->next; 
 } 
 printf("\n"); 
 } 
 
 bool GetElem(DLinkNode *L,int i,ElemType &e) //求线性表中第 i 个元
素值 
 { 
 int j=1; 
 DLinkNode *p=L->next; 
 if (i<=0 || L->next==L) 
 return false; //i 错误或者 L 为空表返回假 
 if (i==1) //i=1 作为特殊情况处理 
 { 
 e=L->next->data; 
 return true; 
 } 
 else //i 不为 1 时 
 { 
 while (j<=i-1 && p!=L) //查找第 i 个结点 p 
 { 
 j++; 
 p=p->next; 
 } 
 if (p==L) //没有找到第 i 个节,返回假 
 return false; 
 else //找到了第 i 个节,返回真 
 { 
 e=p->data; 
 return true;  } 
 } 
 } 
 
bool ListInsert(DLinkNode *h,int i,ElemType e) 
{ 
 int j=0; 
 DLinkNode *p=h,*s; 
 if (p->next==h) //原双链表为空表时 
 { 
 s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点 s 
 s->data=e; 
 p->next=s;s->next=p; 
 p->prior=s;s->prior=p; 
 return true; 
 } 
 else if (i==1) //原双链表不为空表
但 i=1 时 
 { 
 s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点 s 
 s->data=e; 
 s->next=p->next;p->next=s; //将结点 s 插入到结
点 p 之后 
 s->next->prior=s;s->prior=p; 
 return true; 
 } 
 else 
 { 
 p=h->next; 
 while (j<i-2 && p!=h) 
 { j++; 
 p=p->next; 
 } 
 if (p==h) //未找到第 i-1 个结点 
 return false; 
 else //找到第 i-1 个结点*p 
 { 
 s=(DLinkNode *)malloc(sizeof(DLinkNode));//创建新结点 s 
 s->data=e; 
 s->next=p->next; //将结点 s 插入到结点p 之后 
 if (p->next!=NULL) p->next->prior=s; 
 s->prior=p; 
 p->next=s; 
 return true; 
 } 
 } 
} 
 
 bool ListDelete(DLinkNode *h,int i) 
 { 
 int j=0; 
 DLinkNode *p=h,*q; 
 while(j<i-1&&p!=NULL) 
 { 
 j++; 
 p=p->next; 
 } 
 if (p==NULL) //未找到第 i-1 个结点 
 return false; 
 else //找到第 i-1 个结点 p 
 { q=p->next; //q 指向第 i 个结点 
 if (q==NULL) //当不存在第 i 个结点时返
回 false 
 return false; 
 p->next=q->next; //从双链表中删除 q 结点 
 if (q->next!=NULL) //若 q 结点存在后继结点 
 q->next->prior=p; //修改 q 结点后继结点
的前驱指针 
 free(q); //释放 q 结点 
 return true; 
 } 
 } 
 
 int main() 
 { 
 DLinkNode *h; 
 ElemType e ; 
 h=InitList(); 
 CreateListR(h,5);  DispList(h); 
 printf("循环双链表 h 的长度为:%d\n",ListLength(h)); 
 while(!ListEmpty(h)) 
 { 
 printf("循环双链表不为空。\n"); 
 break; 
 } 
 GetElem(h,3,e); 
 printf("链表第 3 个元素为:%c\n",e); 
 ListInsert(h,4,'f'); 
 DispList(h); 
 ListDelete(h,3); 
 DispList(h); 
 DestroyList(&h); 
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现循环双链表的各种基本运算算法的代码: ```python class CDNode: def __init__(self, data=None): self.data = data self.prev = None self.next = None class CDLinkedList: def __init__(self): self.head = CDNode() self.head.prev = self.head self.head.next = self.head self.length = 0 def is_empty(self): return self.length == 0 def get_length(self): return self.length def get_elem(self, index): if index < 1 or index > self.length: return None p = self.head.next for i in range(1, index): p = p.next return p.data def locate_elem(self, elem): p = self.head.next i = 1 while p != self.head and p.data != elem: p = p.next i += 1 if p == self.head: return 0 else: return i def insert(self, index, elem): if index < 1 or index > self.length + 1: return False p = self.head for i in range(1, index): p = p.next node = CDNode(elem) node.prev = p node.next = p.next p.next.prev = node p.next = node self.length += 1 return True def delete(self, index): if index < 1 or index > self.length: return False p = self.head for i in range(1, index): p = p.next p.next.prev = p.prev p.prev.next = p.next del p self.length -= 1 return True def append(self, elem): node = CDNode(elem) node.prev = self.head.prev node.next = self.head self.head.prev.next = node self.head.prev = node self.length += 1 def create_list(self, elem_list): for elem in elem_list: self.append(elem) def clear(self): while self.head.next != self.head: self.delete(1) def print_list(self): p = self.head.next while p != self.head: print(p.data, end=' ') p = p.next print() ``` 以下是实现实验题2要求的代码: ```python if __name__ == '__main__': l = CDLinkedList() l.create_list([1, 2, 3, 4, 5]) l.print_list() # 1 2 3 4 5 print(l.get_length()) # 5 print(l.is_empty()) # False print(l.get_elem(3)) # 3 print(l.locate_elem(1)) # 1 l.insert(4, 6) l.print_list() # 1 2 3 6 4 5 l.delete(3) l.print_list() # 1 2 6 4 5 l.clear() l.print_list() # ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值