数据结构笔记--顺序表
来源:互联网 发布:淘宝小镇姗姗模特是谁 编辑:程序博客网 时间:2024/05/21 15:01
………………数据结构太烂了。。
找了好几本书,重头学吧。。。。
万事开头难啊
写个顺序存储的顺序表写了一个下午!!总算写好了,有点小问题,大家可以讨论讨论
源代码: 我在头文件里把所有的函数都定义出来了。。(好吧。我是懒得再写源文件了。。觉得不爽的可以把定义和声明分开。。。)
FileName: SqList.h
----------------------------------------------------------------------------------------------------------------------------------
#ifndef __SQLIST__H__#define __SQLIST__H__#include <stdio.h>#include <malloc.h>#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define ElemType inttypedef enum { OK, NO, OVERFLOW}Status;typedef struct { ElemType *elem; int length; int listsize;}SqList;//Initial the listStatus InitList_Sq(SqList *L) { L->elem = (ElemType *) malloc(LIST_INIT_SIZE * sizeof(ElemType)); if (!L->elem) { return OVERFLOW; } L->length = 0; L->listsize = LIST_INIT_SIZE; return OK;}void DestroyList_Sq(SqList *L) { free(L->elem); L->length = 0; L->listsize = 0;}Status ClearList_Sq(SqList *L) { DestroyList_Sq(L); L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)); L->length = 0; if (!L->elem) { return OVERFLOW; } else { return OK; }}Status ListEmpty_Sq(SqList L) { if (!L.elem) { return OVERFLOW; } else { if (0 == L.length) { return OK; } else { return NO; } }}int ListLength_Sq(SqList L) { return L.length;}Status ListInsert_Sq(SqList *L, int i, ElemType e) { if ( !L->elem && (i > L->length + 1 || i < 1) ) { return OVERFLOW; } if (L->length + 1 > L->listsize) { ElemType *newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType)); if (!newbase) { return OVERFLOW; } L->elem = newbase; L->listsize += LISTINCREMENT; } //The pointer tail acts as the iterator end() of vector //it points an invalid address just after the last available elements ElemType *head, *tail; for (head = &L->elem[i - 1], tail = L->elem + L->length; tail != head ; --tail) { *tail = *(tail - 1); } *head = e; ++L->length; return OK;} Status ListDelete_Sq(SqList *L, int i, ElemType *e) { if ( !L->elem && (i < 1 || i > L->length) ) { return OVERFLOW; } //The pointer tail acts as the iterator end() of vector //it points an invalid address just after the last available elements ElemType *head, *tail; head = &L->elem[i]; *e = *head; for (tail = L->elem + L->length; head != tail; ++head) { *(head - 1) = *head; } --L->length; return OK;}Status GetElem_Sq(SqList L, int i, ElemType *e) { if ( !L.elem && (i < 1 || i > L.length) ) { return OVERFLOW; } e = &L.elem[i - 1]; return OK;}int LocateElem_Sq(SqList L, ElemType e) { if (!L.elem) { return OVERFLOW; } int i; for (i = 0 ; i < L.length; ++i) { if (L.elem[i] == e) { return i + 1; } } return 0;}Status PriorElem_Sq(SqList L, ElemType cur_e, ElemType *pre_e) { int i = LocateElem_Sq(L, cur_e); if (1 == i || 0 == i) { return OVERFLOW; } pre_e = &L.elem[i - 2]; return OK;}Status NextElem_Sq(SqList L, ElemType cur_e, ElemType *next_e) { int i = LocateElem_Sq(L, cur_e); if(0 == i || L.length == i) { return OVERFLOW; } *next_e = L.elem[i]; return OK;}void ListTraverse_Sq(SqList L, void (*visit)(ElemType)) { int i; for (i = 0; i < L.length; ++i) { visit(L.elem[i]); }}#endif
----------------------------------------------------------------------------------------------------------------------------------
插入和删除还是很麻烦的。
这里的插入和删除函数主要是靠head和tail两个指针,其中tail指针类似vector中的end()迭代器。
还有一个很囧的问题。
我这个代码用g++编译通过:g++ main.c 完全没问题
但是用gcc编译就报错:
/tmp/cchCMy3W.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
要加上-lstdc++才行。。
但是我觉得我的代码是纯c的啊。。连引用的形参我都全搞成指针了。。
囧啊 。
- 数据结构笔记--顺序表
- 数据结构学习笔记:顺序表
- 数据结构笔记之顺序表
- 数据结构学习笔记二 顺序表
- 数据结构之顺序表学习笔记
- 【黑马程序员-学习笔记】数据结构-顺序表
- 数据结构笔记(1)-顺序表
- 数据结构学习笔记(一) 顺序表
- 数据结构学习笔记之顺序表
- 数据结构学习笔记(一) 顺序表
- 数据结构笔记之顺序栈
- 数据结构笔记2 线性表之顺序表
- 数据结构学习笔记 --- 线性表 (顺序表)
- 数据结构学习笔记1-线性表(顺序表,单链表)
- 数据结构学习笔记 --- 线性表 (顺序表)
- 数据结构笔记1---链表与顺序表
- 熟悉笔记—数据结构(c语言版) 之 顺序表
- 数据结构学习笔记(3)线性表-顺序映像
- 关于TCP/IP协议栈(笔记)
- 如何存放好病毒
- linux下打包解包
- makefile :missing seprator
- 【Android代码片段之四】设置全屏
- 数据结构笔记--顺序表
- 数字图像处理基本软件VC++实现基本处理功能(一)
- 筛法筛素数
- 入职一周的展讯笔记
- __declspec 等关键字的全写
- 2 SEO关键词和长尾关键词的分析!
- 常用开源<监控软件>介绍1
- fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h
- XCode、Objective-C、Cocoa !!!