交换给定循环链表中的两个相邻节点 C程序

喜欢ヅ旅行 2023-03-05 05:26 118阅读 0赞

Algorithm:

算法:

Function: largest_DLL()

函数:largest_DLL()

Input: A singly linked list whose address of the first node is stored in a pointer say head and the node we have to exchange with its next node.

输入:一个单链表 ,其第一个节点的地址存储在指针头(head)中 ,而我们必须与其下一个节点交换该节点。

Output: The singly linked list with interchanged node

输出:具有互换节点的单链表

Data structure used: Circular linked list where each node contains a data element say data, and the address of the immediate next node say next, with Head holding the address of the first node and the next part of the last node contains head.

使用的数据结构:循环链表,其中每个节点包含一个数据元素,例如data ,直接下一个节点的地址说next ,其中Head保留第一个节点的地址,而最后一个节点的下一部分包含head。

Pseudo code:

伪代码:

  1. //assumed all node has different key, so searching node by key value
  2. temp=head;
  3. while(temp->data!=key)
  4. begin
  5. temp1=temp
  6. temp=temp->next
  7. End while
  8. //node found which is to be interchanged
  9. IF(temp=head)
  10. temp1=temp->next
  11. temp->next=temp1->next
  12. temp1->next=temp
  13. head=temp1
  14. ELSE
  15. temp2=temp->next
  16. temp->next=temp2->next
  17. temp2->next=temp
  18. temp1->next=temp2
  19. End IF-ELSE
  20. End

C code:

C代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct list
  4. {
  5. int data;
  6. struct list *next;
  7. }node;
  8. void display(node *temp)
  9. {
  10. node *temp1=temp;
  11. printf("\nNow the list is :\n%d->",temp->data);
  12. temp=temp->next;
  13. while(temp!=temp1)
  14. {
  15. printf("%d->",temp->data);
  16. temp=temp->next;
  17. }
  18. printf("%d\n",temp1->data);
  19. }
  20. int main(){
  21. node *head=NULL,*temp,*temp1,*temp2;
  22. int choice,key;
  23. do
  24. {
  25. temp=(node *)malloc(sizeof(node));
  26. if(temp!=NULL)
  27. {
  28. printf("\nEnter the element in the list : ");
  29. scanf("%d",&temp->data);
  30. if(head==NULL)
  31. {
  32. head=temp;
  33. }
  34. else
  35. {
  36. temp1=head;
  37. while(temp1->next!=head)
  38. {
  39. temp1=temp1->next;
  40. }
  41. temp1->next=temp;
  42. }
  43. temp->next=head;
  44. }
  45. else
  46. {
  47. printf("\nMemory not avilable...node allocation is not possible");
  48. }
  49. printf("\nIf you wish to add m ore data on the list enter 1 : ");
  50. scanf("%d",&choice);
  51. }while(choice==1);
  52. display(head);
  53. printf("\nEnter the data of the node which you want to exchange with it's next : ");
  54. scanf("%d",&key);
  55. temp=head;
  56. while(temp->data!=key)
  57. {
  58. temp1=temp;
  59. temp=temp->next;
  60. }
  61. if(temp==head)
  62. {
  63. temp1=temp->next;
  64. temp->next=temp1->next;
  65. temp1->next=temp;
  66. head=temp1;
  67. }
  68. else
  69. {
  70. temp2=temp->next;
  71. temp->next=temp2->next;
  72. temp2->next=temp;
  73. temp1->next=temp2;
  74. }
  75. display(head);
  76. return 0;
  77. }

Output

输出量

  1. Enter the element in the list : 1
  2. If you wish to add m ore data on the list enter 1 : 1
  3. Enter the element in the list : 2
  4. If you wish to add m ore data on the list enter 1 : 1
  5. Enter the element in the list : 3
  6. If you wish to add m ore data on the list enter 1 : 1
  7. Enter the element in the list : 4
  8. If you wish to add m ore data on the list enter 1 : 1
  9. Enter the element in the list : 5
  10. If you wish to add m ore data on the list enter 1 : 0
  11. Now the list is :
  12. 1->2->3->4->5->1
  13. Enter the data of the node which you want to exchange with it's next : 3
  14. Now the list is :
  15. 1->2->4->3->5->1

翻译自: https://www.includehelp.com/c-programs/interchange-the-two-adjacent-nodes-in-a-given-circular-linked-list.aspx

发表评论

表情:
评论列表 (有 0 条评论,118人围观)

还没有评论,来说两句吧...

相关阅读