当前位置: 首页>>代码示例>>C++>>正文


C++ reverseList函数代码示例

本文整理汇总了C++中reverseList函数的典型用法代码示例。如果您正苦于以下问题:C++ reverseList函数的具体用法?C++ reverseList怎么用?C++ reverseList使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了reverseList函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: testReverse

void testReverse(void){

	printf("Testing reverse!\n");

	//reverse the list once...
	List l = setUpList();
	printList(l);
	reverseList(l);
	printList(l);


	assert(getElement(l, 0) == 4);
	assert(getElement(l, 1) == 7);
	assert(getElement(l, 2) == 8);
	assert(getElement(l, 3) == 1);
	assert(listLength(l) == START_LENGTH);

	//reverse it again!
	reverseList(l);
	printList(l);
	assert(getElement(l, 0) == 1);
	assert(getElement(l, 1) == 8);
	assert(getElement(l, 2) == 7);
	assert(getElement(l, 3) == 4);
	assert(listLength(l) == START_LENGTH);

	//testing that appended items are reversed as well
	append(l, 32);
	reverseList(l);
	assert(getElement(l, 0) == 32);
	assert(listLength(l) == START_LENGTH + 1);

	printf("Reversing passed!\n");
}
开发者ID:jake-waratahs,项目名称:linkedlistADT,代码行数:34,代码来源:testList.c

示例2: testReverseList

void testReverseList() {
    cout << endl;
    cout << "Test reverseList():" << endl;
    cout << "===================" << endl;

    char a[] = "a";
    char b[] = "b";
    char c[] = "c";
    char d[] = "d";
    char e[] = "e";
    char f[] = "f";
    char g[] = "g";

    Node<char*> head;
    head.data = a;
    head.next = nullptr;
    appendToList(&head, &b[0]);
    appendToList(&head, &c[0]);
    appendToList(&head, &d[0]);
    appendToList(&head, &e[0]);
    appendToList(&head, &f[0]);
    appendToList(&head, &g[0]);
    printList(&head);

    Node<char*>* rev = reverseList(&head);
    printList(rev);

    Node<char*>* orig = reverseList(rev);
    printList(orig);
}
开发者ID:RichardDKlein,项目名称:whiteboard-cpp,代码行数:30,代码来源:ReverseList.cpp

示例3: main

int main(int argc, char **argv)
{
    int i;
    int n;
    NodePtr node;
    JobPtr job;
    ListPtr list;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <list size> \n",argv[0]);
        exit(1);
    }
    n = atoi(argv[1]);

    list = createList();
    for (i=0; i<n; i++) {
        job = createJob(i, "cmd args");
        node = createNode(job);
        addAtFront(list, node);
    }

    printList(list);
    reverseList(list);
    printList(list);
    reverseList(list);
    printList(list);

    exit(0);
}
开发者ID:KelsonBall,项目名称:CS253-resources,代码行数:29,代码来源:SimpleTestList.c

示例4: reverseList

    ListNode *reverseBetween(ListNode *head, int m, int n) {
        if (head == NULL)
            return NULL;

        if (head->next == NULL || m == n)
            return head;

        m--; n--;

        if (m == 0)
        {
            head = reverseList(head, n - m + 1);
            return head;
        }

        int c = 0;
        ListNode* curNode = head;
        ListNode* tail = NULL;
        while (c < m)
        {
            assert(curNode);
            tail = curNode;
            curNode = curNode->next;
            c++;            
        }

        curNode = reverseList(curNode, n - m + 1);
        tail->next = curNode;

        return head;
    }
开发者ID:kennethhu418,项目名称:LeetCode_Practice,代码行数:31,代码来源:Reverse_Linked_List_II.cpp

示例5: isPalindrome2

    // O(n)-time, O(1)-space
    bool isPalindrome2(ListNode *head) {
        if (head == NULL) {
            return true;
        }

        // 计算中间节点
        ListNode *mid = findMiddleNode(head);

        // 反转后半段链表
        ListNode *reverseHead = reverseList(mid->next);

        // 连接前后两半段链表
        mid->next = reverseHead;

        // 比较前后两个半段链表
        // 注意:后半段链表节点个数 <= 前半段链表节点个数
        bool isPal = true;
        while (reverseHead != NULL) {
            if (head->val != reverseHead->val) {
                isPal = false;
            }
            head = head->next;
            reverseHead = reverseHead->next;
        }

        // 恢复原链表
        reverseHead = reverseList(mid->next);
        mid->next = reverseHead;

        return isPal;
    }
开发者ID:syawlaus,项目名称:Leetcode,代码行数:32,代码来源:main.cpp

示例6: delLesserNodes

/* Deletes nodes which have a node with greater value node on left side */
void delLesserNodes(struct Node **head_ref)
{
	/* 1) Reverse the linked list */
	reverseList(head_ref);
  /*  2) In the reversed list, delete nodes which have a node with greater value node on left side. 
      Note that head node is never deleted because it is the leftmost node.*/
	_delLesserNodes(*head_ref);
  /* 3) Reverse the linked list again to retain the original order */
	reverseList(head_ref);
}
开发者ID:KunjeshBaghel,项目名称:DS,代码行数:11,代码来源:delete_g4g_2.cpp

示例7: linkedListTest

void linkedListTest() {
    node * list = nullptr;
    print(list);
    std::cout << "↔";
    reverseList(list);
    print(list);
    for (const auto & elem : {5,1,3,2,4,8,6,9,7}) {
        appendList(list, elem);
        print(list);
        std::cout << "↔";
        reverseList(list);
        print(list);
    }
}
开发者ID:CCJY,项目名称:coliru,代码行数:14,代码来源:main.cpp

示例8: isPalindrome

 bool isPalindrome(ListNode* head) {
     if (head == nullptr || head->next == nullptr) return true;
     ListNode* slow = head;
     ListNode* fast = head;
     while (fast->next != nullptr && fast->next->next != nullptr) {
         fast = fast->next->next;
         slow = slow->next;
     }
     ListNode* right = reverseList(slow->next);
     slow->next = nullptr;
     bool result = compare(head, right);
     slow->next = reverseList(right);
     return result;
 }
开发者ID:jefferyyuan,项目名称:AlgorithmPractices,代码行数:14,代码来源:palindrome_linked_list.cpp

示例9: test

void test( int* arr, int size ) {
  node* root = newList( arr, size );
  print( root );
  reverseList( &root );
  print( root );
  freeList( root );
}
开发者ID:healexis,项目名称:jeremy,代码行数:7,代码来源:reverseLinkedList.c

示例10: reorderList

    void reorderList(ListNode* head) {
        if (head==NULL || head->next==NULL || head->next->next==NULL)
        {
            return ;
        }
        
        ListNode* l1=head;
        ListNode* l2=getMid(head);
        ListNode* reverseL2=reverseList(l2);
        
		ListNode* newhead=new ListNode(-1);
        ListNode* tail=newhead;
        while (l1!=NULL && reverseL2!=NULL)
        {
			tail->next=l1;
            l1=l1->next;
			tail=tail->next;
            tail->next=reverseL2;
			tail=tail->next;
            reverseL2=reverseL2->next;
        }
       if (l1!=NULL)
       {
		   tail->next=l1;
       }
	   if (reverseL2!=NULL)
	   {
		   tail->next=reverseL2;
	   }
	   head=newhead->next;
    }
开发者ID:dg8fv2010,项目名称:LeetCode,代码行数:31,代码来源:143.cpp

示例11: main

int main() {
    struct node * head   = NULL;
    struct node * second = NULL;
    struct node * third  = NULL;

    head   = (struct node *) malloc(sizeof(struct node));
    second = (struct node *) malloc(sizeof(struct node));
    third  = (struct node *) malloc(sizeof(struct node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    printf("Normal list order:\n");
    printList(head);
    printf("\nReversed order:\n");
    reverseList(&head);
    printList(head);

    return 0;
}
开发者ID:larsx2,项目名称:algorithms,代码行数:26,代码来源:reverse.c

示例12: reverseList

 ListNode* reverseList(ListNode* head) {
     if (!head || !(head->next)) return head;
     ListNode* tmp = reverseList(head->next);
     head->next->next = head;
     head->next = NULL;
     return tmp;
 }
开发者ID:zszyellow,项目名称:leetcode,代码行数:7,代码来源:solution.cpp

示例13: reverseList

 ListNode* reverseList(ListNode *head, int k) {
     ListNode *prev, *current, *next, *tail;
     current = head, prev = NULL, tail = head;
     int cnt = 0;
     while (current != NULL) {
         if (cnt > k)
             break;
         current = current->next;
         cnt++;
     }
     if (cnt < k)
         return head;
     cnt = 0;
     current = head;
     while (cnt < k && current != NULL) {
         next = current->next;
         current->next = prev;
         prev = current;
         current = next;
         cnt++;
     }
     if (current != NULL)
         tail->next = reverseList(current, k);
     return prev;
     
 }
开发者ID:XintongHu,项目名称:leetcode,代码行数:26,代码来源:ReverseNodesInK-Group.cpp

示例14: main

int main()
{
    node *listHead = create();
    if (!listHead)
        return 0;
    for (int i = 0; i < 10; i++)
        add(listHead, i);
    deleteNode(listHead, 5);
    insertNode(listHead, 5, 50);

    printf("Length = %d\n", length(listHead));
    printList(listHead);
    reverseList(listHead);
    printList(listHead);

    node *pMid;
    pMid = getMid(listHead);
    printf("Mid data is %d\n", pMid->data);
    add(listHead, 99);
    printList(listHead);
    pMid = getMid(listHead);
    printf("Mid data is %d\n", pMid->data);

    destroy(listHead);
    return 0;
}
开发者ID:codepower2,项目名称:ProgramerUP,代码行数:26,代码来源:single.cpp

示例15: reverseList

 ListNode* reverseList(ListNode* head){
     if(!head || !head->next) return head;
     ListNode* newHead = reverseList(head->next);
     head->next->next = head;
     head->next = NULL;
     return newHead ;
 }
开发者ID:iXuZhang,项目名称:Practice,代码行数:7,代码来源:Palindrome+Linked+List.cpp


注:本文中的reverseList函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。