Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int num;
- struct node *next;
- } Node, *NodePtr;
- void printlist(NodePtr top)
- {
- printf("lista enlazada ORDENADA ");
- getch();
- /* printf("\t\t Entre a impirmir\n"); */
- while(top!=NULL){ /*mientras el apuntador no sea nulo*/
- printf("%d ",top->num);
- top = top->next; /* va al siguiente nodo*/
- }
- }
- NodePtr insertlist (NodePtr top, int n)
- {
- NodePtr nuevo,p ,pp;
- nuevo =malloc(sizeof(Node));
- nuevo ->num =n;
- if(top == NULL){
- nuevo -> next =NULL;
- return nuevo;
- }
- else
- pp=top;
- p = top-> next;
- while(p != NULL && n> p -> num){
- pp =p;
- p= p-> next;
- }
- pp -> next= nuevo;
- nuevo -> next =p;
- return top;
- }
- int main()
- {
- int n;
- NodePtr top;
- clrscr();
- printf("\n\n\t\t lista enlazada ORDENADA\n");
- top = insertlist (top, n);
- printlist( top);
- top = NULL;
- printf("dar un numero para ingresar en la lista \n\t para salir ingrese cero\n");
- if (scanf("&d", &n) != 1) n= 0;
- printf("le! n = %d\n",n );
- while(n !=0){
- top= insertlist(top, n);
- printf("\t\t Dame el siguiente numero\t\t\n");
- if (scanf("%d",&n)!= 1) n=0;
- }
- if (top ==NULL)
- printf("no se dieron numeros\n");
- else
- {
- printf("los numeros en orden ascendente son: \n\n");
- printlist( top);
- getch();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement