C Tutorial/Data Structure/Linked list — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 17:21, 25 мая 2010

Inserting a node by using recursive programs

<source lang="cpp"># 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 = NULL;
     } else{
        p->link = insert(p->link,n);
     }
     return (p);
  }
  void printlist ( struct node *p )
  {
        printf("The data values in the list are\n");
        while (p!= NULL)
        {
           printf("%d\t",p-> data);
           p = p-> link;
        }
  }
  void main()
  {
        int n =10;
        int x;
        struct node *start = NULL ;
        while ( n-- > 0 )
        {
           start = insert ( start, n );
        }
        printf("The created list is\n");
        printlist ( start );
  }</source>
The created list is
The data values in the list are
9       8       7       6       5       4       3       2       1       0

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.


<source lang="cpp"># 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, 1 );
     start = insert ( start, 2);
     start = insert ( start, 3 );
     start = insert ( start, 4 );                          
     printf("The created list is\n");
     printlist ( start );
  }</source>
The created list is
The data values in the list are
1       2       3       4