locked
how to create link list.... RRS feed

  • Question


  • #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>

    struct node
    {
    int data;
    struct node *next;
    };

    void main(){
      node *create(),*head;
      void display(node *);
      int count(node *);
      void sort(node *);
      int key;
      clrscr();
      while(1){
          if(key==27)
         break;
          else{
           head=create();
           display(head);
           sort(head);
          }
          printf("\nPress Escape to return...");
          key=getch();
      }
    }

    node *create(void){
       node *start=NULL,*newl=NULL,*endl=newl;
       int take;
       while(1){
           printf("\nEnter -1 at end\n");
           printf("Enter data : ");
           scanf("%d",&take);
           if(take==-1)
          break;
           else{
           newl = (node *)malloc(sizeof(node));
           newl->data = take;
           if(start==NULL)
              start=newl;
           else
              endl->next = newl;
           endl=newl;
           endl->next=NULL;
           }
       }
       return(start);
    }


    void display(node *start){
      printf("\ndisplay\n");
      while(start!=NULL){
       printf("%d",start->data);
       if(start->next!=NULL)
          printf("==>");
       start=start->next;
      }
    }

    int count(node *start){
        int cnt=0;
        while(start!=NULL){
           cnt++;
           start = start->next;
        }
        return(cnt);
    }


    void sort(node *start){
       node *temp1=start,*temp2=start;
       int tmpdata;
        while(temp1!=NULL){
        while(temp2->next!=NULL){
            if(temp1->data > temp2->next->data){
            tmpdata           = temp2->next->data;
            temp2->next->data = temp1->data;
            temp1->data       = tmpdata;
            }
            temp2=temp2->next;
        }
        temp1=temp1->next;
        }

    }
    Monday, May 21, 2007 4:33 PM