當前位置:首頁 » 編程語言 » c語言鏈接存儲
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言鏈接存儲

發布時間: 2022-04-18 06:47:33

① 如何用c語言實現簡單的鏈式存儲結構

使用結構體:
typedef struct node{
int data;
struct node* next;
}Node;
就可以實現,以上是一個單鏈表的節點元素,每個節點的next指向下一個節點,就可以實現鏈式存儲了。遇到其他類似的問題,可以根據需要設置相應的指針域。

② 怎樣用c語言實現稀疏矩陣的帶行指針向量的鏈接儲存

double fMatrix[10][10]={...};
這是靜態數組表示矩陣,當然你可以使用鏈表的方式來存儲
一個鏈表表示一行,每行存儲鏈表的頭元素指針就行。。。

③ 在C語言中,存儲類別包括什麼

1、c語言中的存儲類型有static 、auto、extern、及register,函數默認的存儲類型應該是extern,意思是具有外部鏈接性的。一般來說,會通過extern來聲明函數。
2、比如下面的代碼,在1.c中定義一個函數,函數的聲明寫在1.h頭文件中,在2.c中通過添加1.h的頭文件,來聲明及調用函數f()。

//1.c
void f() { ; }
//1.h
extern void f();
//2.c
#include "1.h"
int main() { f();}

④ c語言用鏈表存儲自動存儲櫃,每一句什麼意思

1、鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態生成。每個結點包括兩個部分:一個是存儲數據元素的數據域,另一個是存儲下一個結點地址的指針域。 相比於線性表順序結構,鏈表比較方便插入和刪除操作。2、常式:

/**對鏈表的綜合操作*功能有建立,排序,插入,刪除,輸出*/#include<stdio.h>#include<malloc.h>typedef int ElemType;typedef struct NodeType{ElemType data;struct NodeType *next;} NodeType,*LinkType;LinkType create(){//建立鏈表,返回鏈表的首地址,頭結點沒有數據LinkType head,p1,p2;head=(LinkType)malloc(sizeof(NodeType));p1=head;while(p1->data!=0)//當data=0時鏈表結束{p2=p1;p1=(LinkType)malloc(sizeof(NodeType));printf("Enter student's information:\ndata=");scanf("%d",&p1->data);p2->next=p1;}p2->next=NULL;free(p1);return(head);}void output(LinkType head){//鏈表的輸出,接收鏈表的首地址head=head->next;while(head!=NULL){printf("data=%d\n",head->data);head=head->next;}}LinkType sort(LinkType head){//鏈表排序,接收鏈表首地址,返回鏈表首地址LinkType ph,p1;ElemType temp;ph=head->next;p1=head->next;while(p1->next!=NULL)//冒泡法{ph=head;while(ph->next!=NULL){if(ph->data>ph->next->data)//按data由小到大排序{temp=ph->data;ph->data=ph->next->data;ph->next->data=temp;}ph=ph->next;}p1=p1->next;}return(head);}LinkType del(LinkType head){//刪除結點,接收鏈表的首地址,返回鏈表的首地址ElemType DelData;LinkType ph,p;ph=head->next;printf("Enter the data you want to del:\nDelData=");scanf("%d",&DelData);while(ph!=NULL && ph->data!=DelData)//尋找要刪除的結點{p=ph;ph=ph->next;}if(ph==NULL)//沒有找到要刪除的結點{printf("Enter error!\n");return(head);}else{if(ph==head->next)//刪除頭結點{head->next=ph->next;}else//刪除其它結點{p->next=ph->next;}}free(ph);return(head);}LinkType insert(LinkType head){//插入結點,接收鏈表首地址,返回鏈表首地址LinkType ph,p,insert,temp;insert=(LinkType)malloc(sizeof(NodeType));printf("Enter the data you want to insert:\ndata=");scanf("%d",&insert->data);ph=head->next;while(ph!=NULL && ph->data < insert->data)//尋找插入的位置{p=ph;ph=ph->next;}if(head->next->data > insert->data)//插入頭部{temp=head->next;head->next=insert;insert->next=temp;}else//插入到其它地方{p->next=insert;insert->next=ph;}return(head);}void main(){LinkType head;head=create();output(head);printf("\n\n");head=sort(head);output(head);printf("\n\n");head=del(head);output(head);printf("\n\n");head=insert(head);output(head);}

⑤ 關於C語言中,鏈表數據的文件儲存和提取。

當把鏈表已經確定的時候,就可以依次存入文件。

和平時鏈表的遍歷一樣,每讀取一個節點內容就進行一次存入操作。

不過要注意幾個部分的檢查:

  1. 內存空間是否分配成功

  2. 是否成功存入到文件中

  3. 在工作完成之後,是否將以後不會用到的變數清空和刪除。


按照問題要求的代碼如下:

Consumer*read_list()

{

FILE*fp;

if((fp=fopen("CONSUMER.dat","rb"))==NULL)

{

printf("無法讀取CONSUMER.dat ");

returnNULL;

}

intsign;

Consumer*s,*p,*head;


head=(Consumer*)malloc(SIZE_C);

if(head==NULL)

{

printf("讀取失敗!內存空間申請不足! ");

returnNULL;

}

fseek(fp,0,SEEK_END);

if(ftell(fp)==0)

{

returnNULL;

}

p=head;

p->next=NULL;

while(feof(fp))

{

s=(Consumer*)malloc(SIZE_C);

//fread(s,SIZE_C,1,fp);

fread(s,sizeof(char),SIZE_C,fp);

p->next=s;

p=s;

p->next=NULL;

}

fclose(fp);

returnhead;

}//讀取文件到鏈表

intsave_consumer(Consumer*p)

{

FILE*fp;

Consumer*head;

head=p;//p為已經構建好的鏈表

//if((fp=fopen("CONSUMER.dat","ab+"))==NULL)

if((fp=fopen("CONSUMER.dat","wb"))==NULL)

{

printf("無法打開CONSUMER.dat! ");

return-1;

}

while(p!=NULL)

{

//fwrite(p,SIZE_C,1,fp);

fwrite(p,sizeof(char),SIZE_C,fp);

p=p->next;

}

fclose(fp);

return1;

}//儲存鏈表到文件

⑥ C語言編程 幫幫忙哦~~比較順序存儲和鏈接存儲兩種存儲結構的優缺點

(1) 分別用順序存儲和鏈接存儲實現線性表的基本操作
順序存儲:
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define List_Init_Size 10
#define ListIncrement 2

typedef char ET;
typedef ET * Ep;
typedef int Status;
typedef struct {
ET *elem;
int Length;
int ListSize;
} sqlist;

SqList La,Lb,Lc;

/*This program provide some functions for linear table.
Header file writen user are sqlist.h */

#include"stdio.h"
#include"alloc.h"
#include"sqlist.h"

SqList La,Lb,Lc;

/*Output the linear table emements. */
void printsq(SqList *L) {
int i;

printf("Size=%d Length=%d ",L->ListSize,L->Length);
for (i=0;i<L->Length;i++)
printf("%3c",L->elem[i]);
printf("\n");
}

Status InitList( SqList *L){
L->elem=(Ep)malloc(List_Init_Size*sizeof(ET));
if(L->elem==NULL)
exit(OVERFLOW);
L->Length=0;
L->ListSize=List_Init_Size;
return OK;
}

Status Init(SqList *L) {
int done=TRUE;
ET e;

InitList(L);
while (done) {
scanf("%c",&e);
if (e != '\n')
ListInsert(L,L->Length+1,e);
else done = FALSE;
}
}

/*Insert the ith data into a linear table */
Status ListInsert(SqList *L,int i,ET e){
ET *p,*q;

if (i<1 || i>L->Length+1) return ERROR;
if(L->Length >= L->ListSize){
p=(ET*)realloc(L->elem,(L->ListSize+ListIncrement)*sizeof(ET));
if (p==NULL)exit(OVERFLOW);
L->elem=p;L->ListSize+=ListIncrement;
}
q=L->elem+i-1;
for(p=L->elem+L->Length-1;p>=q;--p)
*(p+1)=*p;
*q=e;
++L->Length;
return OK;
}

Status Insert(SqList *L) {
int i,flag;
ET data;

printf("Please input the position and data: ");
scanf("%d",&i);
printf("Please input the data : ");
data = getche();
flag = ListInsert(L,i,data);
return flag;
}

/*Delete the ith data from a linear table*/
Status ListDelete(SqList *L,int i,ET *e) {
ET *p,*q;

}

Status Delete(SqList *L) {
int i,flag;
ET e;

}

/* Get element from a linear table. */
Status GetElem(SqList *L , int i , ET *e){

}

int LocateElem(SqList *L,ET e) {
int i,flag=FALSE;

}

/*Merge two linear table into one. If they have the same elements,
keep one of them and delete another. */
void Union( SqList *La ,SqList *Lb){
int i;
ET e;

}
}

/*Merge two sequence into one,don't change any elements in
these two linear tables. Join two sequence to one. */
void MergeList(SqList *L1,SqList *L2,SqList *L3) {
int i=0,k=0,j=0;
ET ai,bj;

while((i<L1->Length)&&(j<L2->Length)) {
ai=*(La.elem+i);
bj=*(Lb.elem+j);
if(ai<=bj) {
ListInsert(L3,++k,ai);
++i;
}
else {
ListInsert(L3,++k,bj);
++j;
}
}
while (i<L1->Length){
ai=*(L1->elem+i);
ListInsert(L3,++k,ai);
i++;
}
while(j<L2->Length){
bj=*(L2->elem+j);
ListInsert(L3,++k,bj);
j++;
}
}

/*List the Menu*/
void MenuList() {
printf("\n\n\n**************************\n");
printf(" 1 ------- Insert LA\n");
printf(" 2 ------- Insert LB\n");
printf(" 3 ------- Union LA and LB\n");
printf(" 4 ------- Delete LA\n");
printf(" 5 ------- Delete LB\n");
printf(" 6 ------- Merge LA and LB to LC\n");
printf(" 7 ------- print Linear\n");
printf(" 8 ------- Exit\n");
printf("**************************\n");
}

/*Select the menu */
void MenuSelect( ){
int select,done=1;

while (done) {
MenuList( );
printf("input the operating code : ");
scanf("%d",&select);
switch(select){
case 1: Insert(&La);break;
case 2: Insert(&Lb);break;
case 3: Union(&La,&Lb);break;
case 4: Delete(&La);break;
case 5: Delete(&Lb);break;
case 6: InitList(&Lc);
MergeList(&La,&Lb,&Lc);
printf("LC: ");printsq(&Lc);
break;
case 7: printf("LA: ");printsq(&La);
printf("LB: ");printsq(&Lb);break;
case 8: done=0;break;
default: printf(" ERROR\n");
}
}
}

main( ){
printf("Please input the init LA's element : ");
Init(&La) ;
printf("Please input the init LB's element : ");
Init(&Lb) ;
MenuSelect();
}



鏈接存儲:
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define List_Init_Size 10
#define ListIncrement 2

typedef char ET;
typedef ET * Ep;
typedef int Status;
typedef struct LNode{
ET data;
struct LNode *next;
}LNode, *LinkList;
/*LinkList La,Lb,Lc;*/

#include "stdio.h"
#include "alloc.h"
#include "llist.h"

/*Display the linklist's elements. */
void printlk(LinkList L) {

}

/*Creat linklist from head node. */
void CreatList( LinkList *L,int n){
int i;
LinkList p,q;
ET str[20],c;
p=(LinkList)malloc(sizeof(LNode));
p->next=NULL;
*L = q = p;
printf("Please input the data : ");
for (i=n;i>0;i--) {
p=(LinkList)malloc(sizeof(LNode));
c = getche(); /*scanf("%c",&c);*/
printf("\n\n");

p->data = c;
p->next = q->next;
q->next = p;
}
}

/*Init the linklist. */
void Init(LinkList *L) {
int n;
printf("Please input the number of the node : ");
scanf("%d",&n);
CreatList(L,n);
}

/* Get the value of element I; */
int GetElem(LinkList L,int i,ET *e) {
/* Add your own codes. */

}

/*Insert a element after I */
int ListInsert(LinkList *L,int i,ET e) {
/* Add your own codes. */

}

/*Delete the element I */
int ListDelete(LinkList *L,int i,ET *e)
{
/* Add your own codes. */
}

int Insert(LinkList *L) {
int i,flag;
ET data;

printf("Please input the position : ");
scanf("%d",&i);
printf("Please input the data : ");
data = getche(); /*scanf("%c",&data);*/
flag = ListInsert(L,i,data);
return flag;
}

Status Delete(LinkList *L) {
int i,flag;
ET e;

printf("Please input the number : ");
scanf("%d",&i);
flag = ListDelete(L,i,&e);
printf("Deleted element is %c\n",e);
return flag;
}

/*Find the element's position. */
int LocateElem(LinkList L,ET e) {
int i=0;
LinkList p;
p = L->next;
while (p) {
i++;
if (p->data == e) return i;
}
return 0;
}

/*Add the Lb after the La. */
void Union( LinkList *La ,LinkList *Lb){
LinkList pa,pb;

/* Add your own codes. */
}

/*Merge two sequence into one,don't change any elements in
these two link lists. Join two sequence to one. */
void MergeList(LinkList *L1,LinkList *L2,LinkList *L3) {
LinkList pa,pb,pc;

/* Add your own codes. */
}

/*List the Menu*/
void MenuList() {
printf("\n\n\n==========================\n");
printf(" 1 ******* Insert LA\n");
printf(" 2 ******* Insert LB\n");
printf(" 3 ******* Delete LA\n");
printf(" 4 ******* Delete LB\n");
printf(" 5 ******* Union LA and LB\n");
printf(" 6 ******* Merge LA and LB to LC\n");
printf(" 7 ******* print LinkList\n");
printf(" 8 ******* Exit\n");
printf("==========================\n");
}

/*Select the menu */
void MenuSelect(LinkList *La,LinkList *Lb){
int select,done=1;
LinkList Lc;

while (done) {
MenuList( );
printf("input the operating code : ");
scanf("%d",&select);
switch(select){
case 1: Insert(La);break;
case 2: Insert(Lb);break;
case 3: Delete(La);break;
case 4: Delete(Lb);break;
case 5: Union(La,Lb);break;
case 6: MergeList(La,Lb,&Lc);
printf("LC: ");printlk(Lc);
break;
case 7: printf("LA: ");printlk(*La);
printf("LB: ");printlk(*Lb);
break;
case 8: done=0;break;
default: printf(" ERROR\n");
}
}
}

main( ){
LinkList La,Lb;

printf("LA ");
Init(&La) ;
printlk(La);
printf("LB ");
Init(&Lb) ;
printlk(Lb);
MenuSelect(&La,&Lb);
}


(2) 比較兩者的優缺點,並說明兩者的適用場合
順序存儲:
要求存儲的空間物理上連續,但是可以直接完成查找。
鏈接存儲:
不用物理相鄰,邏輯上採用指針連續,儲存要求不高,但是查找要花更多的時間,修改很方便。

所以,在建立的線性表不需要進行大量的修改,需要查找的時候,就用順序存儲;不經常查找,但是經常進行修改的時候,就用鏈式存儲。

自己的理解,應該正確~

⑦ c語言編程 關於順序存儲與鏈式存儲

<p></p>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
node
{
int
data;
node
*next;
};
node
*create(int
a[],int
len)
{
int
i;
node
*head=new
node;
head->data=a[0];
node
*p=head;
node
*q;
for(i=1;i<len;i++)
{
q=new
node;
q->data=a[i];
p->next=q;
p=q;
}
p->next=NULL;
return
head;
}
node
*
insert(node
*head,int
k)
{
node
*temp=new
node;
temp->data=k;
temp->next=head;
head=temp;
return
head;
}
node
*dele(node
*head,int
m)
{
int
i;
node
*p=head;
node
*x=new
node;
node
*y=new
node;
if(m==1)
{
node
*q=head;
head=head->next;
free(q);
}
else
{
for(i=1;i<m;i++)
{
x=p;
p=p->next;
y=p->next;
}
x->next=y;
free(p);
}
return
head;
}
void
main()
{
int
a[10]={1,2,3,4,5,6,7,8,9,10};
int
len=10;
node
*head=new
node;
head=create(a,len);
node
*p=head;
printf("原數組為:");
while(p!=NULL)
{
printf("%d
",p->data);
p=p->next;
}
printf("\n輸入要插入的元素:");
int
k;
scanf("%d",&k);
head=insert(head,k);
p=head;
printf("增加元素後的數組為:");
while(p!=NULL)
{
printf("%d
",p->data);
p=p->next;
}
printf("\n要刪除的元素位置為:");
int
m;
scanf("%d",&m);
head=dele(head,m);
p=head;
printf("刪除元素後的數組為:");
while(p!=NULL)
{
printf("%d
",p->data);
p=p->next;
}
}<p>此處為鏈表實現的方式,鏈表的好處在於內存不必連續,並且順序存儲
</p>
<p>順序存儲結構的特點是:連續的內存,隨機存儲。</p>

⑧ (C語言)線性表順序存儲與鏈接存儲的建立,插入,刪除,搜索,替換與輸出

http://blog.csdn.net/andrew_yt/article/details/8023709
這個是線性存儲的操作
http://blog.csdn.net/andrew_yt/article/details/8024771
這個是單鏈表的操作

⑨ c語言如何用文件存儲數據

具體操作步驟如下:

1、首先,創建一個新文件夾,在該文件夾中創建一個文檔,如下圖所示,然後進入下一步。

⑩ 用C語言實現刪除鏈接存儲線性表節點的源程序

oc(sizeof(struct studinf))
表示開辟一段strunt studinf大小的內存空間,並把這空間的首地址賦值給p1

head=p1;p2=p1;//p2指向第一個空間
把p1指向的空間地址賦值給p2

p2->next=p1;//此時的p1是指向開辟的第二個空間了
是連接兩個開辟空間的語句,通過*next指針連接的;
把p1的值賦值給p2的next,使得p2可以用過next訪問後繼節點p1;
這樣就實現了兩個節點的連接