Linked Lists : Linked list « Data Structure « C Tutorial

Home
C Tutorial
1.Language
2.Data Type
3.String
4.printf scanf
5.Operator
6.Statement
7.Array
8.Function
9.Structure
10.Pointer
11.Memory
12.Preprocessor
13.File
14.Data Structure
15.Search Sort
16.Wide Character String
17.assert.h
18.ctype.h
19.math.h
20.setjmp.h
21.signal.h
22.stdio.h
23.stdlib.h
24.string.h
25.time.h
26.wctype.h
C / ANSI-C
C++
C++ Tutorial
Visual C++ .NET
C Tutorial » Data Structure » Linked list 
14.1.1.Linked Lists
  1. Each element in a linked list can be placed anywhere in memory.
  2. The elements are linked with each other using an explicit link field.
  3. To access the element you can use the starting pointer of the list.
# include <stdio.h>
   # include <stdlib.h>
   struct node
   {
       int data;
       struct node *link;
   };

   struct node *insert(struct node *p, int n){
      struct node *temp;
      if(p==NULL){
          p=(struct node *)malloc(sizeof(struct node));

         if(p==NULL) {
             printf("Error\n");
             exit(0);
         }
         p-> data = n;
         p-> link = p;
      else {
         temp = p;
         while (temp-> link != p)
            temp = temp-> link;
            temp-> link = (struct node *)malloc(sizeof(struct node));
            if(temp -> link == NULL){
               printf("Error\n");
               exit(0);
            }
            temp = temp-> link;
            temp-> data = n;
            temp-> link = p;
          }
          return (p);
   }
   void printlist struct node *p )
   {
      struct node *temp;
      temp = p;
      printf("The data values in the list are\n");
      if(p!= NULL)
      {
            do
            {
                printf("%d\t",temp->data);
                temp=temp->link;
            while (temp!= p);
      }
      else
         printf("The list is empty\n");
   }

   void main()
   {
      int n;
      int x;
      struct node *start = NULL ;

      start = insert start, );
      start = insert start, 2);
      start = insert start, );
      start = insert start, );                          

      printf("The created list is\n");
      printlist start );
   }
The created list is
The data values in the list are
1       2       3       4
14.1.Linked list
14.1.1.Linked Lists
14.1.2.Inserting a node by using recursive programs
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.