首页 > 代码库 > 数据结构学习日记2:实现一个简单的线性表功能(链式存储方式)
数据结构学习日记2:实现一个简单的线性表功能(链式存储方式)
数据结构学习日记,此次是用链表来实现一个线性表的功能
// ListTable1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<iostream> using namespace std; #define ERROR 0; #define OK 1; typedef int Status; typedef int ElemType; //声明一个节点 typedef struct Node { ElemType data; Node *next; } Node; typedef struct Node *NodePoint; typedef int *int1; //获取第x个元素的值返回给e Status GetElem(Node *list,int x,ElemType *e) { //循环指针 Node *p; //计数 int j = 1; p = list->next; while(p&&j <x) { p = p->next; j++; } if (!p || j > x) { return ERROR; } *e = p->data; return OK; } //单链表的插入操作 Status InsertList(Node *list, int x, ElemType e) { //NodePoint 指向Node的指针类型,用来声明Node指针类型变量 //Node *p; NodePoint p; int i = 1; p = list->next; while(p&&i < x-1) { p = p->next; i++; } if (!p || i > x) { return ERROR; } //第一种: //Node s; //Node *q; //第二种 NodePoint q; q = (NodePoint)malloc(sizeof(Node)); //第三种 //Node *n; //n=new Node(); 注意这里new出来的是地址; //第四种 //q = (Node *)malloc(sizeof(Node)); q->data =http://www.mamicode.com/ e; q->next = p->next; p->next = q; return OK; } //获取链表的长度 int GetLength(NodePoint list) { NodePoint p; p = list; int i = 1; while (p) { p = p->next; i++; } return i; } //对表中的数据进行初始化 Status InitList(NodePoint list, int length) { //判断内存是否满 //第一步:先初始化一个节点 //q相当于一个游标,只存取第一个元素的位置 srand(1); ElemType value; value = rand() % 9; NodePoint s = (NodePoint)malloc(sizeof(Node)); if (!s) { return ERROR; } list->next = s; s->next = NULL; s->data =http://www.mamicode.com/ value; //第二步: //前插法 for (int i = 1; i < length; i++) { NodePoint p = (NodePoint)malloc(sizeof(Node)); if (!p) { return ERROR; } p->data = http://www.mamicode.com/rand() % 9; p->next = list->next; list->next = p; } ////后插法: //NodePoint last;//尾指针相当于游标 //last = s; //for (int i = 0; i < length; i++) //{ // NodePoint p = (NodePoint)malloc(sizeof(Node)); // p->data = http://www.mamicode.com/rand() % 9;>// last->next = p; // last = p; //} return OK; } //删除表中的元素 Status DeleteList(NodePoint ftNodePoint,int x,ElemType *e) { NodePoint p; NodePoint q; p = ftNodePoint; q = p; int i=0; while(p&&i < x) { q = p; p = p->next; i++; } if (!p || i>x) { return ERROR; } //取出p中的数据 *e = p->data; q->next= p->next; //释放p free(p); return OK; } //清空表中的数据 Status ClearList(NodePoint list) { NodePoint p,cursor; p = list->next; //cursor = p->next; if (!p) { return ERROR; } while (p) { /*free(p); p = cursor; if (cursor) { cursor = cursor->next; } else { break; }*/ cursor = p->next; free(p); p = cursor; } list->next = NULL; return OK; } //遍历链表中的数据输出 Status CheckList(Node list) { if (!list.next) { return ERROR; } NodePoint p; p = list.next; while (p) { cout << p->data << "\t"; p = p->next; } cout << endl; return OK; } int main() { /*int1 p; int x = 9; p = &x; cout << *p << endl;*/ Node list; cout << "初始化:" << InitList(&list, 10) << endl; ElemType value; cout << "获取:"<<GetElem(&list, 9, &value) << endl;; cout << "value:" << value << endl; //增加一个数据 cout << InsertList(&list, 9, 66) << endl; cout << CheckList(list) << endl; cout << DeleteList(&list, 9, &value) << endl; cout << value << endl; cout << CheckList(list) << endl; cout << ClearList(&list) << endl; cout << CheckList(list) << endl; system("pause"); return 0; }
以上是自己学习过程中的日记。
数据结构学习日记2:实现一个简单的线性表功能(链式存储方式)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。