C Programming Tutorial

 
 
 
 

Image

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

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

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

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


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

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


void sort(struct node *start){
   struct 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;
    }

}

 
 
Didn't find what you were looking for? Find more on Program of link list