线性结构

线性结构的物理存储形式

1.顺序存储(数组)

2.链式存储(链表)

线性结构的特点

(1)存在惟一的一个被称作“表头”的数据元素和惟一的一个被称作“表尾”的数据元素;
(2)除表头之外,集合中的每个数据元素均只有一个前驱;除表尾之外,集合中的每一个数据元素均只有一个后继。

线性表是最常用且是最简单的一种线性结构。形如:A1、A2、A3….An这样含有有限的数据序列,我们就称之为线性表

栈的结构特点是,仅在栈顶(表尾)进行插入和删除操作。栈可以说是操作受限的线性表。栈中元素的修改是按后进先出的原则进行的,所以又称后进先出的线性表
例子:放在桌上的一堆碗,用碗的时候是从这一堆的顶上拿走的,用过的碗洗好后是放在顶上的,也就是在顶上做插入和删除

栈的链式存储

#include <stdio.h>
#include <malloc.h>
struct node{
	int data;
	struct node *next;
}; 

struct node *gz(){
	struct node *p;
	p=(struct node *)malloc(sizeof(struct node));
	p->next=NULL;
	return p;
}

struct node *cr(struct node *head,int temp){
	struct node *p;
	p=(struct node*)malloc(sizeof(struct node));
	p->data=temp;
	p->next=head->next;
	head->next=p;
	return head;
}
struct bode *sc(struct node *head){
	struct node *p;
	p=head->next;
	head->next=p->next;
	free(p);}
void print(struct node *head){
	struct node *p;
	p=head;
	while(p){
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}
	
int main(){
	struct node *head;
	head=gz();
	int temp;
	char a;

	while(1){
		printf("a 入 b出 c输出\n");
		scanf("%c",&a);
		getchar();
		if(a=='a'){
			printf("输入一个数字进入栈");
			scanf("%d",&temp);
			getchar();
			head=cr(head,temp);
		}
		else if(a=='b'){
			if(head->next==NULL){
			printf("已经空了\n");	
				break;
			} 
			else{
			printf("%d     出栈\n",head->next->data);
			sc(head);}
		}
		else if(a=='c'){
			printf("输出栈");
			print(head->next); 
		}
		else break;
	}  
}

队列是一种先进先出的线性表,元素从表的一端插入,而从另一端删除。
例子:排队

队的顺序存储实现



队的链式存储实现

入队使用尾插法,出队时先输出表头的数据,然后让表头的地址=表头的下一个的地址,在释放表头

顺序队

#include <stdio.h>
#include <malloc.h>
struct node{
	int data[5];
	int real;
	int front;
};

typedef struct node *queue;

void rd(queue ptrq,int item){
	if((ptrq->real+1)%5==ptrq->front){
		printf("队列满 无法入队\n");
		return ;
	}
	ptrq->real=(ptrq->real+1)%5;
	ptrq->data[ptrq->real]=item;
}

void cd(queue ptrq){
	if(ptrq->front==ptrq->real){
	printf("队列空\n");
		return; 
	}
	ptrq->front=(ptrq->front+1)%5;
	printf("%d   出队\n",ptrq->data[ptrq->front]);
}
int main(){
	queue ptrq;
	int item;
	ptrq=(struct node *)malloc(sizeof(struct node));
	ptrq->front=0;
	ptrq->real=0;
	char a;
	while(1){
		printf("a 入队 b 出队 ");
		scanf("%c",&a);
		getchar();
		if(a=='a'){
			printf("输入一个数字进入队列 ");
			scanf("%d",&item);
			getchar();
			rd(ptrq,item); 
		}
		else if(a=='b'){
			cd(ptrq);
		}
		else break;
	}
}

链表队

#include <stdio.h>
#include <malloc.h>

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

node *print(node *head){
	node *p;
	p=head;
	printf("%d\n",p->data);
	head=head->next;
	free(p);
	return head;
}
int main(){
	node *head=NULL,*p,*p1;
	p=(node *)malloc(sizeof(node));
	int n=0,temp,size;
	char a;
	printf("输入队列大小:");
	scanf("%d",&size); 
	getchar();
	while(1){
		printf("a 入队 b出队\n");
		scanf("%c",&a);
		getchar();
		if(a=='a'){
			scanf("%d",&temp);
			getchar();
			p=(struct node *)malloc(sizeof(struct node));
			p->data=temp;
			n++;
					if(n==1){
			head=p1=p;
				p->next=NULL;	
		}
		else if(n<=size){
			p1->next=p;
			p1=p;
		p->next=NULL;			
		}
		else{
		printf("队列满了 无法加入\n");	
		n--;
		} 
	}
		else if(a=='b'){
			if(n>0){
			head=print(head);
			n--; 
		}
		else printf("队列已空 无法出队\n"); 
		}
	}
	else break;
} 
posted @ 2021-07-31 23:52  计科废物1  阅读(547)  评论(0)    收藏  举报
@media only screen and (max-width: 767px){ #sidebar_search_box input[type=text]{width:calc(100% - 24px)} } L2Dwidget.init({ "model": { jsonPath: "https://unpkg.com/live2d-widget-model-hijiki/assets/hijiki.model.json", "scale": 1 }, "display": { "position": "left", "width": 100, "height": 200, "hOffset": 70, "vOffset": 0 }, "mobile": { "show": true, "scale": 0.5 }, "react": { "opacityDefault": 0.7, "opacityOnHover": 0.2 } }); window.onload = function(){ $("#live2dcanvas").attr("style","position: fixed; opacity: 0.7; left: 70px; bottom: 0px; z-index: 1; pointer-events: none;") }