Thursday, July 22, 2010

Program to create linear linked list and print contents and total items( GTU Sem I Program No : 42)

/*Program to create a linear linked list interactively and print its
  content and total no of items in list
  Program No   : 42 of GTU Practice Programs SEM I
  Developed by : Malhar Vora
  Developed on : 20/07/2010
  Email        : vbmade2000@gmail.com
  Web Site     : http://malhar2010.blogspot.com
****************************************************************************/


#include

#define LIMIT 5

//structure to hold data and link to next node
struct node
{
   int data;
   struct node *next;
};


short count=0; //Gloab lcounter to count no of items in list
typedef struct node *listnode;

listnode root=NULL;

void insert(int newdata)
{
    listnode newnode,temp;
    newnode=(listnode)malloc(sizeof(listnode));
    newnode->data=newdata;
    temp=root;
    while(temp->next!=NULL)
    {
       temp=temp->next;
    }
    temp->next=newnode;
    newnode->next=NULL;
    count++;
}

void print()
{
   listnode temp;
   temp=root;
   printf("\nPrinting items");
   while(temp->next!=NULL)
   {
       temp = temp->next;
       printf("\n%d",temp->data);
   }
}

void printcount()
{
   printf("\nTotal %d items are stored in linked list",count);
}

void main()
{
   clrscr();
   insert(1);
   insert(2);
   insert(3);
   insert(4);

   print();

   printcount();

}

2 comments:

  1. I like your post & I will always be coming frequently to read more of your post. Thank you very much for your post once more.3


    embroidery service

    ReplyDelete
  2. Thanks Deepak.
    I will keep posting more articles and code samples.

    ReplyDelete

Note: Only a member of this blog may post a comment.