文章目录
我的代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
struct _d_list
{
int data;
struct _d_list *pre;
struct _d_list *next;
};
typedef struct _d_list DL_LINK_LIST;
void init_d_list_node(DL_LINK_LIST * node, const int data)
{
node->data = data;
node->pre = NULL;
node->next = NULL;
}
DL_LINK_LIST * _d_list_add_head(DL_LINK_LIST *head, int data)
{
if (head == NULL)
{
printf("@_d_list_add_head, param head is null, can not trans continue!\n");
return head;
}
DL_LINK_LIST *node = (DL_LINK_LIST *)malloc(sizeof(DL_LINK_LIST));
if (NULL == node)
{
printf("Not enogh memory to get!\n");
return head;
}
init_d_list_node(node, data);
printf("@_d_list_add_head---->try to add %d.\n", data);
node->next = head;
head->pre = node;
head = node;
return head;
}
DL_LINK_LIST * _d_list_add_tail(DL_LINK_LIST *head, int data)
{
printf("@_d_list_add_tail---->try to add %d.\n", data);
if (head == NULL)
{
printf("@_d_list_add_tail, param head is null, can not trans continue!\n");
return head;
}
DL_LINK_LIST *node = (DL_LINK_LIST *)malloc(sizeof(DL_LINK_LIST));
if (NULL == node)
{
printf("Not enogh memory to get!");
return head;
}
init_d_list_node(node, data);
DL_LINK_LIST *p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = node;
node->pre = p;
return head;
}
bool _d_list_insert_mid(DL_LINK_LIST * p, int data)
{
if (p == NULL)
{
printf("@_d_list_insert_mid, param in node is NULL, can not do insert\n");
return false;
}
DL_LINK_LIST *node = (DL_LINK_LIST *)malloc(sizeof(DL_LINK_LIST));
if (NULL == node)
{
printf("Not enogh memory to get!");
return false;
}
init_d_list_node(node, data);
printf("@_d_list_insert_mid---->try to insert %d.\n", data);
node->pre = p->pre;
node->pre->next = node;
node->next = p;
p->pre = node;
return true;
}
DL_LINK_LIST * _d_list_insert_by_data(DL_LINK_LIST *head, int data)
{
if (head == NULL)
{
printf("@_d_list_insert_by_data, param head is null, can not trans continue!\n");
return head;
}
DL_LINK_LIST *p = head;
while (p->next != NULL &&(p->data <data))
{
p = p->next;
}
printf("@_d_list_insert_by_data---->try to insert %d.\n", data);
if (p->data >= data)
{
if (p == head)
{
head = _d_list_add_head(head, data);
}
else
{
(void)_d_list_insert_mid(p, data);
}
}
else
{
printf("@_d_list_insert_by_data---->未找到比data大的数据 添加在末尾 try to insert %d.\n", data);
head = _d_list_add_tail(head, data);
}
return head;
}
DL_LINK_LIST * _d_list_del_head(DL_LINK_LIST *head)
{
if (head == NULL)
{
printf("@_d_list_del_head, the list head is NULL!\n");
return head;
}
DL_LINK_LIST *p = head;
head = head->next;
if (head != NULL)
{
head->pre = NULL;
}
free(p);
p = NULL;
return head;
}
DL_LINK_LIST * _d_list_del_tail(DL_LINK_LIST *head)
{
if (head == NULL)
{
printf("@_d_list_del_tail, the list head is NULL!\n");
return head;
}
DL_LINK_LIST *p = head;
while (p->next != NULL)
{
p = p->next;
}
if (p->pre != NULL)
{
p->pre->next = NULL;
}
if (p == head)
{
head = NULL;
}
free(p);
p = NULL;
return head;
}
bool _d_list_del_mid_node(DL_LINK_LIST *p)
{
if (p == NULL)
{
printf("@_d_list_del_mid_node, param in node is NULL, can not do insert\n");
return false;
}
p->pre->next = p->next;
p->next->pre = p->pre;
free(p);
p = NULL;
return true;
}
DL_LINK_LIST * _d_list_del_by_data(DL_LINK_LIST *head, int data)
{
if (head == NULL)
{
printf("@_d_list_del_by_data, param head is null, can not trans continue!\n");
return head;
}
printf("@_d_list_del_by_data---->try to delete %d.\n", data);
DL_LINK_LIST *p = head;
while (p->next != NULL && (p->data != data))
{
p = p->next;
}
if (p->data == data)
{
if(p == head)
{
head = _d_list_del_head(head);
}
else
{
if (p->next == NULL)
{
head = _d_list_del_tail(head);
}
else
{
(void)_d_list_del_mid_node(p);
}
}
}
else
{
printf("@_d_list_del_by_data, can not find the data %d\n", data);
}
return head;
}
void _d_list_del_all(DL_LINK_LIST *head)
{
printf("_d_list_del_all******************\n");
while (head != NULL)
{
head = _d_list_del_tail(head);
}
}
void _d_list_diaplay(DL_LINK_LIST* head)
{
printf("__d_list_diaplay******************\n");
DL_LINK_LIST *p = head;
int i = 1;
while (p != NULL)
{
printf("_d_list_ Num %d data = %d.\n", i, p->data);
p = p->next;
i++;
}
}
int main()
{
std::cout << "Hello World!\n";
DL_LINK_LIST *head = (DL_LINK_LIST*)malloc(sizeof(DL_LINK_LIST));
init_d_list_node(head, -1);
for (int i = 0; i < (13*10); i+= 13)
{
if(i <13*5)
{
head = _d_list_add_tail(head, i);
}
else
{
head = _d_list_add_head(head, i);
}
}
_d_list_diaplay(head);
head = _d_list_insert_by_data(head, 14);
head = _d_list_insert_by_data(head, 33);
head = _d_list_insert_by_data(head, 120);
_d_list_diaplay(head);
head = _d_list_del_by_data(head, 15);
head = _d_list_del_by_data(head, 26);
_d_list_diaplay(head);
head = _d_list_del_by_data(head, 14);
head = _d_list_del_by_data(head, 120);
_d_list_diaplay(head);
_d_list_del_all(head);
}
老师的代码
#include <stdio.h>
#include <malloc.h>
typedef struct DoubleLinkedNode{
char data;
struct DoubleLinkedNode *previous;
struct DoubleLinkedNode *next;
} DLNode, *DLNodePtr;
DLNodePtr initLinkList(){
DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
tempHeader->data = '\0';
tempHeader->previous = NULL;
tempHeader->next = NULL;
return tempHeader;
}
void printList(DLNodePtr paraHeader){
DLNodePtr p = paraHeader->next;
while (p != NULL) {
printf("%c", p->data);
p = p->next;
}
printf("\r\n");
}
void insertElement(DLNodePtr paraHeader, char paraChar, int paraPosition){
DLNodePtr p, q, r;
p = paraHeader;
for (int i = 0; i < paraPosition; i ++) {
p = p->next;
if (p == NULL) {
printf("The position %d is beyond the scope of the list.", paraPosition);
return;
}
}
q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
q->data = paraChar;
r = p->next;
q->next = p->next;
q->previous = p;
p->next = q;
if (r != NULL) {
r->previous = q;
}
}
void deleteElement(DLNodePtr paraHeader, char paraChar){
DLNodePtr p, q, r;
p = paraHeader;
while ((p->next != NULL) && (p->next->data != paraChar)){
p = p->next;
}
if (p->next == NULL) {
printf("The char '%c' does not exist.\r\n", paraChar);
return;
}
q = p->next;
r = q->next;
p->next = r;
if (r != NULL) {
r->previous = p;
}
free(q);
}
void insertDeleteTest(){
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, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'o', 1);
printList(tempList);
}
void basicAddressTest(){
DLNode tempNode1, tempNode2;
tempNode1.data = 4;
tempNode1.next = NULL;
tempNode2.data = 6;
tempNode2.next = NULL;
printf("The first node: %d, %d, %d\r\n",
&tempNode1, &tempNode1.data, &tempNode1.next);
printf("The second node: %d, %d, %d\r\n",
&tempNode2, &tempNode2.data, &tempNode2.next);
tempNode1.next = &tempNode2;
}
void main(){
insertDeleteTest();
basicAddressTest();
}