[数据结构]链表创建、打印、反转

  1. struct _Node
  2. {
  3.     int data;
  4.     _Node *next;
  5. };
  6. typedef struct _Node Node;
  7. /*****************************************************
  8.     Test 3: Node *CreateList(Node *head) 
  9. *****************************************************/
  10. Node *CreateList() 
  11. {
  12.     Node *head = NULL;
  13.     Node *cur = NULL;
  14.     Node *pre = NULL;
  15.     
  16.     cur=pre=head=(Node*)malloc(sizeof(Node));
  17.     cout<<"Please input the first node:";
  18.     cin>>cur->data;
  19.     cur->next=NULL;/*将新节点的指针置为空*/
  20.     while(cur->data != 0)/*输入节点的数值不等于0*/
  21.     {
  22.         pre->next=cur;/*非空表,接到表尾*/
  23.         pre=cur;
  24.         cur=(Node*)malloc(sizeof(Node));//申请下一个节点 
  25.         cout<<"Please input next node(zero to exit):";
  26.         scanf("%d",&cur->data);/*输入节点的值*/
  27.     }
  28.     pre->next = NULL; /*输入cur->data = 0, 抛弃*/
  29.     return head;/*返回链表的头指针*/
  30. }
  31. /*****************************************************
  32.  Test 4: void PrintfList(Node *head) 
  33. *****************************************************/
  34. void PrintList(Node *head)
  35. {
  36.  Node *cur = head;
  37.  cout<<"List:";
  38.  if(cur == NULL)
  39.  {
  40.   cout<<"NULL"<<endl;
  41.   return;
  42.  }
  43.  while(cur != NULL)
  44.  {
  45.   cout<<"("<<cur->data<<")"<<"->";
  46.   cur = cur->next;
  47.  }
  48.  cout<<endl;
  49. }
  50. /*****************************************************
  51.  Test 5: Node * ReverseList(Node *head) 
  52. *****************************************************/
  53. Node *ReverseList(Node *head)
  54. {
  55.  if(head == NULL)
  56.   return NULL;
  57.  Node *cur,*pre,*nex = NULL;
  58.   
  59.  pre = head;
  60.  cur = head->next;
  61.  while(cur != NULL)
  62.  {
  63.   nex = cur->next;
  64.   cur->next = pre;
  65.   pre = cur;
  66.   cur = nex;
  67.  }
  68.  head->next = NULL;
  69.  head = pre;
  70.  return head;
  71. }
    • /*****************************************************
    •  Test 6: Node * ReverseListRecursion(Node *head) 
    • *****************************************************/
  72. Node *ReverseListRecursion(Node *head)
    {
     if(head == NULL || head->next == NULL)
      return head;
  73.  Node *newHead = ReverseListRecursion(head->next);
     head->next->next = head;
     head->next = NULL;
  74.  return newHead;
    }

调用

 

  1. /*****************************************************
  2.     Test : Node *CreateList(Node *head) 
  3.     *****************************************************/
  4.     Node *list = CreateList();
  5.     /*****************************************************
  6.     Test : void PrintfList(Node *head) 
  7.     *****************************************************/
  8.     cout<<"The List looks like this:"<<endl;
  9.     PrintList(list);
  10.     /*****************************************************
  11.         Test : Node * ReverseList(Node *head) 
  12.     *****************************************************/
  13.     //Node *reverselist = ReverseList(list);
  14.     //PrintList(reverselist); 
  15.     Node *reverselistRecursion = ReverseListRecursion(reverselist);
  16.     PrintList(reverselistRecursion);
  17.     System("pause");

截屏

链表反转可以通过修改节点的前驱和后继指针来实现。以下是一个示例代码实现: ```c #include <stdio.h> #include <stdlib.h> // 定义双链表节点 struct Node { int data; struct Node* prev; struct Node* next; }; // 反转链表 struct Node* reverse(struct Node* head) { struct Node* current = head; struct Node* temp = NULL; while (current != NULL) { // 交换当前节点的前驱和后继指针 temp = current->prev; current->prev = current->next; current->next = temp; // 向后移动 current = current->prev; } // 更新头节点指针 if (temp != NULL) { head = temp->prev; } return head; } // 打印链表 void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { // 创建链表 struct Node* head = (struct Node*)malloc(sizeof(struct Node)); struct Node* second = (struct Node*)malloc(sizeof(struct Node)); struct Node* third = (struct Node*)malloc(sizeof(struct Node)); head->data = 1; head->prev = NULL; head->next = second; second->data = 2; second->prev = head; second->next = third; third->data = 3; third->prev = second; third->next = NULL; printf("原始双链表:"); printList(head); // 反转链表 head = reverse(head); printf("\n反转后的双链表:"); printList(head); return 0; } ``` 这段代码首先定义了一个 `Node` 结构体来表示双链表的节点,其中包括一个数据域 `data`、一个指向前驱节点的指针 `prev`,以及一个指向后继节点的指针 `next`。 在 `reverse` 函数中,我们使用一个临时变量 `temp` 来交换当前节点的前驱和后继指针,然后将当前节点指针向后移动。反转完成后,更新头节点指针,确保它指向反转后的链表的头部。 最后,在 `main` 函数中创建一个简单的双链表,并调用 `reverse` 函数进行反转操作。通过调用 `printList` 函数来打印原始链表反转后的链表。 以上代码的输出结果应为: ``` 原始双链表:1 2 3 反转后的双链表:3 2 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值