代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
实现双向链表
作者:
chenylex
/ 发布于
2015/3/23
/
619
/************************************************************************* > File Name: doublelist.c > Author: ChenYiLiang > Mail: chenyiliangex@163.com > 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; }
试试其它关键字
双向链
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
chenylex
贡献的其它代码
(
1
)
.
实现双向链表
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3