C-language implementation of bidirectional linked list _c language

Source: Internet
Author: User
Tags readfile

This small code is my own to the pointer and linked list of understanding and understanding, my own implementation, no reference to other people's code, if there is the same place, it is really just a coincidence, code I test in Ubuntu 15.04 Pass, there may be many errors and loopholes.

Doublelist.c

/************************************************************************* > File name:doublelist.c > Author: Chenyiliang > Mail:chenyiliangex@163.com > Created time:sat Mar 2015 07:32:22 PM CST ******************* /#include <stdio.h> #include <stdlib.h> #include
  <string.h> struct userdata{int userid;
 
  Char username[30];
  struct UserData *previous;
struct UserData *next;
 
 
 
};
struct UserData *header;
size_t scanf_id;
 
Char scanf_name[30];
int Yesno;
int deleteposition;
int alterposition;
int Alterid;
 
Char altername[30];
 
int searchposition;
 
FILE *ptr_fpid;
/* Insert data into the list/int insert_list (struct UserData *header, size_t position, Char name[], size_t ID);
/* Delete the specified node in the list */int delete_node (struct UserData *header, size_t position);
/* Modify the node information at the specified location/int alter_node (struct UserData *header, size_t position, size_t ID, char name[]); /* Find the data in the list/struct UserData *search_node (struCT UserData *header, size_t position);
/* Traverse list/int travel_list (struct userdata *header);
 
/* Judgment list is empty/int isempty (struct userdata *header);
/* Write the list structure to file/int write_into_file (struct userdata *header, file *fp);
 
* * from the file read data into the list/int read_from_file (struct userdata *header, file *fp);
  int main () {struct UserData *header_node = (struct UserData *) malloc (sizeof (struct userdata));
  Header_node-> previous = NULL;
 
  Header_node-> next = NULL;
  Read_from_file (Header_node, ptr_fpid);
  Travel_list (Header_node);
  while (1) {//scanf ("%*[^\n]");
  scanf ("%*c");
  scanf ("%*[^\n]");
  printf ("Please input ID-");
  scanf ("%d", &scanf_id);
  scanf ("%*c");
  scanf ("%*[^\n]");
  printf ("Please input your name-");
 
  scanf ("%s", scanf_name);
 
  printf ("%d-%s\n\n", scanf_id, Scanf_name);
 
  IsEmpty (Header_node);
   
  /*0 indicates that the default is inserted into the tail of the list * * * insert_list (header_node, 0, Scanf_name, scanf_id);
  Write_into_file (Header_node, ptr_fpid);
 
 
    IsEmpty (Header_node); Printf ("Input anymore-");
    scanf ("%d", &yesno);
    if (Yesno = = 1) {break;
  } scanf ("%*c");
scanf ("%*[^\n]");
 
  Travel_list (Header_node);
  } getchar ();
  printf ("Delete position data-");
  scanf ("%d", &deleteposition);
 
Delete_node (Header_node, deleteposition);
printf ("Alter data for position-");
scanf ("%d", &alterposition);
printf ("Please inout new ID-");
scanf ("%d", &alterid);
printf ("Please input new name-");
scanf ("%s", altername);
  Alter_node (Header_node, Alterposition, Alterid, altername);
  Write_into_file (Header_node, ptr_fpid);
  Travel_list (Header_node);
  printf ("\ n \ nthe");
  printf ("Please input position to search-");
  scanf ("%d", &searchposition);
  struct UserData *searchdata = Search_node (Header_node, searchposition);
  printf ("%d\n", Searchdata-> userid);
  printf ("%s\n", Searchdata-> username);
return 0; /* Insert node/* int insert_list (struct UserData *header, size_t position, CHAr name[], size_t id) {struct userdata *temp_newuser = header;
 
  struct UserData *getmemory = (struct UserData *) malloc (sizeof (struct userdata));
  GetMemory-> userid = ID;
  strncpy (getmemory-> username, name, 30); * When position = 0 o'clock, indicates that the default is inserted into the tail of the list/if (0 = position) {if (null!= temp_newuser-> next) {while null!= Temp_ne
      Wuser-> Next) {Temp_newuser = Temp_newuser-> Next; }/* When position > 1 o'clock Find the right place to insert/if (1 <= position) {for (int i = 0; I <= position; i++) {/
      * When executing the code here, the list has reached the tail or is an empty list/if (null = = Temp_newuser-> next) {break;
    } temp_newuser = Temp_newuser-> Next;
  } getmemory-> previous = Temp_newuser;
    if (temp_newuser-> next = = NULL) {temp_newuser-> next = getmemory; 
  GetMemory-> next = NULL;
    }else{Temp_newuser-> Next-> previous = GetMemory;
    GetMemory-> next = Temp_newuser-> next; Temp_newuser-> Next = getmemory;
return 0;
  /* Delete the specified node in the list/int delete_node (struct UserData *header, size_t position) {int is_empty = IsEmpty (header);
    if (0 = is_empty) {printf ("This Si a empty list!\n\n");
  return-1;
 
    } struct UserData *deletenode = header;
        for (int i = 0; i < position i++) {/* When executing code here, the list has reached the tail or is an empty list/if (null = = Deletenode-> next) {
      Break
    } deletenode = Deletenode-> Next;
  }/**/deletenode-> next-> previous = Deletenode->;
  Deletenode-> Previous-> next = Deletenode-> next;
 
  Free (deletenode);  
return 0; /* Modify node information at the specified location/int alter_node (struct UserData *header, size_t position, size_t ID, char name[]) {int isempty = Isem
  Pty (header);
    if (0 = isempty) {printf ("This is a empty list\n\n");
  return-1; 
    } struct UserData *alternode = header; for (int i = 0; i < position i++) {/* When executing the code here, the list has reached its tail or is an empty list/if (null = = AlteRnode-> next) {break;
    } Alternode = Alternode-> Next;
    } alternode-> userid = ID;
 
  strncpy (Alternode-> username, name, 30);
return 0; 
  /* Find the data in the list/* struct UserData *search_node (struct UserData *header, size_t position) {int isempty = IsEmpty (header);
    if (0 = isempty) {printf ("This is a empty!\n");
  return NULL;
  } struct UserData *searchnode = header;
    for (int i = 0; i < position. i++) {if (NULL = = Searchnode-> next) {break;
  } Searchnode = Searchnode-> Next;
return searchnode;
  }/* Traversal list/int travel_list (struct userdata *header) {struct UserData *travel = header; if (NULL = = Travel-> next) {printf ("This is a empty list!!
    \ n ");
  return 1;
    for (travel = travel-> next;; travel = Travel-> next) {printf ("%d\n" travel->);
    printf ("%s\n", travel-> username);
    if (NULL = = Travel-> next) {break;
} return 1;}/* The judgment list is null/int isempty (struct UserData *header) {if (header-> next = NULL) {return 0;
  }else{return 1;
  }/* Write linked list structure to file/int write_into_file (struct userdata *header, file *fp) {fp = fopen ("Listdata", "WB");
  if (NULL = fp) {perror ("Open file failed when write into file!"), Exit (-1);
  printf ("Come into write!\n"); 
    for (struct UserData *move = header-> next;. move = Move-> next) {fwrite (move,sizeof struct), 1, FP);
    if (NULL = = Move-> next) {break;
  } fclose (FP);
  fp = NULL;
return 0;
  */* from file read data into the list/int read_from_file (struct userdata *header, file *fp) {struct UserData *readfile = header;
  fp = fopen ("Listdata", "RB");
    if (NULL = fp) {perror ("Open file failed when read-");
  return-1;
    while (1) {struct UserData *newread = (struct UserData *) malloc (sizeof (struct userdata));
    Fread (newread, sizeof (struct userdata), 1, FP); if (feof (FP)) {/* when read to the end of the file.
   Jump out of circulation * * break; } ReadFile-> next = newread;
    Newread-> next = NULL;
    Newread-> previous = ReadFile;
  ReadFile = Newread;
  Fclose (FP);
  fp = NULL;
return 0; }

C language implementation of two-way linked lists delete nodes, insert nodes, bidirectional output and other operations

#include <cstdio> #include <cstdlib> typedef struct DOUBLELINKEDLIST {int data; 
  struct Doublelinkedlist *pre; 
struct Doublelinkedlist *next; 
}dlinkedlist_node; 
  Establish a list of dlinkedlist_node* Createdlink () {Dlinkedlist_node *head,*p,*s; 
  int x; 
  Head = (dlinkedlist_node*) malloc (sizeof (Dlinkedlist_node)); 
  p = head; 
    while (1) {printf ("Please input the data: \ n"); 
    scanf ("%d", &x); 
      if (x!= 65535) {s = (dlinkedlist_node*) malloc (sizeof (Dlinkedlist_node)); 
      s->data = x; 
      s-> pre = P; 
      P->next = s; 
    P=s; 
        else {printf (\ n data input end \ n); 
      Break 
  } p->next = NULL; 
  Head = Head->next; 
  Head->pre = NULL; 
return head; 
  }//order, reverse-order Print List void Printdlink (Dlinkedlist_node *head) {Dlinkedlist_node *p,*s; 
  p = head; 
  printf ("Positive sequence output bidirectional list: \ n"); 
    while (p) {printf ("%d", p->data); 
    s = p; 
  p = p->next; } printf ("\ n reverse output doubly linked list: \ n"); 
    while (s) {printf ("%d", s->data); 
  s = s->pre; 
printf ("\ n"); 
  }//delete a node dlinkedlist_node* deletedlinkedlist_node (Dlinkedlist_node *head,int i) {Dlinkedlist_node *p; 
  p = head; 
    if (P->data = i) {head = p->next; 
    Head->pre = NULL; 
    Free (p); 
  return head; 
    while (p) {if (P->data = i) {P->pre->next = p->next; 
    P->next->pre = p->pre; 
    Free (p); 
    return head; 
  } p = p->next; 
  printf ("Did not find the data you want to delete \ n"); 
return head; 
  }//Insert a node dlinkedlist_node* insertdlinkedlist_node (Dlinkedlist_node *head,int i) {Dlinkedlist_node *p,*temp; 
  p = head; 
  temp = (dlinkedlist_node*) malloc (sizeof (Dlinkedlist_node)); 
  Temp->data = i; 
    if (I < p->data)//is smaller than the first node data, inserted into the head of the linked list = temp; 
    Head->next = p;//Here P is the original head head->pre = NULL; 
  P->pre = head;//here p for the original head return head; } 
  while (P!= NULL && i > P->data)//Find the appropriate insertion position {p = p->next; 
    if (I < p->data)//Find the appropriate insertion position somewhere in the middle of the list {temp->next = p; 
    Temp->pre = p->pre; 
    P->pre->next = temp; 
    P->pre = temp; 
  return head; 
    else//did not find a suitable location, only inserting data into the tail of the list {p->next = temp;//traversing to the tail of the list, P==null temp->pre = p; 
    Temp->next = NULL; 
  return head; 
  int main () {Dlinkedlist_node *head; 
  Head = Createdlink (); 
  Printdlink (head); 
  Head = Insertdlinkedlist_node (head,1012); 
  Head = Deletedlinkedlist_node (head,1991); 
Printdlink (head); /***************************** run results are as follows: Please input the data:1991 please input the data:1992 please input the D  
ata:2013 Please input the "data:2014" please input the data:512 please input the data:420 please input the data: 65535 data input end positive sequence output bidirectional list: 1991 1992 2013 2014 512 420 reverse output bidirectional list: 420 512 2014 2013 1992 1991 Positive Sequence output bidirectional list: 1012 1992 2013 2014 512 420 reverse output bidirectional list: 420 512 2014 2013 1992 1012 ******************************/  

The above is this article to share all the content, I hope to be more familiar with the C-language two-way linked list can help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.