1. 程式人生 > >順序表相關操作

順序表相關操作

順序表相關操作


程式程式碼如下:


SeqList.h


#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#include <stdio.h>
#include <assert.h>
#define MAX 10
typedef int DataType;
typedef struct SeqList
{
	DataType data[MAX];
	int sz;
}SeqList,*pSeqList;
//初始化 
void InitSeqList(pSeqList pSeq);
//尾部插入 
void
PushBack(pSeqList pSeq, DataType data); //尾部刪除 void PopBack(pSeqList pSeq); //頭部插入 void PushFront(pSeqList pSeq, DataType data); //頭部刪除 void PopFront(pSeqList pSeq); //查詢指定元素 int Find(pSeqList pSeq, DataType data); //指定位置插入 void Insert(pSeqList pSeq, int pos, DataType data); //刪除指定位置元素 void Erase(pSeqList pSeq,
int pos); //刪除指定元素 void Remove(pSeqList pSeq, DataType data); //刪除所有的指定元素 void RemoveAll(pSeqList pSeq, DataType data); //返回順序表的大小 int Size(pSeqList pSeq); //判斷順序表是否為空 int Empty(pSeqList pSeq); //氣泡排序 void BubbleSort(pSeqList pSeq); //選擇排序 void SelectSort(pSeqList pSeq); //選擇排序的優化 void SelectSortOP(pSeqList pSeq)
; //二分查詢 int BinarySearch(pSeqList pSeq, DataType data); //二分查詢遞迴寫法 int BinarySearch_R(pSeqList pSeq, int left, int right, DataType d); //列印 void PrintSeqList(pSeqList pSeq); #endif //__SEQLIST_H__

SeqList.c

#include <string.h>
#include "SeqList.h"
//初始化
void InitSeqList(pSeqList pSeq)
{
	assert(pSeq);
	pSeq->sz = 0;
	memset(pSeq->data, 0, sizeof(pSeq->data));
}
//尾部插入
void PushBack(pSeqList pSeq,DataType data)
{
	assert(pSeq);
	if (pSeq->sz == MAX)
	{
		printf("順序表已滿,無法插入\n");
		return;
	}
	pSeq->data[pSeq->sz] = data;
	pSeq->sz++;
}
//尾部刪除
void PopBack(pSeqList pSeq)
{
	assert(pSeq);
	if (pSeq->sz == 0)
	{
		return;
	}
	pSeq->sz--;

}
//頭部插入 
void PushFront(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int i = 0;
	if (pSeq->sz == MAX)
	{
		printf("順序表已滿,無法插入\n");
		return;
	}
	for (i = pSeq->sz; i > 0; i--)
	{
		pSeq->data[i] = pSeq->data[i - 1];
	}
	pSeq->data[0] = data;
	pSeq->sz++;
}
//頭部刪除 
void PopFront(pSeqList pSeq)
{
	assert(pSeq);
	if (pSeq->sz)
	{
		int i = 0;
		for (i = 0; i < pSeq->sz - 1; i++)
		{
			pSeq->data[i] = pSeq->data[i + 1];
		}
		pSeq->sz--;
	}
}
//查詢指定元素 
int Find(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int i = 0;
	for (i = 0; i < pSeq->sz; i++)
	{
		if (data == pSeq->data[i])
			return i;
	}
	return -1;
}
//指定位置插入 
void Insert(pSeqList pSeq, int pos, DataType data)
{
	assert(pSeq);
	int i = 0;
	if (pSeq->sz == MAX)
		return;
	for (i = pSeq->sz; i > pos; i--)
	{
		pSeq->data[i] = pSeq->data[i - 1];
	}
	pSeq->data[pos] = data;
	pSeq->sz++;
}
//刪除指定位置元素 
void Erase(pSeqList pSeq, int pos)
{
	assert(pSeq);
	int i = 0;
	if (pSeq->sz == 0)
		return;
	for (i = pos; i < pSeq->sz - 1; i++)
	{
		pSeq->data[i] = pSeq->data[i + 1];
	}
	pSeq->sz--;
}
//刪除指定元素 
void Remove(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int i = 0;
	int pos = 0;
	if (pSeq->sz == 0)
		return;
	while ((pos = Find(pSeq, data)) != -1)
	{
		for (i = pos; i < pSeq->sz - 1; i++)
		{
			pSeq->data[i] = pSeq->data[i + 1];
		}
		pSeq->sz--;
	}
}
//刪除所有的指定元素 
void RemoveAll(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int i = 0;
	int j = 0;
	if (pSeq->sz == 0)
		return;
	while (i < pSeq->sz)
	{
		if (pSeq->data[i] != data)
		{
			if (i != j)
				pSeq->data[j] = pSeq->data[i];
			i++;
			j++;
		}
		else
		{
			i++;
		}
	}
	pSeq->sz = j;

}
//返回順序表的大小 
int Size(pSeqList pSeq)
{
	assert(pSeq);
	return pSeq->sz;
}
//判斷順序表是否為空 
int Empty(pSeqList pSeq)
{
	assert(pSeq);
	return 0 == pSeq->sz;
}
//氣泡排序 
void BubbleSort(pSeqList pSeq)
{
	assert(pSeq);
	int i = 0;
	int j = 0;
	DataType tmp = 0;
	if (pSeq->sz == 0)
		return;
	for (i = 0; i < pSeq->sz - 1; i++)
	{
		for (j = 0; j < pSeq->sz - 1 - i; j++)
		{
			if (pSeq->data[j] > pSeq->data[j + 1])
			{
				tmp= pSeq->data[j];
				pSeq->data[j] = pSeq->data[j + 1];
				pSeq->data[j + 1] = tmp;
			}
		}
	}
}
//選擇排序 
void SelectSort(pSeqList pSeq)
{
	assert(pSeq);
	int i = 0;
	int j = 0;
	DataType tmp = 0;
	if (pSeq->sz == 0)
		return;
	for (i = 0; i < pSeq->sz - 1; i++)
	{
		for (j = i + 1; j < pSeq->sz - 1; j++)
		{
			if (pSeq->data[i] > pSeq->data[j])
			{
				tmp = pSeq->data[i];
				pSeq->data[i] = pSeq->data[j];
				pSeq->data[j] = tmp;
			}
		}
	}
}
//選擇排序的優化 
void SelectSortOP(pSeqList pSeq)
{
	assert(pSeq);
	int i = 0;
	int j = 0;
	int index = 0;
	DataType num = 0;
	DataType tmp = 0;
	if (pSeq->sz == 0)
		return;
	for (i = 0; i < pSeq->sz - 1; i++)
	{
		index = i;
		num = pSeq->data[i];
		for (j = i + 1; j < pSeq->sz - 1; j++)
		{
			if (num > pSeq->data[j])
			{
				index = j;
				num = pSeq->data[j];
			}
		}
		if (index != i)
		{
			tmp = pSeq->data[index];
			pSeq->data[index] = pSeq->data[i];
			pSeq->data[i] = tmp;
		}
	}
}
//二分查詢 
int BinarySearch(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int left = 0;
	int right = pSeq->sz - 1;
	while (left <= right)
	{
		int mid = left + ((right - left) >> 1);
		if (data < pSeq->data[mid])
			right = mid - 1;
		else if (data == pSeq->data[mid])
			return mid;
		else
			left = mid + 1;
	}
	return -1;
}
//二分查詢遞迴寫法 
int BinarySearch_R(pSeqList pSeq, int left, int right, DataType d)
{
	assert(pSeq);
	
	if (left <= right)
	{
		int mid = left + ((right - left) >> 1);
		if (d == pSeq->data[mid])
			return mid;
		else if (d < pSeq->data[mid])
			return  BinarySearch_R(pSeq, left, mid - 1, d);
		else
			return BinarySearch_R(pSeq, mid + 1, right, d);
	}
	return -1;
}

//列印 
void PrintSeqList(pSeqList pSeq)
{
	assert(pSeq);
	int i = 0;
	if (pSeq->sz == 0)
	{
		printf("順序表為空\n");
		return;
	}
	for (i = 0; i < pSeq->sz; i++)
	{
		printf("%d ", pSeq->data[i]);
	}
	printf("\n");
}

test.c

#include <stdio.h>
#include "SeqList.h"
#define SORT 0
#define BS 2

void test1()
{
	SeqList ps;
	//初始化 
	InitSeqList(&ps);
	//尾部插入 
	PushBack(&ps, 1);
	PushBack(&ps, 2);
	PushBack(&ps, 3);
	PushBack(&ps, 4);
	PushBack(&ps, 5);
	//列印 
	PrintSeqList(&ps);
	//尾部刪除 
	PopBack(&ps);
	PopBack(&ps);
	//列印 
	PrintSeqList(&ps);
}

void test2()
{
	SeqList ps;
	//初始化 
	InitSeqList(&ps);
	//頭部插入 
	PushFront(&ps, 6);
	PushFront(&ps, 5);
	PushFront(&ps, 5);
	PushFront(&ps, 4);
	PushFront(&ps, 3);
	PushFront(&ps, 1);
	PushFront(&ps, 0);
	//列印 
	PrintSeqList(&ps);
	//頭部刪除 
	PopFront(&ps);
	//列印 
	PrintSeqList(&ps);
	//查詢指定元素 
	int num = Find(&ps, 3);
	if (num !=-1)
		//指定位置插入 
		Insert(&ps, num, 2);
	//列印 
	PrintSeqList(&ps);
	//查詢指定元素 
	num = Find(&ps, 5);
	//刪除指定位置元素 
	Erase(&ps, num);
	//列印 
	PrintSeqList(&ps);
	//刪除指定元素 
	Remove(&ps, 5);
	//列印 
	PrintSeqList(&ps);
}

void test3()
{
	SeqList ps;
	//初始化 
	InitSeqList(&ps);
	//頭部插入 
	PushFront(&ps, 9);
	PushFront(&ps, 7);
	PushFront(&ps, 11);
	PushFront(&ps, 6);
	PushFront(&ps, 2);
	PushFront(&ps, 8);
	PushFront(&ps, 11);
	//列印 
	PrintSeqList(&ps);
	//刪除所有的指定元素 
	RemoveAll(&ps, 11);
	//列印 
	PrintSeqList(&ps);
	//返回順序表的大小 
	int sz = Size(&ps);
	printf("順序表大小:%d\n", sz);
	//判斷順序表是否為空 
	int ret = Empty(&ps);
	if (0 == ret)
		printf("順序表不為空\n");
	else
		printf("順序表為空\n");
	switch (SORT)
	{
	case 1:
		//選擇排序 
		SelectSort(&ps);
		
            
           

相關推薦

順序相關操作

順序表相關操作 程式程式碼如下: SeqList.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include <stdio.h> #include <assert.h> #define

動態順序相關操作

    #pragma once #include <stdio.h> #include <assert.h> #include <windows.h> #include <stdlib.h> typedef

單鏈順序儲存相關操作的c語言實現

以前學資料結構時,對於單鏈表的順序儲存的原理都懂,但是從沒有好好地程式設計實現一下,現在呢,重溫資料結構,然後實現了一下.下面是原始碼.實現了初始化,建立,查詢,刪除,定位還有兩個連結串列的合併.#i

資料結構順序相關基本操作練筆

#include<stdio.h> #include<stdlib.h> #define MAX 30 #define LIST_INCREMENT 40 typedef struct{ int lenth; int listsize; int *base; }s

07、順序操作

main type 6.0 函數聲明 功能 刪除! align size 一個 順序表的操作 一、從順序表中刪除具有最小值的元素 /* 時間:2017年7月2日10:49:39 功能:從順序表中刪除具有最小值的元素並將最後元素放於被刪除元素的位置,由函數返回被刪元素的

1.順序基本操作

空間 sql註入 表達 pan 專業 truct 優點 length 理解 (ps上了大學,一開始不知道自己專業是學編程的,等到半路知道自己是學編程的時候,又不知道到底該怎麽學,該學什麽。一直處於一個很尷尬的境地。 大一的時候玩了玩pangolin,學了html和一點點ja

順序基本操作的實現

oid new for fine h+ 基本 delet 輸出 turn 順序表基本操作的實現,主要包括順序表的初始化、建立、輸出、插入、刪除、位置查詢、數據查詢。 #include<iostream.h>#define MAXSIZE 100typedef i

django-模型層(model)-多相關操作(圖書管理練習)

brush height cit query == es2017 blank res name 66、django之模型層(model)--多表相關操作(圖書管理小練習) 前面幾篇隨筆的數據庫增刪改查操作都是在單表的操作上的,然而現實中不可能都是單表操作,更多的是多表操作,

memcached中hash相關操作

top this eof get 完整 啟動 哈希 作用 需要   以下轉自http://blog.csdn.net/luotuo44/article/details/42773231 memcached源碼中assoc.c文件裏面的代碼是構造一個哈希表。m

mysql的庫和相關操作

ear dml 枚舉類型 mysqld grant 文件路徑 關系數據庫 通用 非關系型 一、數據庫介紹1、數據庫的由來我們之前所學,數據要想永久保存,都是保存於文件中,毫無疑問,一個文件僅僅只能存在於某一臺機器上,這樣就帶來了許多問題:(1)程序所有的組件就不可能運行在一

python學習第三十九天:mysql相關操作

sca 學習 參數 primary cas reat id字段 create 約束 表的完整性約束 約束條件與數據類型的寬度一樣,都是可選參數 作用:用於保證數據的完整性和一致性 主要分為: not null 標識該字段不能為空 default 為該字段設置默認值 u

MySQL之相關操作

warning inf data employees duplicate 不同 建表 www img 一 存儲引擎介紹 存儲引擎即表類型,mysql根據不同的表類型會有不同的處理機制 詳見:http://www.cnblogs.com/linhaifeng/arti

6-1 順序基本操作 (10 分)

本題要求實現順序表元素的增、刪、查詢以及順序表輸出共4個基本操作函式。L是一個順序表,函式Status ListInsert_Sq(SqList &L, int pos, ElemType e)是在順序表的pos位置插入一個元素e(pos應該從1開始),函式Status ListDelete_Sq(S

【資料結構】順序操作函式

SeqList.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include <stdio.h> #include <assert.h> #include <malloc.h> #include <window

mysql 相關操作

mysql 表的相關操作 建立表 表的操作需要依賴於庫在建立表的時候需要先指定資料庫 指定資料庫 use 資料庫名; $ use mydatabase; mysql> use mydatabase; Database changed mysql> 建立資料表

6-2 順序基本操作 (10 分)

6-2 順序表基本操作 (10 分) 本題要求實現順序表元素的增、刪、查詢以及順序表輸出共4個基本操作函式。L是一個順序表,函式Status ListInsert_Sq(SqList &L, int pos, ElemType e)是在順序表的pos位置插入一個元

線性——對順序操作

#ifndef LIST_H #define LIST_H class List { public: List(int size); ~List(); void CleanList(); bool ListEmpty(); int ListLength();

相關操作

重復 lob 客戶端 mod 決定 open 浮點 表示 isp 一.存儲引擎 1.什麽是存儲引擎? 存儲引擎就是如何存儲數據、如何為存儲的數據建立索引和如何更新、查詢數據等技術的實現方法。因為在關系數據庫中數據的存儲是以表的形式存儲的,所以存儲引擎也可以稱為表類型。 數據

Mysql之相關操作3

Mysql表的相關操作3 增語法 ''' 1.所有資料按順序插入 insert [into] 表名 values (值1, ..., 值n)[, ..., (值1, ..., 值n)]; 2.指定欄位匹配插入,可以任意順序 insert [into] 表名(欄位2, 欄位1, ..., 欄位n) val

第三篇:相關操作

一 儲存引擎介紹 儲存引擎即表型別,mysql根據不同的表型別會有不同的處理機制 二 表介紹 表相當於檔案,表中的一條記錄就相當於檔案的一行內容,不同的是,表中的一條記錄有對應的標題,稱為表的欄位 id,name,qq,age稱為欄位,其餘的,一行內容稱為一條記錄 三 建立表 #語法: