【问题标题】:LinkedList Elements Swap Problem In CC中的LinkedList元素交换问题
【发布时间】:2011-03-31 17:25:35
【问题描述】:

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

【问题讨论】:

  • “这是我的代码,没有任何交换操作” - 你为交换操作尝试了什么?以此为起点会很棒!
  • 这是作业吗?请酌情标记。
  • 您需要精简代码以尽可能简单地重现您的问题。
  • 在单链表中交换元素的唯一方法是遍历整个链表。
  • @glowcoder 我尝试使用用户输入的数字进行交换操作。例如,用户输入 12 和 45 以交换链表。我该怎么做?

标签: c linked-list swap


【解决方案1】:

我尝试使用 using 进行交换操作 用户输入的数字。例如, 用户输入 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;

【讨论】:

    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 2012-01-18
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多