當前位置:首頁 » 編程語言 » c語言實現的鏈表

c語言實現的鏈表

發布時間: 2022-07-11 03:04:34

① 如何實現c語言鏈表

鏈表的創建:

#include 「stdlib.h」
#include 「stdio.h」
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};

int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf(「%ld,%f」,&p1->num,&p1->score);
head=NULL;
while(p1->num != 0)
{
n=n+1;
if(n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf(「%ld,%f」,&p1->num,&p1->score);
}
p2->next = NULL;
return(head);
}

void main()
{
creat();
}

這樣便可創建鏈表

② 用C語言實現對單鏈表的基本操作

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

typedefintDataType;

typedefstructnode{
DataTypemember;
structnode*next;
}*LinkList,*pNode;

//初始化鏈表
LinkListGetEmptyList(){
LinkListhead=(pNode)malloc(sizeof(structnode));
head->member=0;
head->next=NULL;
returnhead;
}


//在非增鏈表中插入結點
voidInsertNode(LinkListhead,DataTypex){
pNodep,q;
for(p=head;p->next!=NULL;p=p->next){
if(p->next->member<=x){
q=(pNode)malloc(sizeof(structnode));
q->member=x;
q->next=p->next;
p->next=q;
return;
}
}
q=(pNode)malloc(sizeof(structnode));
q->member=x;
q->next=p->next;
p->next=q;
}

//新結點插入為首結點
voidPushNode(LinkListhead,DataTypex){
pNodep=(pNode)malloc(sizeof(structnode));
p->member=x;
p->next=head->next;
head->next=p;
}

//刪除結點
intDeleteNode(LinkListhead,DataTypex){
pNodep,q;
for(p=head;p!=NULL;p=p->next){
if(p->next->member==x){
q=p->next;
p->next=q->next;
free(q);
return1;//成功刪除member(第一個)為x的結點
}
}
return0;//沒有找到member為x的結點
}

//查找結點
intFindNode(LinkListhead,DataTypex){
pNodep;
for(p=head->next;p!=NULL;p=p->next){
if(p->member==x)return1;//找到了
}
return0;//沒有找到
}

//銷毀鏈表
voidDestroyList(LinkListhead){
pNodeq,p=head;
while(p){
q=p;
p=q->next;
free(q);
}
head=NULL;
}

//遍歷鏈表
voidShowList(LinkListhead){
pNodep=head->next;
while(p!=NULL){
printf("%d",p->member);
p=p->next;
}
printf(" ");
}

intmain(){
DataTypex,res;
LinkListhead=GetEmptyList();
printf("輸入一個整數('q'toquit):");
while(scanf("%d",&x)==1){
InsertNode(head,x);//創建非增鏈表
printf("輸入一個整數('q'toquit):");
}
fflush(stdin);
ShowList(head);
printf("輸入待查找的整數:");
scanf("%d",&x);
res=FindNode(head,x);
if(res)printf("找到了。 ");
elseprintf("沒找到! ");
printf("輸入待刪除的整數:");
scanf("%d",&x);
res=DeleteNode(head,x);
if(res)printf("成功刪除。 ");
elseprintf("沒找到數據為:%d的結點! ",x);
ShowList(head);
DestroyList(head);
return0;
}

③ 用c語言寫一個簡單的鏈表,具體要怎麼用代碼實現

struct
stu
{
char
a[10];
char
e[4];
char
c[14];
char
d[30];
struct
stu*b;
};
struct
stu
*creat(struct
stu*head,char
c)
//功能1:創建鏈表
{
while(c-'y'==0||c-'y'==0)
{
printf("請輸入姓名,性別,電話號碼,e-mail地址\n");
head=insert(head);
printf("是否繼續輸入y/n\n");getchar();
scanf("%c",&c);system("cls");
}
if(c-'n'==0||c-'n'==0)
return
head;
else
{
printf("是否繼續輸入y/n\n");
getchar();
scanf("%c",&c);
system("cls");
return
(creat(head,c));
}
}

④ 用C語言編程實現單鏈表的基本操作

第二個顯示為什麼?為什麼什麼東西都沒有、
#include<stdio.h>
typedef
struct
sample
{
char
ch;
struct
sample
*next;
}LNode;
LNode
*Createlist(LNode
*head)
//創建單鏈表,頭插入法
{
LNode
*p;
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf("aply
error\n");
head->next=NULL;
head->ch
=
'\0';
int
i=
0;
printf("請一次輸入A-Z:\n");
for(i
=
0
;
i
<
26;
++i)
{
p=(LNode
*)malloc(sizeof(LNode));
if(!p)
printf("aply
error\n");
scanf("%c",&p->ch);
p->next
=
head->next;
head->next=p;
}
return
head;
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head->next,*r
=
head->next;
while(p->next)
{
p
=
p->next;
}
int
i
=
0
;
for(i
=
0
;
i
<
3;
i++)
{
p->next
=
r;
p
=
p->next;
q
=
q->next;
r
=
q;
}
p->next
=NULL;
head->next
=
q;
return
head;
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head->next;
while(p->next!=NULL)
{
printf("%c\t",p->ch);
p=p->next;
}
printf("%c\n",p->ch);
}
int
main(void)
{
LNode
*head;
head
=
Createlist(head);//鏈表初始化
ListPrint(head);
//列印單鏈表數據
head
=
EncryptList(head);
ListPrint(head);
return
0;
}
看看。

⑤ 實現鏈表(C語言)

鏈表應該沒有很高效的查找演算法吧,只有一個一個按順序查找啊

不過可以設置兩個指針變數,
link* p1,p2;
p1用來遍歷
p2用來記錄與p2相差K-1個節點
當p1遍歷到完鏈表時,p2就是了

⑥ 如何用C語言編寫一個鏈表

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

struct Node
{
int data;//數據域
struct Node * next;//指針域
};

/*************************************************************************************
*函數名稱:Create
*函數功能:創建鏈表.
*輸入:各節點的data
*返回值:指針head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函數名稱:insert
*函數功能:在鏈表中插入元素.
*輸入:head 鏈表頭指針,p新元素插入位置,x 新元素中的數據域內容
*返回值:無
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函數名稱:del
*函數功能:刪除鏈表中的元素
*輸入:head 鏈表頭指針,p 被刪除元素位置
*返回值:被刪除元素中的數據域.如果刪除失敗返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函數名稱:print
*函數功能:列印鏈表中的元素
*輸入:head 鏈表頭指針
*返回值:無
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函數名稱:main
*函數功能:主函數創建鏈表並列印鏈表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}

⑦ C語言裡面的鏈表是什麼

C語言裡面的鏈表是一種數據結構
是一種線形的存儲結構
鏈表和數組一樣,也是將一組同類型的數據組織在一起的一種數據結構
不同的是
數組採用的是順序存儲,依靠數組的首地址和元素的相對地址(下標)來實現訪問
優點是訪問方便快捷,而缺點是數組是靜態的,不利於實現元素的動態增減。
而鏈表採用的是離散存儲,依靠節點間的指向下一個節點的指針來實現訪問。
其優缺點和數組相反

⑧ 用C語言實現鏈表的演算法

這個是我們數據結構上機實驗的鏈表問題,
#include<stdio.h>
#include<malloc.h>
#define
LEN
sizeof(LinkNode)
typedef
int
Datatype;
typedef
int
Status;
typedef
struct
LinkNode{
Datatype
data;
struct
LinkNode
*next;
}
LinkNode,*LinkList;
typedef
struct
OrderedList
{
LinkNode
*head,*tail;
int
Listsize;
}
OrderedList;//有序循環鏈表的頭節點head,尾接接節點
tail及長度Listsize
Status
InitList(OrderedList
*List)//生成循環鏈表頭節點
{
List->tail=List->head=(LinkList)malloc(LEN);
if(List->head==NULL)
return
0;
else
{
List->head->next=List->tail;
List->tail->next=List->head;
List->Listsize=0;
return
1;
}
}
void
OrderedInsert(OrderedList
*List,Datatype
data)//每調用一次有序插入data形成有序的(從小到大)的鏈表
{
LinkNode
*p
,*q;
if(List->head==List->tail->next)
{
p=(LinkNode*)malloc(LEN);
p->data
=
data;
List->head->next=p;
p->next=List->tail;
List->Listsize++;
}
else
{
p=List->head->next;
q
=
List->head;
while(p->data<data&&p!=List->tail)
{
q
=
p;
p=p->next;
}
if(p->data==data)
{printf("YOu
have
input
the
same
datas
%d\n\t
YOu
should
input
another
data
\n",data);
scanf("%d",&data);
OrderedInsert(List,data);
}
else
{
p=(LinkNode*)malloc(LEN);
p->data
=
data;
p->next
=
q->next;
q->next
=
p;
List->Listsize++;
}
}
}
void
Creatset(OrderedList
*List)//多次調用OrderedInsert()生成有序鏈表即集合List
{
Datatype
data;
int
setsize
,
i=0;
printf("Please
input
the
setsize
you
want
to
creat:\n");
scanf("%d",&setsize);
InitList(List);
if(setsize==0)
printf("You
needen't
input
any
data\n");
else
if(setsize==1)
printf("Please
input
a
single
data\n");
else
printf("Please
input
%d
different
datas;\n",setsize);
while(i<setsize||setsize>List->Listsize)//當循環次數i小於setsize或者集合內實際元素數List.Listsize小於setsize時一直循環下去
{
scanf("%d",&data);
OrderedInsert(List,data);
i++;
}
}
void
Append(OrderedList
*List,Datatype
data)//在循環鏈表的最後面追加
一個data
{
LinkNode
*p;
p=(LinkNode*)malloc(LEN);
p->data=data;
List->tail=List->tail->next=p;
List->tail->next=List->head;
List->Listsize+=1;
}
void
MergeList(OrderedList
La,OrderedList
Lb,OrderedList
*Lc)//有序循環鏈表ListLa,ListLb求並集生成ListLc
{
LinkList
Pa,Pb;
Pa=La.head->next;Pb=Lb.head->next;
while(Pa!=La.tail&&Pb!=Lb.tail)
{
if(Pa->data<=Pb->data)
{
Append(Lc,Pa->data);
Pa=Pa->next;
}
else
{
Append(Lc,Pb->data);Pb=Pb->next;
}
}
while(Pa!=La.tail)
{
Append(
Lc,Pa->data);Pa=Pa->next;}
while(Pb!=Lb.tail)
{
Append(Lc,Pb->data);Pb=Pb->next;}
}
void
Print(OrderedList
List)
{
LinkNode
*p;
p=List.head->next;
if(p->next==List.head)
printf("No
Elem\n");
while(p!=List.head)
{
printf("%5d",p->data);p=p->next;
}
printf("\n");
}
void
main()
{
OrderedList
ListLa,ListLb,ListLc;
Creatset(&ListLa);
Creatset(&ListLb);
InitList(&ListLc);
MergeList(ListLa,ListLb,&ListLc);
printf("The
orgnial
list
ListLa,ListLb:\n");
Print(ListLa);
Print(ListLb);
printf("The
Merge
list
ListLc;\n");
Print(ListLc);
}

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:432
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:233
java駝峰 發布:2025-02-02 09:13:26 瀏覽:651
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:532
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726