与双向链表有关的C程序 [英] A C Program related to doubly linked list

查看:69
本文介绍了与双向链表有关的C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

4.下一个问题解决了将新元素添加到双向链接列表中的问题.
记住那
i)列表中的元素应按升序排列.
ii)您的答案必须解决以下问题:a)添加到空白列表,b)添加到列表的开头,c)添加到列表的末尾,以及d)添加到现有列表的中间.

4. The next problem addresses adding a new element to a doubly-linked list.
Remember that
i) the elements of the list are to kept in ascending numerical order.
ii) your answers must address issues of a) adding to an empty list, b) adding at the start of the list, c) adding to the end of the list, as well as d) adding somewhere in the middle of an existing list.

推荐答案

/*添加到空链中*/
doubleChainCell * addInOrder(doubleChainCell * header,int data){
doubleChainCell * addee = new(NULL,data,NULL);
if(header == NULL){
标头= addee;
返回标头;
}

/*在链的开头添加*/
if(数据<标头->数据){
addee-> next =标头;
标头= addee;
返回标头;
}

/*在链的末尾添加*/
doubleChainCell * pt =标头,* previous = NULL;
while((pt!= NULL)&&(data> pt-> data)){
上一页= pt;
pt = pt-> next;
}
previous-> next = addee;
addee->上一个=上一个;
addee-> next = pt;
if(pt!= NULL){

pt->上一个= addee;
}
返回标头;
}
/* Adding to an empty chain*/
doubleChainCell* addInOrder(doubleChainCell *header, int data) {
doubleChainCell *addee = new(NULL, data, NULL);
if (header == NULL) {
header = addee;
return header;
}

/* Adding at the start of the chain */
if (data < header->data) {
addee->next = header;
header = addee;
return header;
}

/* Adding at the end of the chain */
doubleChainCell *pt = header, *previous = NULL;
while ((pt != NULL) && (data > pt->data)) {
previous = pt;
pt = pt->next;
}
previous->next = addee;
addee->previous = previous;
addee->next = pt;
if (pt != NULL) {

pt->previous = addee;
}
return header;
}


这篇关于与双向链表有关的C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆