『壹』 c語言中怎麼根據文件建立一個鏈表
依次是編號
名字
數據是么?你需要先建立一個creat.txt文件,然後對文件進行操作~!
頭文件用#include<stdio.h>
#include<stdlib.h>這兩個,然後定義個文件指針
FILE
*fp;
用fopen("creat.txt","r")進行寫入,寫入用fscanf(fp,"%f%s%f",&id,&name,&grade);操作完成關閉文件fclose(fp);這樣就行了~!在鏈表裡插入這些東西就可以進行寫入了。。具體還是靠自己寫,要考試了,具體程序沒有時間給你寫。。
『貳』 C語言中怎樣建立鏈表
參考以前寫的這個吧,寫的不好,你修改吧
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define CD sizeof(struct Biao)
struct Biao
{
int num;
struct Biao *next;
};
int m,x;
int main()
{
struct Biao *jianli();
void paixu(struct Biao *n);
void chashu(struct Biao *n);
void shanshu(struct Biao *n);
void qiupingjun(struct Biao *n);
void tuichu();
int n;
struct Biao *t;
printf("1.建立鏈表\n2.排序\n3.插數\n4.刪數\n5.求平均值\n");
printf("請輸入選項:\n");
for(;;)
{
scanf("%d",&n);
switch(n)
{
case 1:t=jianli();break;
case 2:paixu(t);break;
case 3:chashu(t);break;
case 4:shanshu(t);break;
case 5:qiupingjun(t);break;
case 6:tuichu();break;
default:printf("輸入錯誤!請重新輸入\n");
}
}
}
struct Biao *jianli()
{
int i,j;
printf("建立鏈表,請輸入數據:\n");
struct Biao *head,*p1,*p2;
p1=p2=(struct Biao * )malloc(CD);//if(p1==NULL)建立鏈表失敗
for(i=0;i<=100;i++)
{
if(i==0)
{
head=p1;
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
continue;
}
if(p1==NULL)
break;
else
{
scanf("%d",&p1->num);
if(p1->num==-1)
{
p2->num=NULL;
break;
}
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
}
}
if(head->next->num==NULL)
printf("您建立了一個空鏈表\n");
else
{
printf("共有數據:%d\n",i-1);
m=i-1;
printf("輸出鏈表:\n");
p1=head->next;
for(j=0;j<i-1;j++)
{
printf("%d ",p1->num);
p1=p1->next;
}
}
printf("\n");
return(head);
}
void paixu(struct Biao *n)
{
int i,j,a,temp;
a=m;
struct Biao *p1,*p2,*tp;
p2=p1=n->next;
printf("從小到大排序結果:\n");
for(i=0;i<a-1;i++)
{
p1=p2;
tp=p1;
for(j=i+1;j<a;j++)
{
if((p1->next->num)<(tp->num))
{
tp=p1->next;
}
p1=p1->next;
}
temp=tp->num;tp->num=p2->num;p2->num=temp;
p2=p2->next;
}
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
}
void chashu(struct Biao *n)
{
int a,i,j;
struct Biao *p1,*p2,*tp;
m=m+1;
x=m;
a=m;
p1=n;
printf("請插入數字:\n");
p2=(struct Biao *)malloc(CD);
scanf("%d",&p2->num);
p1=n->next;
for(i=0;i<a-1;i++)
{
if(p1->num<=p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(p1->num<p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(n->next->num>p2->num)
{
tp=n->next;
n->next=p2;
p2->next=tp;
break;
}
p1=p1->next;
}
p1=n;//
for(i=0;i<a-1;i++)
{
p1=p1->next;
}
if(p1->num<=p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
}//演算法不簡便
printf("插入後的數據:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("數據個數:%d\n",a);
}
void shanshu(struct Biao *n)
{
int a,i,j;
a=x;
struct Biao *p1,*p2;
printf("請輸入要刪除的數:\n");
scanf("%d",&j);
for(;;)
{
p1=n;
for(i=0;i<a;i++)
{
if(p1->next->num==j)
{
p2=p1->next;
p1->next=p1->next->next;
a-=1;
x-=1;
break;
}
p1=p1->next;
}
if(i==a)
break;
}
printf("結果:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("剩餘數據個數:%d\n",x);
}
void qiupingjun(struct Biao *n)
{
int s,i;
struct Biao *p1;
s=0;
p1=n->next;
for(i=0;i<x;i++)
{
s+=p1->num;
p1=p1->next;
}
printf("平均值為:%f\n",s*1.0/x);
}
void tuichu()
{
exit(0);
}
『叄』 c語言如何從文件讀入,並存放在鏈表中
//很簡單,給你個單鏈表的例子,首先定義鏈表成員的結構體
struct
filetext{
char
buf[BUFSIZE];
struct
filetext
*next;
};
//讀取文件,並插入進鏈表的函數,filename為要讀取的文件名,head為鏈表的頭節點,函數返回插入新節點後鏈表的頭節點
struct
filetext
*
readfile(char
*
filename,struct
filetext
*
head)
{
struct
filetext
*
new
=
(struct
filetext
*)malloc(sizeof(struct
filetext));//定義一個新成員,並給它分配空間
FILE
*
fp;//讀取文件的文件流
struct
filetext
*
p
=head;//定義一個p,用來尋找鏈表中最後一個節點
if((fp=(fopen(filename,"r+")))==NULL)
{
//如果打開文件失敗,返回head,並提示
printf("open
file
failure");
return
head;
}
//然後開始讀取文件,放到new的buf中
if(fread(new->buf,BUFSIZE,1,fp)<1)
{
//如果讀取失敗,提示,並返回head
printf("read
file
failure");
return
head;
}
fclose(fp);
//文件讀取完後,進行鏈表操作
if(!head)//如果傳進來的head是個空指針,那麼新指針就作為頭節點返回
{
new->next
=
NULL;
return
new;
}
while(p->next)
p
=
p->next;//把p移動到最後一個節點
p->next
=
new;//p的下一個節點為new
new->next
=
NULL;//new的下一個節點為空
return
head;
}
//這樣這個函數就完成了,你可以寫個主函數,定義一個頭節點,試下
『肆』 c語言中怎麼把打開的文本存入鏈表中啊
把你鏈表中的結構體保存到文本,下次打開以後一次讀文本的內容到結構體中,然後把這些結構體加入鏈表就可以了。如果文本是事先給你的,你可以先了解文件中數據的屬性,比如一個結構體中有哪些類型,然後按這些類型創建結構體,然後加入鏈表即可
『伍』 用c語言創建鏈表
void CreateList_H(Linklist L,int n)中的L是局部變數,你生成的頭結點L不能被返回,應該改為:
Linklist CreateList_H(int n)
調用方式和函數內部返回值都需要相應改動。
『陸』 C語言從文件中讀出數據構造成鏈表
文件i/o通過fgets來讀入整行(也就是一個struct的數據)
然後藉助sscanf或者strtok來分離數據並分別存入結構體
鏈表本身操作不困難吧,先有一個鏈表頭,然後通過malloc創建新的項
如果是一次性連續讀入數據(比如資料庫的初始化),用一個while循環來控制就好
『柒』 C語言如何創建單鏈表
C語言創建單鏈表如下:
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#include "iostream.h"
typedef struct node
{
intdata;
node * next;
}node , * List;
void create(int n)
{
int c;
List s,L;
L=(List)malloc(sizeof(node));
L->next=NULL;
printf("請輸入第1個數據:");
scanf("%d",&c);
L->data=c;
for(int i=2;i<=n;i++)
{
s=(List)malloc(sizeof(node));
printf("請輸入第%d個數據:",i);
scanf("%d",&c);
s->data=c;
s->next=L;
L->next =s;
}
printf("鏈表創建成功!");
}
void main()
{
int n;
printf("請你輸入鏈表的個數:");
scanf("%d",&n);
create(n);
}
『捌』 c語言創建鏈表
1、你使用了malloc函數但沒有導入頭文件malloc.h。
2、函數DATA *create(int n);沒有申明就直接調用。
(另外main函數中DATA* head;給個初值NULL,避免野指針。)
修改以上內容,親測可運行。
『玖』 編寫C語言程序實現從txt文件中讀取數據創建單鏈表
#include
<stdio.h>
#include
<stdlib.h>
typedef
int
DataType;
typedef
struct
node
{
DataType
data;
struct
node
*next;
}
LNode,*LinkList;
void
CreateList(LinkList
&L,char
*filename){
//創建帶頭結點的單鏈表
FILE
*fp;
if((fp=fopen(filename,"r"))==NULL){
printf("文件沒找到!\n");return;}
L=(LinkList)malloc(sizeof(LNode));//創建頭結點
LinkList
p=L;
while(!feof(fp)){
LinkList
q=(LinkList)malloc(sizeof(LNode));
DataType
e;
fscanf(fp,"%d",&e);//使用格式化讀取文件中的數據
q->data=e;p->next=q;p=q;
}
p->next=NULL;
fclose(fp);
}
int
main(){
LinkList
L;
CreateList(L,"1.txt");
LinkList
p=L->next;
while(p){//輸出單鏈表的數據
printf("%d\t",p->data);p=p->next;
}
printf("\n");
return
0;
}
//在vc++6.0下運行,源文件擴展名應為:.cpp,若為.c要修改函數參數
『拾』 怎樣創建一個線性鏈表(C語言)
/*線性鏈表的構建*/
#include<stdio.h>
#include<stdlib.h>
typedefstructLnode
{
intdata;
structLnode*next;
}Lnode;
intmain()
{
Lnode*H,*p1,*p2,*p3,*p4;
H=(Lnode*)malloc(sizeof(Lnode));
p1=(Lnode*)malloc(sizeof(Lnode));
p2=(Lnode*)malloc(sizeof(Lnode));
p3=(Lnode*)malloc(sizeof(Lnode));
p4=(Lnode*)malloc(sizeof(Lnode));
p1->data=132;
p1->next=p2;
p2->data=942;
p2->next=p3;
p3->data=158;
p3->next=182;
p4->data=231;
p4->next=NULL;
printf("%d,%d ",p1->data,p3->data);
printf("%d",p1->next->data);
return0;
}