C 中的 LinkedList 元素交换问题

发布于 2024-10-28 22:03:47 字数 2723 浏览 4 评论 0原文

我编写了一个具有很多功能的程序,但我无法交换链表中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 ");
}

I wrote a program that has many functionalities but I can not swap 2 elements of 2 nodes in the linked list.Actually I can swap 2 nodes by changing their links but I can not swap 2 elements when the user requested 2 elements swapping.Here is my code without any swap operation.Again I have to say I want to do this swap operation by swapping 2 node elements not changing node links.How can I get rid of this problem?Any help will be appreciated.

#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 ");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2024-11-04 22:03:47

我尝试使用以下命令进行交换操作
用户输入数字。例如,
用户输入 12 和 45 以便
交换链表。我该怎么办
那?

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

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;
 }

之后,您调用搜索用户输入的两个值。
然后,您可以在不更改任何指针引用的情况下交换值,只需分配新值即可。当然,您需要检查是否确实找到了节点(不是 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;

I attempted swap operation with using
user entered numbers.For instance,the
user entered 12 and 45 in order to
swap in the linkedlist.How can I do
that?

I would suggest you extend the existing function with return values, (not tested just the idea)

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;
 }

After that you call search for both values entered by the user.
Then you swap the values in place without changing any pointer references simply be assigning new values. Of course you need to check whether the nodes were actually found (not 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文