#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);
/*传入结构体指针后,可以修改内部的值,除了自己存储的地址,在函数内部移动指针,外部指针依旧是头指针*/
}
?
|