C语言中的LinkedList元素交换问题

C语言中的LinkedList元素交换问题,c,linked-list,swap,C,Linked List,Swap,我写了一个程序,有很多功能,但我不能交换链表中2个节点的2个元素。实际上,我可以通过更改链接来交换2个节点,但当用户请求交换2个元素时,我不能交换2个元素。这是我的代码,没有任何交换操作。我必须再次说,我想通过交换2个元素来执行此交换操作节点元素不更改节点链接。如何解决此问题?如有任何帮助,将不胜感激 #include <stdio.h> #include <stdlib.h> struct node{ int data; struct node *next;

我写了一个程序,有很多功能,但我不能交换链表中2个节点的2个元素。实际上,我可以通过更改链接来交换2个节点,但当用户请求交换2个元素时,我不能交换2个元素。这是我的代码,没有任何交换操作。我必须再次说,我想通过交换2个元素来执行此交换操作节点元素不更改节点链接。如何解决此问题?如有任何帮助,将不胜感激

#include <stdio.h>
#include <stdlib.h>

struct node{
  int data;
  struct node *next;
     };
typedef struct node nodetype;

void insert(int ,struct node **);
void display(struct node *);
void search(int, struct node *);
void delete(int, struct node **);
int main(void){

    nodetype *p;
    p=NULL;
    int x=0,choice;
    int s_no,k,r_no;


    while(x!=1){
        printf("enter 1 for insert\n");
        printf("enter 2 for display\n");
        printf("enter 3 for search\n");
        printf("enter 4 for delete\n");

        printf("enter 0 for exit\n");
        fflush(stdout);
        scanf("%d",&choice);
        if(choice==1){
            printf("enter inserted  no\n");
            fflush(stdout);
             scanf("%d",&k);
            insert(k,&p);
        }

        else if(choice==2)
            display(p);
        else if(choice==3){
            printf("enter searched no\n");
            scanf("%d",&s_no);
            search(s_no, p);
        }
        else if(choice==4){
            printf("enter deleted no\n");
            scanf("%d",&r_no);
            delete(r_no,&p);
                }
        else
            printf("invalid choice\n");
    }
    return 0;
}
void display ( struct node *p)
{
    printf("the content is:\n");
    if(p==NULL)
        printf("the link is empty\n");
    while ( p != NULL )
    {
        printf ( "%d ", p -> data ) ;
        p = p -> next ;
    }
 printf ( "\n" ) ;
}

void search(int no, struct node *p){
   nodetype * loc;
   for(loc=p;loc!=NULL;loc=loc->next)
    {
     if(no==loc->data){
       printf("\nthe number exist in the list\n");
       return;
     }

    }
   printf("\nthe number is not exist in the \n");
 }
void insert(int x,struct node **p)
{
    struct node *r,*temp=*p;
    r = (struct node *)malloc ( sizeof (struct node)) ;
    r ->data = x ;
    r->next=NULL;
    if ( *p == NULL)
    {
        *p = r ;
    }
    else
    {
        while(temp->next!= NULL)
        {
                  temp=temp->next;
        }

     temp->next=r;
    }
}
void delete(int num, struct node **p){
  struct node *temp,*x;
  temp=*p;
  x= NULL;
  while (temp->next !=NULL){
    if(temp->data == num)
     {
      if (x==NULL)
       {
        *p = temp->next;
        free(temp);
        return;
       }
      else
       {
        x->next = temp->next;
        free(temp);
        return;
       }
     }
    x=temp;
    temp=temp->next;
  }
  printf(" No such entry to delete ");
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
typedef结构节点节点类型;
void insert(int,结构节点**);
无效显示(结构节点*);
无效搜索(int,结构节点*);
void delete(int,结构节点**);
内部主(空){
节点类型*p;
p=零;
int x=0,选择;
国际标准号,k,r;
while(x!=1){
printf(“输入1作为插入\n”);
printf(“输入2进行显示\n”);
printf(“输入3进行搜索\n”);
printf(“输入4表示删除”);
printf(“输入0退出\n”);
fflush(stdout);
scanf(“%d”,选择(&C);
如果(选项==1){
printf(“输入插入的编号”);
fflush(stdout);
scanf(“%d”和“&k”);
插入(k和p);
}
else if(选项==2)
显示器(p);
else if(选项==3){
printf(“输入搜索编号”);
scanf(“%d”和s_编号);
搜索(序号,p);
}
else if(选项==4){
printf(“输入已删除的否”);
scanf(“%d”和r_编号);
删除(r_编号和p);
}
其他的
printf(“无效选择\n”);
}
返回0;
}
无效显示(结构节点*p)
{
printf(“内容为:\n”);
if(p==NULL)
printf(“链接为空\n”);
while(p!=NULL)
{
printf(“%d”,p->data);
p=p->next;
}
printf(“\n”);
}
无效搜索(整数编号,结构节点*p){
节点类型*loc;
对于(loc=p;loc!=NULL;loc=loc->next)
{
如果(否==loc->data){
printf(“\n列表中存在编号\n”);
返回;
}
}
printf(“\n该编号不存在于\n”);
}
空插入(整数x,结构节点**p)
{
结构节点*r,*temp=*p;
r=(结构节点*)malloc(sizeof(结构节点));
r->data=x;
r->next=NULL;
如果(*p==NULL)
{
*p=r;
}
其他的
{
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
温度->下一步=r;
}
}
void delete(int num,结构节点**p){
结构节点*temp,*x;
温度=*p;
x=零;
while(临时->下一步!=NULL){
如果(临时->数据==num)
{
如果(x==NULL)
{
*p=温度->下一步;
免费(临时);
返回;
}
其他的
{
x->next=temp->next;
免费(临时);
返回;
}
}
x=温度;
温度=温度->下一步;
}
printf(“无需删除的条目”);
}
我尝试使用 用户输入的数字。例如 用户输入12和45以便 在linkedlist中交换。我该怎么做 那个

我建议您使用返回值扩展现有函数(不只是测试想法)

然后调用search以搜索用户输入的两个值。 然后,在不更改任何指针引用的情况下就地交换值,只需分配新值即可。当然,您需要检查是否实际找到了节点(不是NULL)


“这是我没有任何交换操作的代码”-您尝试了什么交换操作?把它作为一个起点会很好!这是家庭作业吗?请在适当的情况下进行标记。您需要将代码精简到尽可能简单地再现您的问题。交换单链接列表中元素的唯一方法是遍历整个列表。@glowcoder我尝试使用用户输入的数字进行交换操作。例如,用户输入12和45以便在linkedlist中交换。我如何才能做到这一点?
nodetype * search(int no, struct node *p){
   nodetype * loc;
   for(loc=p;loc!=NULL;loc=loc->next)
    {
     if(no==loc->data){
       printf("\nthe number exist in the list\n");
       return loc;
     }

    }
   printf("\nthe number is not exist in the \n");
   return NULL;
 }
nodetype *node1;
nodetype *node2;

node1 = search( userInput1, &p );
node2 = search( userInput2, &p );

int tmp_data = node1->data;
node1->data = node2->data;
node2->data = tmp;