单项链表的创建、倒序

    技术2025-07-11  24

     

    头文件

     

     

    class myLink { public: myLink(void); ~myLink(void); static void print(myLink* link); static myLink* traverse(myLink* link); static myLink* createLink(int a[], int size); public: myLink* next; int data; }; 

     

    单项链表的创建

     

    myLink* myLink::createLink(int a[], int size) { if (size <= 0) return NULL; myLink *head = new myLink; head->data = a[0]; myLink *node = head; for (int i = 1; i< size ; i++) { myLink *nextNode = new myLink; nextNode->data = a[i]; node->next = nextNode; nextNode->pre = node; node = nextNode; } return head; } 

     

     

    单项链表的倒序

     

    myLink* myLink::traverse(myLink* head) { myLink* nextNode = head; myLink* preNode = NULL; myLink* curNode = head; while (curNode->next != NULL) { nextNode =curNode->next; curNode->next = preNode; preNode = curNode; curNode = nextNode; } return preNode; } 

    最新回复(0)