程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 單鏈表的小例子(三)鏈表讀取和保存,單鏈例子

單鏈表的小例子(三)鏈表讀取和保存,單鏈例子

編輯:關於C語言

單鏈表的小例子(三)鏈表讀取和保存,單鏈例子


#include <stdio.h> 
#include <malloc.h> 
#include <string.h> 

struct address 
{ 
	int a; 
	int b; 
	char c; 
	struct address *next; 
}; 

void SaveToFile(struct address *p, FILE *fp) 
{ 
	if(p != NULL) 
	{ 
		do 
		{ 
			fwrite(p, sizeof(struct address), 1, fp); 
			p = p->next; 
		} while(p != NULL); 
	} 
} 

int load(FILE *fp, struct address **head)
{
	int n=0; 
	struct address *p1,*p2; 
	*head = (struct address *) malloc(sizeof(struct address)); 
	memset(*head, 0, sizeof(struct address)); 
	if( fread(*head, sizeof(struct address), 1, fp) != 1) 
	{ 
		free(*head); 
		*head = NULL; 
		return(0); 
	} 
	p2 = *head; 
	n++; 
	while( !feof(fp)) 
	{ 
		p1 = (struct address *) malloc(sizeof(struct address)); 
		fread(p1, sizeof(struct address), 1, fp); 
		p2->next = p1; 
		p2 = p1; 
		n++; 
	} 
	p2->next = NULL; 
	return(n); 
} 

void main() 
{ 
	struct address *head; 
	struct address *Test[10] = {0}; 
	int i = 0; 
	FILE *fp = NULL; 

	for(i=0; i < 10; i++) 
	{ 
		Test[i] = (struct address *)malloc(sizeof(struct address)); 
		if(Test[i] != NULL) 
		{ 
			memset(Test[i], 0, sizeof(struct address)); 
			Test[i]->a = 65 + i; 
			Test[i]->b = 65 + i; 
		} 

	} 
	for(i = 0; i < 10; i++) 
	{ 
		if(i < 10) 
		{ 
			Test[i]->next = Test[i+1];
		} 
	} 
	if((fp = fopen("addrbook.txt", "wb")) != NULL) 
	{ 
		SaveToFile(Test[0], fp); 
		fclose(fp); 
	} 
	if((fp = fopen("addrbook.txt", "rb")) != NULL) 
	{ 
		load(fp, &head); 
	} 
}

  


C語言單向鏈表,怎往文件裡存入數據與讀取數據

花了我半個小時,給了寫了一個簡單的例子,以下是在vs2005下調試成功,test.txt為文件名,在當前目錄下。
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct Node
{
int num;
int score;
struct Node* next;
}Node, *Linklist;

void InitLinklist(Linklist* L) //初始化單鏈表,建立空的帶頭結點的鏈表
{
*L = (Node*)malloc(sizeof(Node));
(*L)->next = NULL;
}

void CreateLinklist(Linklist L) //尾插法建立單鏈表
{
Node *r, *s;
r = L;
int iNum, iScore;
printf("Please input the number and score:\n");
scanf("%d",&iNum);
scanf("%d",&iScore);
while(0 != iScore) //當成績為負時,結束輸入
{
s = (Node*)malloc(sizeof(Node));
s->num = iNum;
s->score = iScore;
r->next = s;
r =s;
printf("Please input the number and score:\n");
scanf("%d",&iNum);
scanf("%d",&iScore);
}
r->next = NULL; //將最後一個節點的指針域置為空
}

int WriteLinklistToFile(const char* strFile, Linklist L)
{
FILE *fpFile;
Node *head = L->next;
if(NULL == (fpFile = fopen(strFile,"w"))) //以寫的方式打開
{
printf("Open file failed\n");
return FALSE;
}

while(NULL != head)
{
fprintf(fpFile,"%d\t%d\n",head->num,head->score);
head = head->next;
}

if(NULL != fpFile)
fclose(fpFile);
return TRUE;
};

int ReadFromFile(const char* strFile)
{
FILE *fpFile;
if(NULL == (fpFile = fopen(strFile,"r"))) //以讀的方式打開
{
printf("Open file failed\n");
retur......余下全文>>
 

將a,b,c三個結點鏈接成一個單向鏈表,並輸出鏈表結點中的數據

如何鏈接?順序?逆序?遞增...
 

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