zoukankan      html  css  js  c++  java
  • 线性结构

    线性结构的物理存储形式

    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("
    ");
    }
    	
    int main(){
    	struct node *head;
    	head=gz();
    	int temp;
    	char a;
    
    	while(1){
    		printf("a 入 b出 c输出
    ");
    		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("已经空了
    ");	
    				break;
    			} 
    			else{
    			printf("%d     出栈
    ",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("队列满 无法入队
    ");
    		return ;
    	}
    	ptrq->real=(ptrq->real+1)%5;
    	ptrq->data[ptrq->real]=item;
    }
    
    void cd(queue ptrq){
    	if(ptrq->front==ptrq->real){
    	printf("队列空
    ");
    		return; 
    	}
    	ptrq->front=(ptrq->front+1)%5;
    	printf("%d   出队
    ",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
    ",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出队
    ");
    		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--;
    		} 
    	}
    		else if(a=='b'){
    			if(n>0){
    			head=print(head);
    			n--; 
    		}
    		else printf("队列已空 无法出队
    "); 
    		}
    	}
    	else break;
    } 
    
  • 相关阅读:
    ActiveMQ学习第八篇:Consumer
    ActiveMQ学习第七篇:Messaage
    线性判别分析LDA
    逻辑回归
    那些年,曾踩过的Spark坑
    HBase表创建、删除、清空
    python的多线程
    python的多进程
    python实现读写txt文件
    python的封包和解包
  • 原文地址:https://www.cnblogs.com/wjc6765/p/15085707.html
Copyright © 2011-2022 走看看