zoukankan      html  css  js  c++  java
  • 顺序表

    seqlist.h


    <span style="font-size:24px;">#ifndef __SEQLIST_H__
    #define __SEQLIST_H__
    #include<stdio.h>
    #include<assert.h>
    #include<string.h>
    #define MAX_SIZE 1000
    typedef int DataType;
    typedef struct seqlist
    {
    	DataType array[MAX_SIZE];
    	size_t size;
    }seqlist;
    typedef enum tag
    {
    	one,//删去第一个
    	more,//所有都删去
    }Tag;
    void Init(seqlist* pSeq);
    void PushBack(seqlist* pSeq, DataType x);
    void PushFront(seqlist* pSeq, DataType x);
    void Print(seqlist* pSeq);
    void PopBack(seqlist* pSeq);
    void PopFront(seqlist*pSeqsssss);
    void Insert(seqlist* pSeq, size_t index, DataType x);
    void Modify(seqlist* pSeq, size_t index, DataType x);
    void removed(seqlist* pSeq, size_t index);
    void Erase(seqlist* pSeq, DataType x, Tag all);
    int Find(seqlist* pSeq, DataType x, size_t index);
    
     
    #endif		//__SEQLIST_H__</span>


    seqlist.c


    #include"seqlist.h"
    
    void Init(seqlist* pSeq)
    {
    	assert(pSeq);
    	memset(pSeq->array, 0, MAX_SIZE*sizeof(DataType));
    	pSeq->size = 0;/////////////size要初始化。否则是随机值会出现故障
    }
    void PushBack(seqlist* pSeq, DataType x)
    {
    	assert(pSeq);
    	assert(pSeq->size <= MAX_SIZE);
    	pSeq->array[pSeq->size++] = x;
    }
    void PushFront(seqlist* pSeq, DataType x)
    {
    	assert(pSeq);
    	assert(pSeq->size <= MAX_SIZE);
    	int i;
    	for (i = pSeq->size; i > 0; i--)
    	{
    		pSeq->array[i] = pSeq->array[i - 1];
    	}
    	pSeq->array[0] = x;
    	pSeq->size++;
    }
    void Insert(seqlist* pSeq, size_t index, DataType x)
    {
    	assert(pSeq);
    	assert(index < pSeq->size);
    	assert(pSeq->size < MAX_SIZE);
    	int i = pSeq->size - 1;
    	for (; i >= (int)index; i--)
    	{
    		pSeq->array[i + 1] = pSeq->array[i];
    	}
    	pSeq->array[index] = x;
    	pSeq->size++;
    }
    void Print(seqlist* pSeq)
    {
    	assert(pSeq);
    	size_t i = 0;
    	for (; i < pSeq->size; i++)
    	{
    		printf("%d ", pSeq->array[i]);
    	}
    	printf("
    ");
    }
    void PopBack(seqlist* pSeq)
    {
    	assert(pSeq);
    	pSeq->size--;
    }
    void PopFront(seqlist* pSeq)
    {
    	assert(pSeq);
    	size_t i;
    	for (i = 0; i < pSeq->size; i++)
    	{
    		pSeq->array[i] = pSeq->array[i + 1];
    	}
    	pSeq->size--;
    }
    void Modify(seqlist* pSeq, size_t index, DataType x)
    {
    	assert(pSeq);
    	assert(index < pSeq->size);
    	pSeq->array[index] = x;
    }
    void removed(seqlist* pSeq, size_t index)
    {
    	assert(pSeq);
    	assert(index < pSeq->size);
    	size_t i = index;
    	for (; i<pSeq->size-1 ; i++)
    	{
    		pSeq->array[i] = pSeq->array[i + 1];
    	}
    	pSeq->size--;
    }
    //返回的值为-1时,找不到此元素;否则返回的值为找到的元素的下标。
    int Find(seqlist* pSeq, DataType x, size_t index)
    {
    	assert(pSeq);
    	int i = index;
    	for (; i < (int)pSeq->size; i++)
    	{
    		if (pSeq->array[i] == x)
    		{
    			return i;
    		}
    	}
    	return -1;
    }
    void Erase(seqlist* pSeq, DataType x, Tag all)
    {
    	assert(pSeq);
    	int ret;
    	ret = Find(pSeq, x, 0);
    	if (ret == -1)
    	{
    		printf("顺序表中没有这个元素
    ");
    	}
    	else if (all == one)
    	{
    		removed(pSeq, ret);
    	}
    	else
    	{
    		for (; ret != -1;)
    		{
    			removed(pSeq, ret);
    			ret = Find(pSeq, x, ret);
    		}
    	}
    }
    
    void Test()
    {
    	seqlist s;
    	Init(&s);
    	PushBack(&s, 1);
    	PushBack(&s, 2);
    	PushBack(&s, 2);
    	PushBack(&s, 2);
    	PushBack(&s, 3);
    	PushBack(&s, 2);
    	PushBack(&s, 4);
    	PushBack(&s, 2);
    	Print(&s);
    	PopBack(&s);
    	Print(&s);
    	PushFront(&s, 5);
    	Print(&s);
    	PopFront(&s);
    	Print(&s);
    	Insert(&s, 0, 24);
    	Print(&s);
    	Modify(&s, 2, 90);
    	Print(&s);
    	removed(&s, 0);
    	Print(&s);
    	Erase(&s, 2, more);
    	Print(&s);
    }
    void main()
    {
    	Test();
    }



  • 相关阅读:
    IOS中UIImage用代码压缩
    自定义UILabel设置垂直方向的居上,居中,居下
    IOS开发之数据加密与解密:AES256+Base64转码
    IOS开发之网络数据Json处理
    Unity3d中CharacterController的移动和碰撞分析
    关于Unity3d中物体之间碰撞检测的方法
    WPF 调用Win32控件
    C# 扩展方法
    颜色空间转换 RGB到HSV
    加速计算技术-- Intel 的IPP库
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7141440.html
Copyright © 2011-2022 走看看