current position:Home>How can I exchange those two numbers in order?

How can I exchange those two numbers in order?

2022-02-04 17:03:37 CSDN Q & A

The linear table L Chain storage structure linklist, Realize the operation of inserting elements into the ordered linear table
【 The sample input 】

5

1 2 3 4 5

3
【 Sample output 】

1 2 3 3 4 5

6

#include<iostream>#include<stdio.h>#include<stdlib.h>using namespace std;#define error 0#define ok 1#define overflow -2typedef int status;typedef int elemtype;typedef struct lnode{    elemtype data;// Data fields      struct lnode *next;// Pointer to the domain  }lnode, *linklist;// Lead node single linked list 3 Basic functions and 1 Auxiliary functions :status initlist(linklist &l) // initialization ;{  l=(linklist)malloc(sizeof(lnode));  l->next=NULL;  return ok;}status inputlist(linklist &l) // Input , The tail interpolation  ;{ int i,n ; linklist p,q;  //printf("please input the length of the linklist:");  scanf("%d",&n);  q=l;  //printf("please input the data of the linklist:");  for (i=n;i>=1;i--)  {      p=(linklist)malloc(sizeof(lnode));      scanf("%d",&p->data);      p->next=NULL;      q->next=p;      q=p;      }  return ok;}status listtraverse(linklist l)//  Output , Output data elements first 、 Rear output length ;{  int j; linklist p;  p=l->next; j=0;  //printf("the data of the linklist:");  p=l->next;  while (p!=NULL)   { printf("%d ",p->data);    p=p->next;    j++;  }  printf("\n");  //printf("the length of the linklist:");  printf("%d\n",j);  return ok;}status destroylist(linklist &l) // revoke ; { linklist p,q;   q=l;p=l->next;   while (p!=NULL)    { q->next=p->next;     free(p);     p=q->next;   }   free(l);   return ok; } status orderlistinsert(linklist &l,elemtype e) {     linklist p;     linklist s;     p=l->next;     while(p){         if(e<p->data){              s = (linklist)malloc(sizeof(lnode));// Generate new nodes                s->data = e;             s->next = p->next;             p->next = s;            return ok;             //s=p->data;             //p->data=e;            // p->next->data=s;         }         p=p->next;     }     return ok; }int main(){   linklist l;elemtype e;  initlist(l);  inputlist(l);  scanf("%d",&e);  orderlistinsert(l,e);  listtraverse(l);  return 0;}

img




Take the answer :

Revised as follows , For reference :

#include<iostream>#include<stdio.h>#include<stdlib.h>using namespace std;#define error 0#define ok 1#define overflow -2typedef int status;typedef int elemtype;typedef struct lnode{    elemtype data;// Data fields     struct lnode *next;// Pointer to the domain }lnode, *linklist;// Lead node single linked list 3 Basic functions and 1 Auxiliary functions :status initlist(linklist &l) // initialization ;{  l=(linklist)malloc(sizeof(lnode));  l->next=NULL;  return ok;}status inputlist(linklist &l) // Input , The tail interpolation  ;{  int i,n ;  linklist p,q;  //printf("please input the length of the linklist:");  scanf("%d",&n);  q=l;  //printf("please input the data of the linklist:");  for (i=n;i>=1;i--)  {      p=(linklist)malloc(sizeof(lnode));      scanf("%d",&p->data);      p->next=NULL;      q->next=p;      q=p;  }  return ok;}status listtraverse(linklist l)//  Output , Output data elements first 、 Rear output length ;{  int j;  linklist p;                                //p=l->next;  j=0;  //printf("the data of the linklist:");  p=l->next;  while (p!=NULL)   {    printf("%d ",p->data);    p=p->next;    j++;  }  printf("\n");  //printf("the length of the linklist:");  printf("%d\n",j);  return ok;}status destroylist(linklist &l) // revoke ;{   linklist p,q;   q=l;   p=l->next;   while (p!=NULL)   {     q->next=p->next;     free(p);     p=q->next;   }   free(l);   return ok;}status orderlistinsert(linklist &l,elemtype e){     linklist p,pr;     linklist s;     pr = l;     p=l->next;     while(p && p->data <e)     {         pr = p;         p = p->next;     }     s = (linklist)malloc(sizeof(lnode));// Generate new nodes      s->data = e;     s->next = pr->next;     pr->next = s;     return ok;}int main(){  linklist l;  elemtype e;  initlist(l);  inputlist(l);  listtraverse(l);  scanf("%d",&e);  orderlistinsert(l,e);  listtraverse(l);  return 0;}

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/02/202202041703350441.html

Random recommended