Advertisement
Guest User

listaordenada

a guest
Nov 26th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5.         int num;
  6.         struct node *next;
  7.         } Node, *NodePtr;
  8.         void printlist(NodePtr top)
  9.         {
  10.              printf("lista enlazada ORDENADA ");
  11.              getch();
  12.             /* printf("\t\t Entre a impirmir\n"); */
  13.              while(top!=NULL){    /*mientras el apuntador no sea nulo*/
  14.              printf("%d ",top->num);
  15.              top = top->next;  /* va al siguiente nodo*/
  16.              }
  17.          }
  18.          NodePtr insertlist (NodePtr top, int n)
  19.  
  20.          {
  21.             NodePtr nuevo,p ,pp;
  22.             nuevo =malloc(sizeof(Node));
  23.             nuevo ->num =n;
  24.             if(top == NULL){
  25.                 nuevo -> next =NULL;
  26.                 return nuevo;
  27.  
  28.             }
  29.             else
  30.                 pp=top;
  31.                 p = top-> next;
  32.                    while(p != NULL && n> p -> num){
  33.                     pp =p;
  34.                     p= p-> next;
  35.                    }
  36.                    pp -> next= nuevo;
  37.                    nuevo -> next =p;
  38.                    return top;
  39.          }
  40.               int main()
  41.         {
  42.             int n;
  43.             NodePtr top;
  44.             clrscr();
  45.             printf("\n\n\t\t lista enlazada ORDENADA\n");
  46.  
  47.             top = insertlist (top, n);
  48.             printlist( top);
  49.  
  50.             top = NULL;
  51.             printf("dar un numero para ingresar en la lista \n\t para salir ingrese cero\n");
  52.             if (scanf("&d", &n) != 1) n= 0;
  53.             printf("le! n = %d\n",n );
  54.  
  55.             while(n !=0){
  56.                 top= insertlist(top, n);
  57.                 printf("\t\t Dame el siguiente numero\t\t\n");
  58.                 if (scanf("%d",&n)!= 1) n=0;
  59.             }
  60.             if (top ==NULL)
  61.                 printf("no se dieron numeros\n");
  62.             else
  63.             {
  64.                 printf("los numeros en orden ascendente son: \n\n");
  65.                 printlist( top);
  66.                 getch();
  67.             }
  68.            
  69.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement