顺序表
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define MAXSIZE 100
typedef int ElemType;
typedef enum {
OVERFLOW = -1,ERROR, OK
}Status;
typedef enum {
FALSE, TRUE
}Bool;
struct list {
ElemType *base;
int length;
};
typedef struct list list;
typedef list *p_list;
Status init(p_list pl) {
pl->base = (ElemType *)malloc(sizeof(ElemType) * MAXSIZE);
if (!pl->base) {
exit(OVERFLOW);
}
pl->length = 0;
return OK;
}
Status destory(p_list pl) {
if (!pl) {
return ERROR;
}
free(pl->base);
pl->base = NULL;
pl->length = 0;
return OK;
}
Status clear(p_list pl) {
if (!pl) {
return ERROR;
}
pl->length = 0;
return OK;
}
int length(p_list pl) {
assert(pl);
return pl->length;
}
Bool isEmpty(p_list pl) {
assert(pl);
return pl->length == 0 ? TRUE : FALSE;
}
Status getData(p_list pl, int index, ElemType* pe) {
assert(pl);
if (index < 1 || index >= pl->length) {
return ERROR;
}
*pe = pl->base[index - 1];
return OK;
}
int locate(p_list pl, ElemType e) {
int i;
assert(pl);
for (i = 0; i < pl->length; i++) {
if (*(pl->base + i) == e) {
return i + 1;
}
}
return -1;
}
Status insert(p_list pl, int index, ElemType e) {
int i;
ElemType* newbase;
if (pl->length + 1 > MAXSIZE) {
newbase = (ElemType *)malloc(sizeof(ElemType) * 2 * MAXSIZE);
for (i = 0; i < pl->length; i++) {
newbase[i] = pl->base[i];
}
free(pl->base);
pl->base = newbase;
}
if (index < 1 || index >= pl->length){
return ERROR;
}
for (i = pl->length; i > index; i--) {
pl->base[i] = pl->base[i - 1];
}
pl->base[i] = e;
pl->length++;
return OK;
}
Status listDelete(p_list pl,int index,ElemType *pe) {
if (index<1 || index>pl->length) {
return ERROR;
}
*pe = pl->base[index - 1];
for (int i = index - 1; i < pl->length; i++) {
pl->base[i] = pl->base[i + 1];
}
pl->length--;
return OK;
}
int main()
{
return 0;
}
链表
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int ElemType;
typedef enum {
OVERFLOW = -1,ERROR , OK
}Status;
typedef enum {
FALSE,TRUE
}Bool;
struct Node {
ElemType data;
struct Node *next;
};
typedef struct Node Node;
typedef Node *p_Node;
typedef Node List;
typedef Node *p_list;
Status init(p_list *ppl) {
*ppl = (p_list)malloc(sizeof(Node));
if (!*ppl) {
return ERROR;
}
(*ppl)->next = NULL;
return OK;
}
Status destory(p_list pl){
p_Node pn;
while (pl)
{
pn = pl;
pl = pl->next;
free(pn);
}
return OK;
}
Status clear(p_list pl) {
p_Node p,q;
if (!pl) {
return ERROR;
}
p = pl->next;
while (p)
{
q = p;
p = p->next;
free(q);
}
pl->next = NULL;
return OK;
}
int length(p_list pl)
{
p_Node p;
p = pl->next;
int i = 0;
while (p)
{
i++;
p = p->next;
}
return i;
}
Bool isEmpty(p_list pl) {
if (pl->next != NULL) {
return FALSE;
}
return TRUE;
}
Status getElem(p_list pl,int index,ElemType *e) {
p_Node p;
p = pl->next;
int i = 1;
while (p||i<index)
{
p = p->next;
i++;
}
if (i == index) {
*e = p->data;
}
else {
return ERROR;
}
return OK;
}
p_Node findElem(p_list pl, ElemType e) {
p_Node p;
p = pl->next;
while (p&&p->data!=e)
{
p = p->next;
}
return p;
}
Status insert(p_list pl, int index, ElemType e) {
p_Node p;
p = pl->next;
int i = 0;
while (p&&i<index-1)
{
p = p->next;
i++;
}
if (!p || i > index - 1)
{
return ERROR;
}
p_Node node;
node->data = e;
node->next = p->next;
p->next = node;
return OK;
}
Status Delete(p_list pl, int index, ElemType *e) {
p_Node p;
p = pl->next;
int i = 0;
while (p->next&&i < index - 1)
{
p = p->next;
i++;
}
if (!(p->next) || i > index - 1) {
return ERROR;
}
p_Node q;
q = p->next;
*e = q->data;
p->next = q->next;
free(q);
return OK;
}
void createHead(p_list pl,int n) {
p_Node p;
p->next = NULL;
for (int i = 0; i < n; i++) {
p_Node q;
scanf("%d", &q->data);
q->next = p->next;
p->next = q;
}
}
void createHead(p_list pl, int n) {
p_Node p;
p->next = NULL;
p_Node r;
r = p;
for (int i = 0; i < n; i++) {
p_Node q;
scanf("%d", &q->data);
q->next = NULL;
r->next = q;
r = q;
}
}
int main() {
p_list* pl;
init(pl);
return 0;
}