資料結構筆記(二)——線性表(Linear List)
阿新 • • 發佈:2018-12-15
個人學習筆記,不喜勿噴。
一開始寫程式碼時糾結於在C++中是用struct還是用class。
C++可以使用struct和class,並且C++中與C中的struct不一樣,它更像class。
用struct實現資料結構,class用於物件。
線性表:由同類型資料元素構成有序序列的線性結構。包括資料物件集和操作集。
一、順序儲存實現(陣列實現)
利用陣列的連續儲存空間順序存放線性表各元素
arrlist.h
#ifndef ARRLIST_H #define ARRLIST_H #define MAXSIZE 20 #include<iostream> using namespace std; struct LNode; typedef int ElementType; typedef int Position; typedef struct LNode *List; void init(List l); bool isEmpty(List l); bool isLast(List l,Position p); Position findX(List l, ElementType x); ElementType findKth(List l, int k); void insertX(List l, ElementType x, Position p); void deleteP(List l, Position p); void deleteX(List l, ElementType x); void deleteL(List l); void printL(List l); typedef struct LNode { ElementType *data;//指向存線性表的動態儲存空間的指標 Position last;//最後一個元素在陣列中的位置 }ArrList; #endif
ArrList.cpp
#include "stdafx.h" #include"arrlist.h" //初始化 void init(List l) { l->data = new ElementType[MAXSIZE](); if (l->data==NULL) { cout << "空間用完" << endl; exit(1); } l->last = -1; } //是否為空表 bool isEmpty(List l) { return l->last < 0; } //是否為表末尾 bool isLast(List l,Position p) { return p == l->last; } //查詢第一次出現的元素x,找不到返回-1 Position findX(List l, ElementType x) { for (Position i = 0; i <= l->last;++i) { if (x==l->data[i]) { return i; } } return -1; } //返回第k個元素 ElementType findKth(List l, int k) { if (k-1>l->last||k-1<0) { cerr << "k無效" << endl; } return l->data[k-1]; } //在位置p處插入x void insertX(List l, ElementType x, Position p) { if (l->last+2>MAXSIZE||p<0) { cerr << "p無效" << endl; } else { for (Position i = l->last; i >= p;--i) { l->data[i + 1] = l->data[i]; } l->data[p] = x; ++l->last; } } //刪除p處元素 void deleteP(List l, Position p) { if (p==-1||p>l->last) { cerr << "無該元素或輸入p無效" << endl; } for (Position i = p + 1; i <= l->last; ++i) { l->data[i - 1] = l->data[i]; } l->data[l->last] = 0; --l->last; } //刪除表中第一個x void deleteX(List l, ElementType x) { Position p = findX(l, x); deleteP(l, p); } void deleteL(List l) { if (l->data!=NULL) { delete[]l->data; l->data = NULL; } } void printL(List l) { if (l->last<0) { cerr << "空表" << endl; } else { for (Position i = 0; i < l->last;++i) { cout << l->data[i]<<ends; } cout << l->data[l->last] << endl; } }
main.cpp
#include "stdafx.h" #include "arrlist.h" int _tmain(int argc, _TCHAR* argv[]) { ArrList arrlist; List l = &arrlist; init(l); int input; for (int i = 0; i < 10;++i) { cin >> input; insertX(l, input, i); } printL(l); deleteP(l, 5); printL(l); deleteX(l,10); printL(l); cout << findX(l,5) << endl; cout << findKth(l,4) << endl; return 0; }
二、鏈式儲存實現(連結串列實現)
list.h
#ifndef LIST_H
#define LIST_H
#include<iostream>
using namespace std;
typedef int ElementType;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node
{
ElementType element;//結點資料
Position next;//指向下一個結點的指標
};
void init(List l);
int isEmpty(List l);
int isLast(Position p, List l);
Position findX(ElementType x, List l);
void deleteX(ElementType x, List l);
Position findPre(ElementType x, List l);
void insertX(ElementType x, List l,Position p);
void deleteL(List l);
Position header(List l);
Position first(List l);
Position advance(Position p);
void printL(List l);
#endif
List.cpp
#include "stdafx.h"
#include "list.h"
void init(List l)
{
l->element = 0;
l->next = nullptr;
}
int isEmpty(List l)
{
return l->next == nullptr;
}
int isLast(Position p, List l)
{
return p->next == nullptr;
}
Position findX(ElementType x, List l)
{
Position p;
p = l->next;
while (p != nullptr&&p->element != x)
{
p = p->next;
}
return p;
}
Position findPre(ElementType x, List l)
{
Position p;
p = l;
while (p->next != nullptr&&p->next->element != x)
{
p = p->next;
}
return p;
}
void deleteX(ElementType x, List l)
{
Position p = findPre(x, l);
if (!isLast(p, l))
{
Position tmp = p->next;
p->next = tmp->next;
delete tmp;
}
}
//插在p的後面
void insertX(ElementType x, List l,Position p)
{
Position tmp;
tmp = new Node();
if (tmp == nullptr)
{
cerr<<"out of space" << endl;
}
tmp->element = x;
tmp->next = p->next;
p->next = tmp;
}
void deleteL(List l)
{
Position p, tmp;
p = l->next;
l->next = nullptr;
while (p != nullptr)
{
tmp = p->next;
delete p;
p = tmp;
}
}
Position header(List l)
{
return l;
}
Position first(List l)
{
return l->next;
}
Position advance(Position p)
{
return p->next;
}
void printL(List l)
{
Position p = l->next;
while (p!=nullptr)
{
cout << p->element << ends;
p = advance(p);
}
cout << endl;
}
main.cpp
#include "stdafx.h"
#include "list.h"
int _tmain(int argc, _TCHAR* argv[])
{
Node node;
init(&node);
ElementType input;
Position p = header(&node);
cout << isEmpty(&node) << endl;
while (cin>>input)
{
insertX(input,&node,p);
p = advance(p);
}
printL(&node);
insertX(100, &node, header(&node));
printL(&node);
deleteX(100, &node);
printL(&node);
return 0;
}