3.6 链式存储结构(大话)c++实现 21.10.08

#include "stdio.h"    
#include "string.h"
#include "ctype.h"      
#include "stdlib.h"  
#include"iostream"
#include"typeinfo"

#include "math.h"  
#include "time.h"
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 20 /* 存储空间初始分配量 */

typedef int Status;/* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int ElemType;/* ElemType类型根据实际情况而定,这里假设为int */


typedef struct node {
	/*数据区*/
	ElemType shu;
	/*链子区*/
	struct node* next;
}Node;

/*将结构体的指针名字定义成这个*/
typedef struct node* Linklist;

/*结构体指针,序号,要插入的值*/
Status insert(Linklist l, int index, ElemType e)
{
	Linklist p,s;
	p = l;
	int j = 1;
	while (j < index && p)
	{
		p = p->next;
		++j;
	}
	if (j > index || !p)
	{
		cout << "失败了哦"<<endl;
		return FALSE;
	}
	s = (Linklist)malloc(sizeof(Node));
	s->shu = e;
	s->next = p->next;
	p->next = s;
	cout << "添加成功了哦" << endl;
	return OK;



}

/*初始化链表*/
Status initlist(Linklist l)
{
	Linklist p;
	l->next = NULL;
	for (int i = 10; i > 0; i--)
	{
		p = (Linklist)malloc(sizeof(Node));
		p->shu = i;
		p->next = l->next;
		l->next = p;

	}
	cout << "win";
	return OK;
}

Status del(Linklist l, int index)
{
	Linklist p,s;
	int j = 1;
	p = l;
	while (j < index&&p->next)
	{
		p = p->next;
		++j;
	}
	if (j > index || !p->next)
	{
		cout << "删除失败了哦" << endl;
		return FALSE;
	}
	s=p->next;
	p->next = s -> next;
	free(s);
	cout << "删除成功" << endl;
	return OK;

}

Status clearlist(Linklist l)
{
	Linklist p = l->next,s;
	while (p->next)
	{
		s= p->next;

		free(p);
		p = s;
	}

	return OK;

}

Status search(Linklist l, int index)
{
	int j = 0;
	while (j < index&& l)
	{
		l = l->next;
		++j;
	}
	if (j > index || !l)
	{
		cout << "找不到哦"<<endl;
		return FALSE;
	}

	cout << "找到值为:" << l->shu<<endl;
	return OK;
}



int main() {
	/*创建一个链表*/
	Node l;
	initlist(&l);
	Linklist p, s;
	s = &l;
	
	for (int i = 1; i < 11; i++)
	{
		s = s->next;
		cout << s->shu <<" ";
		
	}
	/*cout << "ll type" << typeid(*ll).name()<<endl;
	cout << "ll zhi" << ll<<endl;
	
	cout << "ll zhi2" << *ll;*/
//	*ll = &l;
	insert(&l, 10, 99);
	s = &l;
	s = s->next;
	while (s)
	{
		cout << s->shu << " ";
		s = s->next;


	}
	del(&l, 2);
	s = &l;
	s = s->next;
	while (s)
	{
		cout << s->shu << " ";
		s = s->next;


	}
	search(&l, 9);
	//clearlist(&l);
	/*传入结构体指针后,可以修改内部的值,除了自己存储的地址,在函数内部移动指针,外部指针依旧是头指针*/
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值