c語言寫入鏈,將文件讀入鏈表c語言

本文目錄一覽:

C語言鏈表寫入文件

你的程序有什麼問題,關P1指針什麼事?

你的save不能寫入文件嗎?

你的程序在哪裡釋放P1指針了?

C語言 怎麼把結構體數組寫入鏈表?

1.用頭插法。因為數據追加和刪除比較多,追加的話,頭插法可以直接插,用尾插降低了時間效率,刪除用兩個一樣。

2./*結構體定義*/

struct client{

char account[14];

char name[10];

char identity[20];

char address[15];

long int money;

};

/*鏈表結點定義*/

struct node{

struct client band_inf;

struct node *next;

};

應該把結構體結點定義成鏈表的成員,這樣鏈表才對。如果像你那樣定義的話,完全不用定義結構體,鏈表就搞定了。因為你在鏈表裏面把結構的成員都又定義了。

3.

1),定義結點:p1=(struct node*)malloc(sizeof(struct node)) ;表示定義一個node型的結點指針

使用,這個要看具體怎麼用了。比如說刪除searchp,priorp是searchp的前面一個結點,這樣寫

priorp-next=searchp-next;

delete searchp; 即可

插入newnode在searchp的後面,可以這樣:

newnode-next=searchp-next;

關於C語言文件數據寫入鏈表的問題

#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”);

return FALSE;

}

printf(“The contents of File are:\n”);

while(!feof(fpFile))

putchar(fgetc(fpFile));//證明fprintf()輸出到文件中的絕對是字符串

if(NULL != fpFile)

fclose(fpFile);

return TRUE;

}

void Destroy(Linklist L)

{

Node *head =L;

while (head)

{

Node* temp = head;

head = head-next;

free(temp);

}

}

int main()

{

char* strName = “test.txt”;

Linklist L;

InitLinklist(L);

CreateLinklist(L);

WriteLinklistToFile(strName, L);

ReadFromFile(strName);

Destroy(L);

return 0;

}

如何用c語言將文件中的數據寫入鏈表中

sw是我鏈表的首地址

fp是文件的指針

下面定義鏈表類型:num域存放的是int型數據,可根據你的情況來改變。

typedef

struct

node{

int

num;

struct

node

*next;

}node;

p

指向鏈表中的首元結點

while(p!=null){

fprintf(fp,

“%d,%s”,

p-num);

p=p-next;

}

其實,這樣操作是非常簡單的。

C語言怎麼從文件中將信息導入鏈表中

這個實現起來挺簡單的,就是寫起來麻煩點,不是一點代碼能寫完的!

1,建立一個鏈表,鏈表的節點struct定義為聯繫人信息的格式;

2,讀取文件,把內容存入鏈表;

3,查詢就根據姓名關鍵字遍歷鏈表就行了;

4,把內容存入文件;

首先建立鏈表,以及插入節點,查詢鏈表函數寫出來;

文件的讀取和存入到不是很麻煩;

—————————-下面是簡單的實現,可以輸入,存入文件,從文件讀取,打印,如果還想要實現其他的稍修改一下就行了——

#includestdio.h

#includestdlib.h

#includestring.h

#define MAX 80

struct stu{

int id;

char name[MAX];

struct stu* next;

};

typedef struct stu STU;

typedef STU* STUPTR;

STUPTR insert(STUPTR head, int id, char* name);

void print_st(STUPTR head);

void save_to_file(FILE* fp, STUPTR head);

FILE* crt_file( void );

STUPTR read_to_link(STUPTR head);

int main( void )

{

int choice;

int stu_id;

char stu_name[MAX];

STUPTR head;

FILE* fp;

clrscr();

head = NULL;

clrscr();

printf( “please enter the choice!\n” );

scanf( “%d”, choice );

while( choice != 9 ){

switch(choice){

case 1: printf(“enter the insert —-id—name!\n”);

scanf( “%d%s”, stu_id ,stu_name);

head = insert(head,stu_id,stu_name);

break;

case 2:print_st(head);

break;

case 3:puts(“save the info to file e:\stu_info.txt!\n”);

fp = crt_file();

save_to_file( fp, head);

puts( “save the data sucessful!\n”);

fclose(fp);

break;

case 4:puts(“read the file to link!\n”);

head = NULL;

head = read_to_link(head);

puts(“read the file successful!\n”);

break;

}

printf( “please enter the choice!\n”);

scanf( “%d”, choice );

}

}

STUPTR insert(STUPTR head, int id, char* name)

{

STUPTR new, cur;

cur = head;

new = malloc( sizeof( STU) );

new-id = id;

strcpy(new-name, name);

new-next = NULL;

if(cur == NULL)

head = new;

else{

while(cur-next != NULL){

cur = cur-next;

}

cur-next = new;

}

return head;

}

void print_st(STUPTR head)

{

STUPTR cur=head;

int i;

i=1;

if(cur == NULL){

printf( “has no student info!\n” );

}

else while(cur != NULL){

printf( “%d:——%d—%s\n”, i,cur-id,cur-name);

i++;

cur = cur-next;

}

}

void save_to_file(FILE* fp, STUPTR head)

{

int i;

STUPTR cur;

cur = head;

while(cur != NULL){

fprintf(fp, “%d %s\n”, cur-id,cur-name);

cur = cur-next;

}

}

FILE* crt_file(void)

{

FILE* fp;

fp = fopen( “e:\stu_info.txt”, “w” ); /*w is right or not*/

if(fp == NULL)

puts(“shit!!!!!!!!!!”);

return fp;

}

STUPTR read_to_link( STUPTR head)

{

int id;

char name[MAX];

FILE* fp;

fp = fopen(“e:\stu_info.txt”, “r”);

while( !feof(fp) ){

fscanf(fp, “%d%s”, id, name );

head = insert(head, id, name);

}

return head;

}

c語言怎麼把文本信息出去寫入鏈隊,然後處理後保存到文本文件里?提供一個思路就可以了。謝謝大神們了。

你的描述有點不清楚噶。我大概說一下,把文本文件以數據流的方式讀出來,讀到文件尾。把讀出來的數據放到鏈表或者隊列,一個一個處理,處理一個就往文本文件裏面填一個。最後保存文件就可以了。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/308765.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2025-01-03 14:49
下一篇 2025-01-03 14:49

相關推薦

發表回復

登錄後才能評論