當前位置:首頁 » 編程語言 » 線性表的建立c語言

線性表的建立c語言

發布時間: 2022-06-07 02:20:08

『壹』 c語言中怎麼定義個線性表

線性表是數據結構
具體到c中實現
可以有多種方式
比較常見的是數組方式以及鏈表方式。

『貳』 怎樣創建線性表(C語言)

線性表是個抽象的概念,沒辦法直接創建抽象類型,需要具體的類型(比如數組,鏈表)。
比如創建數組:int array[100];
創建一個GList鏈表: GList * list = NULL; (空的)

『叄』 c語言中創建線性表問題

因為你的p沒有初始化,printf 裡面d與,後面的&。。不匹配
#include<stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100 //線性表存儲空間的初始分配量
#define LISTINCREMENT 10 //線性表存儲空間的分配量
typedef int ElemType[LIST_INIT_SIZE];
typedef struct {
ElemType *elem;
int length;
int listsize;
}sqlist;
typedef int Status;
int InitList_Sq(SqList L)
{
//構造一個空的線性表L。
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem){return 0;}
L.length=0;
printf("%d\n",L.length);
L.listsize=LIST_INIT_SIZE;
return 1;
}
int main()
{
int t=0;
SqList p={};
InitList_Sq(p);
printf("%d\n",p.length);
return 0;

『肆』 C語言創建一個線性表,然後輸出線性表,如何編寫程序

#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>

#defineOVERFLOW -2
#define OK 1
#define ERROR 0
#defineLIST_INIT_SIZE 100
#defineLISTINCREMENT 10

typedef intElemType;
typedef intStatus;
//定義順序存儲結構
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
//初始化順序表
StatusInitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem ) exit(ERROR);
L.length =0;
L.listsize =LIST_INIT_SIZE;
return OK;
}
//自定義創建順序表
voidCreate_SqList(SqList &L)
{
int c,i=0;
int *newBase;
printf("請輸入順序表元素:\n");
while((scanf("%d",&c))!=EOF)
{
if(i>=L.listsize) //自定義順序表大小超過初始化大小
{
newBase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
//為初始順序表以LISTINCREMENT大小重新增加存儲空間
if(!newBase)exit(OVERFLOW);
L.elem=newBase;
L.listsize+=LISTINCREMENT;
}
L.elem[i++]=c;
}
L.length=i;
printf("輸入的順序表元素:\n");
for(i=0;i<L.length;i++)
printf("%d ",L.elem[i]);
printf("\n");
}
//在指定位置插入元素
StatusListInsert(SqList &L,int i,ElemType e)
{
ElemType *p,*q,*newbase;
if(i<1||i>L.length+1)
{
printf("插入位置錯誤\n");
return(ERROR);
}
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
if(i==L.length) L.elem[i+1]=e;
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
//在指定位置刪除元素
StatusListDelete_Sq(SqList &L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1||i>L.length+1)
return ERROR;
p=&(L.elem[i-1]);
*e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length ;
return OK;
}

void main()
{
SqList L;
int m,n;
int location,element;
if(!InitList_Sq(L))
{
printf("初始化順序表失敗!\n");
exit(ERROR);
}
Create_SqList(L);
for(m=0;m<3;m++)
{
printf("輸入插入位置:");
scanf("%d",&location);
while(location>L.length+1||location<1)
{
printf("輸入位置錯誤,請重新輸入!\n");
scanf("%d",&location);
}
printf("插入元素:");
scanf("%d",&element);
if(!ListInsert(L,location,element))
{
printf("順序表插入失敗!\n");
exit(ERROR);
}
printf("插入順序表為:\n");
for(int i=0;i<=L.length -1;i++)
{
printf("%d ",L.elem[i]);
}
printf("\n新順序表一共有%d個元素。\n",L.length);
}
for(n=0;n<3;n++)
{
printf("輸入刪除位置:");
scanf("%d",&location);
while(location>L.length||location<1)
{
printf("輸入位置錯誤,請重新輸入!\n");
scanf("%d",&location);
}
if(!ListDelete_Sq(L,location,&element))
{
printf("刪除錯誤!\n");
exit(ERROR);
}
printf("被刪除的元素為:%d \n",element);

printf("被刪除後的順序表為:\n");
for(int j=0;j<=L.length-1;j++)
{
printf("%d ",L.elem[j]);
}
printf("\n新順序表一共有%d個元素。\n",L.length);
}
}
這個是我最近編寫的 順序表也是線性表的
這里還有鏈表的程序 用的話再傳給你

『伍』 數據結構中,怎麼用C語言構造線性表!

#define
OK
1
#define
ERROR
-1
#define
MAX_SIZE
100
typedef
int
Status
;
typedef
int
ElemType
;
typedef
struct
sqlist
{
ElemType
Elem_array[MAX_SIZE]
;
int
length
;
}
SqList
;
以上為線性表建立的相關定義
Status
Init_SqList(
SqList
*L
)
{
L->elem_array=(
ElemType
*
)malloc(MAX_SIZE*sizeof(
ElemType
)
)
;
if
(
!L
->
elem_array
)
return
ERROR
;
else
{
L->length=
0
;
return
OK
;
}
}
以上為線性表初始化函數
有以上兩部分可構造一個線性表

『陸』 怎樣創建一個線性鏈表(C語言)

可以用頭插法或尾插法
(下面用尾插法)
思想為:讓你輸入一串字元串,
為每個字元創建一個節點,添加到鏈表的後面.直到輸入的字元為@為止.
#include
<stdio.h>
#include
<malloc.h>
typedef
char
datatype;
typedef
struct
node
{
datatype
data;
struct
node
*next;
}linklist;
linklist
*p,*q,*head;
main()
{
char
c;
head
=
(linklist
*)malloc(sizeof(linklist));
head->next
=
NULL;
p
=
head;
c
=
getchar();
while(c
!=
'@')
{
q
=
(linklist
*)malloc(sizeof(linklist));
q->data
=
c;
q->next
=
NULL;
p->next
=
q;
p
=
p->next;
c
=
getchar();
}
}
可以在
main()
最後加上
for(p=head->next;
p!=NULL;
p=p->next)
{
printf("%5c",
p->data);
}
來測試結果,本人已經TC
2.0下面測試通過.

『柒』 數據結構 線性表 用c語言

#define MAXSIZE 100 //表中元素的最大個數

typedef int ElemType;//元素類型

typedef struct list{

ElemType elem[MAXSIZE];//靜態線性表

int length; //表的實際長度

}SqList;//順序表的類型名

『捌』 線性表的基本操作c語言實現

代碼如下:

頭文件:

2_1.h

#ifndef _2_1_H

#define _2_1_H

typedef void SeqList;

typedef void SeqListNode;

//創建線性表

SeqList * SeqList_Create(int capacity);

//銷毀線性表

void SeqList_DesTroy(SeqList * list);

void SeqList_Clear(SeqList* list);

int SeqList_Length(SeqList* list);

int SeqList_Capacity(SeqList* list);

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);

SeqListNode* SeqList_Get(SeqList* list, int pos);

SeqListNode* SeqList_Delete(SeqList* list, int pos);

#endif

源文件:

// 順序線性表.cpp : 定義控制台應用程序的入口點。

//

#include "stdafx.h"

#include <malloc.h>

#include <stdlib.h>

#include "2_1.h"

typedef unsigned int TSeqListNode;

typedef struct {

int len; //長度

int capacity;//總長度

TSeqListNode * node;//每個節點的指針

} TSeqList;

int main()

{

SeqList* list = SeqList_Create(5);//創建線性表

int i = 6;//賦值6個變數,已超過線性表最大值 5

int j = 1;

int k = 2;

int x = 3;

int y = 4;

int z = 5;

int index = 0;

SeqList_Insert(list, &i, 7); //將這6個變數插入線性表中

SeqList_Insert(list, &j, 0);

SeqList_Insert(list, &k, 0);

SeqList_Insert(list, &x, 0);

SeqList_Insert(list, &y, 0);

SeqList_Insert(list, &z, 0);

//遍歷

for(index=0; index<SeqList_Length(list); index++)

{

int* p = (int*)SeqList_Get(list, index);

printf("%d ", *p);

}

printf(" ");

//刪除操作

while( SeqList_Length(list) > 0 )

{

int* p = (int*)SeqList_Delete(list, 0);

printf("刪除了: %d ", *p);

}

SeqList_Clear(list);

SeqList_DesTroy(list);

system("pause");

return 0;

}

//創建線性表

SeqList * SeqList_Create(int capacity)

{

TSeqList* ret = NULL ;

if(capacity >= 0)

{

ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode)*capacity); //為線性表分配空間,包含結 //構體和節點的總大小

}

if(NULL != ret)

{

ret->len = 0;

ret->capacity = capacity;

ret->node = (TSeqListNode*)(ret + 1);//將節點指向上述分配到的空間的後部分

}

return ret;

}

//銷毀

void SeqList_DesTroy(SeqList * list)

{

free(list);

}

//清空

void SeqList_Clear(SeqList* list)

{

TSeqList * ret = (TSeqList*)list;

if(NULL != ret)

{

ret->len = 0;

}

}

//獲得線性表的長度

int SeqList_Length(SeqList* list)

{

TSeqList * ret = (TSeqList*)list;

int len = -1;

if(NULL != ret)

{

len = ret->len;

}

return len;

}

//線性表的總長度

int SeqList_Capacity(SeqList* list)

{

TSeqList * ret = (TSeqList*)list;

int capacity = -1;

if(NULL != ret)

{

ret->capacity = capacity;

}

return capacity;

}

//插入

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)

{

TSeqList * sList = (TSeqList*)list;

int i,ret = -1;

if((sList != NULL) &&(pos >= 0) && sList->capacity >= sList->len+1)

{

if(pos >= sList->len)

{

pos = sList->len;

}

for(i = sList->len; i > pos; i--)

{

sList->node[i] = sList->node[i-1];

}

sList->node[i] = (TSeqListNode)node;

++sList->len;

ret = 1;

}

return ret;

}

//獲得指定位置的節點

SeqListNode* SeqList_Get(SeqList* list, int pos)

{

TSeqList * sList = (TSeqList*)list;

TSeqListNode* node = NULL;

if(NULL != sList && pos>=0 && pos < sList->len)

{

node = (TSeqListNode*)sList->node[pos];

}

return node;

}

//刪除

SeqListNode* SeqList_Delete(SeqList* list, int pos)

{

TSeqList * sList = (TSeqList*)list;

SeqListNode * node = SeqList_Get( list, pos);

int i;

if(sList != NULL && pos >= 0 && pos< sList->len)

{

for( i=pos+1; i<sList->len; i++)

{

sList->node[i-1] = sList->node[i];

}

sList->len--;

}

return node;

}

演示:

資料拓展:

線性表是最基本、最簡單、也是最常用的一種數據結構。

線性表中數據元素之間的關系是一對一的關系,即除了第一個和最後一個數據元素之外,其它數據元素都是首尾相接的(注意,這句話只適用大部分線性表,而不是全部。比如,循環鏈表邏輯層次上也是一種線性表(存儲層次上屬於鏈式存儲),但是把最後一個數據元素的尾指針指向了首位結點)。

我們說「線性」和「非線性」,只在邏輯層次上討論,而不考慮存儲層次,所以雙向鏈表和循環鏈表依舊是線性表。

在數據結構邏輯層次上細分,線性表可分為一般線性表和受限線性表。一般線性表也就是我們通常所說的「線性表」,可以自由的刪除或添加結點。受限線性表主要包括棧和隊列,受限表示對結點的操作受限制。

線性表的邏輯結構簡單,便於實現和操作。因此,線性表這種數據結構在實際應用中是廣泛採用的一種數據結構。

『玖』 用C語言怎麼建立線性鏈表

線性鏈表是一種數據結構。
使用指針在線性表的各個節點進行連接。
struct
NODE
{
int
value;
NODE*
next;
}
這種結構就是一種線性表的數據結構咯,你使用next指針指向下一個節點。

熱點內容
有壓縮錢嗎 發布:2025-02-08 16:34:01 瀏覽:517
折紙手工解壓小方塊 發布:2025-02-08 16:32:45 瀏覽:254
php與運算符 發布:2025-02-08 16:32:45 瀏覽:764
如何用伺服器搭建懸賞平台 發布:2025-02-08 16:29:53 瀏覽:280
ftp伺服器破解版 發布:2025-02-08 16:28:41 瀏覽:523
mysql配置訪問ip 發布:2025-02-08 16:22:49 瀏覽:116
體表面積簡易演算法 發布:2025-02-08 16:18:04 瀏覽:687
存儲器的分級儲存是如何實現的 發布:2025-02-08 16:11:27 瀏覽:193
電腦怎麼看路由器密碼 發布:2025-02-08 16:10:13 瀏覽:401
匯編宏編譯 發布:2025-02-08 16:08:30 瀏覽:194