交换给定循环链表中的两个相邻节点 C程序
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:
伪代码:
//assumed all node has different key, so searching node by key value
temp=head;
while(temp->data!=key)
begin
temp1=temp
temp=temp->next
End while
//node found which is to be interchanged
IF(temp=head)
temp1=temp->next
temp->next=temp1->next
temp1->next=temp
head=temp1
ELSE
temp2=temp->next
temp->next=temp2->next
temp2->next=temp
temp1->next=temp2
End IF-ELSE
End
C code:
C代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int data;
struct list *next;
}node;
void display(node *temp)
{
node *temp1=temp;
printf("\nNow the list is :\n%d->",temp->data);
temp=temp->next;
while(temp!=temp1)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d\n",temp1->data);
}
int main(){
node *head=NULL,*temp,*temp1,*temp2;
int choice,key;
do
{
temp=(node *)malloc(sizeof(node));
if(temp!=NULL)
{
printf("\nEnter the element in the list : ");
scanf("%d",&temp->data);
if(head==NULL)
{
head=temp;
}
else
{
temp1=head;
while(temp1->next!=head)
{
temp1=temp1->next;
}
temp1->next=temp;
}
temp->next=head;
}
else
{
printf("\nMemory not avilable...node allocation is not possible");
}
printf("\nIf you wish to add m ore data on the list enter 1 : ");
scanf("%d",&choice);
}while(choice==1);
display(head);
printf("\nEnter the data of the node which you want to exchange with it's next : ");
scanf("%d",&key);
temp=head;
while(temp->data!=key)
{
temp1=temp;
temp=temp->next;
}
if(temp==head)
{
temp1=temp->next;
temp->next=temp1->next;
temp1->next=temp;
head=temp1;
}
else
{
temp2=temp->next;
temp->next=temp2->next;
temp2->next=temp;
temp1->next=temp2;
}
display(head);
return 0;
}
Output
输出量
Enter the element in the list : 1
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 2
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 3
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 4
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 5
If you wish to add m ore data on the list enter 1 : 0
Now the list is :
1->2->3->4->5->1
Enter the data of the node which you want to exchange with it's next : 3
Now the list is :
1->2->4->3->5->1
翻译自: https://www.includehelp.com/c-programs/interchange-the-two-adjacent-nodes-in-a-given-circular-linked-list.aspx
还没有评论,来说两句吧...