Just like last time, single linked list, only implement create and add...
NodeList.h
#ifndef NodeList_H #define NodeList_H #include <memory.h> #include <stdio.h> #include <stdlib.h> struct Node { void *pData; struct Node *next; struct Node *pre; }; typedef struct Node *Link; void CreateList(Link head,Link tail); void AddNodeToList(Link head,Link tail,int pos); void CreateData(char **mp_pData); #endif
NodeList.cpp
#include "NodeList.h" void CreateList(Link head,Link tail) { Link phead = head; Link ptail = tail; char *pdata; Link pNode; while (true) { CreateData(&pdata); //'#' means the end of the input. if (pdata[0] != '#') { pNode = (Link)malloc(sizeof(struct Node)*1); pNode->next = NULL; pNode->pre = NULL; pNode->pData = pdata; phead->next = pNode; pNode->pre = phead; pNode->next = ptail; ptail->pre = pNode; phead = phead->next; } else break; } } //add before the specific os position. void AddNodeToList(Link head,Link tail,int pos) { Link curNode = head; int iIndex = 0; while (iIndex <= pos) { curNode = curNode->next; iIndex++; } Link preNode = curNode->pre; Link tempNode = (Link) malloc(sizeof(struct Node)*1); //take care: since the inserted node data is type of double. double temDouble = 3.45; tempNode->pData = (double *)(&temDouble); preNode->next = tempNode; tempNode->pre = preNode; tempNode->next = curNode; curNode->pre = tempNode; } void CreateData(char **mp_pData) { *mp_pData = (char *)malloc(sizeof(10)); if (mp_pData) { memset(*mp_pData,0,10); printf("input the string:/n"); scanf("%s",*mp_pData); } }
code in the file including main function as following:
Link head = (Link)malloc(sizeof(struct Node)*1); Link tail = (Link)malloc(sizeof(struct Node)*1); head->pData = (char *)("head"); tail->pData = (char *)("tail"); head->next = tail; head->pre = NULL; tail->pre = head; tail->next = NULL; CreateList(head,tail); Link phead = head->next; while (phead && phead->next) { printf("%s/n",phead->pData); phead = phead->next; } AddNodeToList(head,tail,3); Link ptail = tail->pre; while (ptail && ptail->pre) { printf("%s/n",ptail->pData); ptail = ptail->pre; }
It has been about one week after graduating from college, miss my friends,
miss my classmates, miss my bros, and miss marine most.
I always could not concentrate on my work completely at my work time. All kinds
of emotions filled in my mind. But I just tried my best to consider, think about what
will be my future like. And the most important is I realize that I should attach more importance
to my basic knowledge such data structure and algorithms , because these can let me become
more confident . Okay, time up, over ...