国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c語言之單鏈表

2019-11-08 03:05:42
字體:
來源:轉載
供稿:網友

1.什么是鏈式存儲結構

線性表的鏈式存儲結構稱為鏈表。在鏈表的存儲中,每個結點不僅包含所存元素本身的信息,還包含元素之間邏輯關系的信息,即前驅結點包含后繼結點的地址信息,這樣就可以通過前驅結點中的地址信息方便地找到后繼結點的位置。

2.鏈式存儲的特點

(1)鏈表不支持隨機訪問。

(2)鏈表的結點的存儲空間利用率較之順序表稍低一些。

(3)鏈表支持存儲空間的動態分配。

(4)在鏈表中進行插入操作無需移動元素。

3.單鏈表結構

typedef struct Node{	int data;	struct Node *next;}Node;

4.單鏈表實現增刪查

頭插法圖:

實現:

#include<stdio.h>#include<stdlib.h>typedef struct Node{	int data;	struct Node *next;}Node;//頭插法創建鏈表Node *createListByHead(Node *A){	int length,i,x;	Node *p,*s;	A=(Node *)malloc(sizeof(Node));	if(A ==NULL){		PRintf("ERROR");		exit(0);	}	A->next=NULL;	p=A;	scanf("%d", &length);	for(i=0;i<length;i++){		scanf("%d", &x);		s=(Node *)malloc(sizeof(Node));		if(s ==NULL){			printf("ERROR");			exit(0);		}		s->data=x;		s->next=p->next;		p->next=s;	}	return A;}//尾插法創建鏈表Node *createListByTail(Node *B){	int length,i,x;	Node *p,*q;	B=(Node *)malloc(sizeof(Node));	if(B==NULL){		printf("ERROR");	}	B->next=NULL;	p=B;	scanf("%d",&length);	for(i=0;i<length;i++){		scanf("%d",&x);		q=(Node *)malloc(sizeof(Node));		q->data=x;		p->next=q;		p=p->next;	}	p->next=NULL;	return B;}//查詢輸入值是否在鏈表中int findVal(Node *C,int x){	Node *p=C->next;	while(p !=NULL){		if(p->data==x){			return 1;		}		p=p->next;	}	return 0;}//刪除單鏈表結點int deleteList(Node *A,int x){	Node *p=A;	Node *q;	if(findVal(A,x)==0){		return 0;	}	while(p !=NULL){		if(p->next->data==x){			q=p->next;			p->next=q->next;			free(q);			return 1;		}		p=p->next;	}	return 0;}//打印鏈表所有值void printList(Node *A){	Node *p=A->next;	printf("打印鏈表結果:/n");	while(p !=NULL){		printf("%d ",p->data);		p=p->next;	}	printf("/n");}void main(){	Node *A,*B;	int x;	//B=createListByHead(A);	B=createListByTail(A);	printList(B);	printf("刪除值:/n");	scanf("%d",&x);	deleteList(B,x);	printList(B);	//printf("%d",findVal(B,x));}結果:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新晃| 长汀县| 饶河县| 陆河县| 密云县| 永年县| 毕节市| 丹阳市| 东至县| 铜川市| 抚远县| 南昌市| 潮安县| 扎鲁特旗| 安达市| 扶风县| 建宁县| 大连市| 随州市| 青浦区| 西林县| 剑川县| 宁陵县| 永州市| 资源县| 大悟县| 济阳县| 武城县| 亳州市| 葵青区| 富顺县| 扶绥县| 固阳县| 天水市| 廊坊市| 英山县| 新郑市| 龙泉市| 辽宁省| 布尔津县| 林芝县|