程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C說話完成雙向鏈表

C說話完成雙向鏈表

編輯:關於C++

C說話完成雙向鏈表。本站提示廣大學習愛好者:(C說話完成雙向鏈表)文章只能為提供參考,不一定能成為您想要的結果。以下是C說話完成雙向鏈表正文


這個小代碼是我憑本身對指針和鏈表的懂得和熟悉,本身完成的,沒有參考其別人的代碼,假如有雷同的處所,那真的只是偶合,代碼我在ubuntu 15.04下測試經由過程,能夠存在許多毛病和破綻.

doublelist.c

/*************************************************************************
  > File Name: doublelist.c
  > Author: ChenYiLiang
  > Mail: [email protected] 
  > Created Time: Sat 21 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;
 
/*向鏈表中拔出數據*/
int insert_list(struct userdata *header, size_t position, char name[], size_t id);
/*刪除鏈表中指定的節點*/
int delete_node(struct userdata *header, size_t position);
/*修正指定地位的節點信息*/
int alter_node(struct userdata *header, size_t position, size_t id, char name[]);
/*查找鏈表中的數據*/
struct userdata *search_node(struct userdata *header, size_t position);
/*遍歷鏈表*/
int travel_list(struct userdata *header);
/*斷定鏈表是空*/
int isempty(struct userdata *header);
 
/*將鏈表構造寫入文件*/
int write_into_file(struct userdata *header, FILE *fp);
/*從文件讀取數據放到鏈表中*/
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表現默許拔出到鏈表的尾部*/
  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\n");
  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;
}
 
/* 拔出節點 */
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);
  /*當position == 0時,表現默許拔出到鏈表的尾部*/
  if(0 == position){
    if(NULL != temp_newuser -> next){
      while(NULL != temp_newuser -> next){
        temp_newuser = temp_newuser -> next;
      }
    }
  }
 
  /*當position > 1時則尋覓適合的地位拔出*/
  if(1 <= position){
    for(int i = 0; i <= position; i++){
      /*當履行此處的代碼時表現,鏈表曾經達到尾部或許是空鏈表*/
      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;
}
 
/*刪除鏈表中指定的節點*/
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++ ){
      /*當履行此處的代碼時表現,鏈表曾經達到尾部或許是空鏈表*/
      if(NULL == deleteNode -> next){
        break;
      }
      deleteNode = deleteNode -> next;
    }
 
  /**/
  deleteNode -> next -> previous = deleteNode -> previous;
  deleteNode -> previous -> next = deleteNode -> next;
  free(deleteNode);
 
  return 0;  
}
 
/*修正指定地位的節點信息*/
int alter_node(struct userdata *header, size_t position, size_t id, char name[]){
  int isEmpty = isempty(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++ ){
      /*當履行此處的代碼時表現,鏈表曾經達到尾部或許是空鏈表*/
      if(NULL == alterNode -> next){
        break;
      }
      alterNode = alterNode -> next;
    }
 
    alterNode -> userid = id;
    strncpy(alterNode -> username, name, 30);
 
  return 0;
}
 
/*查找鏈表中的數據*/
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;
}
 
/*遍歷鏈表*/
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 -> userid);
    printf("%s\n", travel -> username);
    if(NULL == travel -> next){
      break;
    }
  }  
 
  return 1;
}
 
/*斷定鏈表是空*/
int isempty(struct userdata *header){
  if(header -> next == NULL){
    return 0;
  }else{
    return 1;
  }
}
 
/*將鏈表構造寫入文件*/
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 userdata), 1, fp);
    if(NULL == move -> next){
      break;
    }
  }
  fclose(fp);
  fp = NULL;
  return 0;
}
/*從文件讀取數據放到鏈表中*/
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)){/*當讀取到文件的尾部時.跳出輪回*/
      break;
    }
    readfile -> next = newread;
    newread -> next = NULL;
    newread -> previous = readfile;
    readfile = newread;
  }
  fclose(fp);
  fp = NULL;
  return 0;
}

C說話完成雙向鏈表刪除節點、拔出節點、雙向輸入等操作

#include<cstdio>  
#include<cstdlib>  
typedef struct DoubleLinkedList 
{ 
  int data; 
  struct DoubleLinkedList *pre; 
  struct DoubleLinkedList *next; 
}DlinkedList_Node; 
//樹立鏈表  
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數據輸出停止\n"); 
        break; 
      } 
  } 
  p->next = NULL; 
  head = head ->next; 
  head->pre = NULL; 
  return head; 
} 
//次序、反序打印鏈表  
void printDLink(DlinkedList_Node *head) 
{ 
  DlinkedList_Node *p,*s; 
  p = head; 
  printf("正序輸入雙向鏈表:\n"); 
  while(p) 
  { 
    printf("%d ",p->data); 
    s = p; 
    p = p->next; 
  } 
  printf("\n 逆序輸入雙向鏈表: \n"); 
  while(s) 
  { 
    printf("%d ",s->data); 
    s = s->pre; 
  } 
  printf("\n \n"); 
} 
//刪除一個結點  
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("沒有找到想要刪除的數據\n"); 
  return head; 
} 
//拔出一個結點  
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)//比頭結點數據小,拔出到鏈表頭部  
  { 
    head = temp; 
    head->next = p;//此處p為本來的head  
    head->pre = NULL; 
    p->pre = head;//此處p為本來的head  
    return head; 
  } 
  while(p != NULL && i > p->data)//尋覓適合的拔出地位  
  { 
    p = p->next; 
  } 
  if(i < p->data)//在鏈表中央某處找到適合拔出地位  
  { 
    temp ->next = p; 
    temp ->pre = p->pre; 
    p ->pre->next = temp; 
    p ->pre = temp; 
    return head; 
  } 
  else//沒有找到適合的地位,只要將數據拔出到鏈表尾部  
  { 
    p->next = temp; //遍歷到鏈表尾部,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); 
} 
/***************************** 
運轉成果以下: 
please input the data: 
1991 
please input the data: 
1992 
please input the data: 
2013 
please input the data: 
2014 
please input the data: 
512 
please input the data: 
420 
please input the data: 
65535 
 
數據輸出停止 
正序輸入雙向鏈表: 
1991 1992 2013 2014 512 420 
 逆序輸入雙向鏈表: 
420 512 2014 2013 1992 1991 
 
正序輸入雙向鏈表: 
1012 1992 2013 2014 512 420 
 逆序輸入雙向鏈表: 
420 512 2014 2013 1992 1012 
 
******************************/ 

以上就是本文給年夜家分享的全體內容了,願望對年夜家加倍熟習C說話雙向鏈表可以或許有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved