鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > c++ > >

数据结构,链式线性表

来源:互联网 作者:佚名 时间:2016-06-06 10:04
线性表,链式存储 结构体内存分配 插入数据 获取数据 无 #include "stdafx.h"#include "stdlib.h"#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef int Status;typedef int ElemType;typedef struct Node {ElemType data;struct Node *next;
线性表,链式存储
结构体内存分配
插入数据
获取数据 <无>
#include "stdafx.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct Node {
	ElemType data;
	struct Node *next;
}Node;

typedef struct Node *LinkList;

Status GetElem(LinkList L,int i,ElemType *e) {
	int j;
	LinkList p;
	p = L->next;
	j = 1;
	while (p && j<i)
	{
		p = p->next;
		++j;
	}
	if (!p || j>i) {
		return ERROR;
	}
	*e = p->data;
	return OK;
}

Status ListInsert(LinkList *L,int i,ElemType e) {
	int j;
	LinkList p, s;
	p = *L;
	j = 1;
	while (p && j<i)
	{
		p = p->next;
		++j;
	}
	if (!p || j>i)
		return ERROR;
	s = (LinkList)malloc(sizeof(Node));
	s->data = e;
	s->next = p->next;
	p->next = s;
	return OK;
}



int main() {
	LinkList n = (LinkList)malloc(sizeof(Node));
	int y = ListInsert(&n,1,22);
	printf("%d\n",y);
	int num = 0;
	int y1 = GetElem(n,1,&num);
	printf("y1:%d\n",y1);
	printf("num:%d\n",num);
	while (true)
	{

	}
	return 0;
}
网友评论
<