📄 oneline.h
字号:
#include "stdafx.h"
#include "iostream.h"
#include "malloc.h"
//****************************定义数据类型*********************************
typedef struct linknode//定义接点类型
{
char data;
struct linknode *next;
}nodetype;
//**************************建立单链表并负值*******************************
nodetype *create()
{
char d;
nodetype *h=NULL,*s,*t;
int i=1;
cout <<"建立单链表<<endl<<请输入结点值以输入0为输入结束"<<endl;
while (i)
{
cout <<"输入第"<< i <<"个结点值 ";
cin >>d;
if (d=='0')
break;
if (i==1)//建立第一个结点
{
h=(nodetype *)malloc(sizeof(nodetype));
h->data=d;
h->next=NULL;
t=h;
}
else//建立其余的
{
s=(nodetype *)malloc(sizeof(nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;
}
return h;
}
//********************************输出数据********************************
void disp(nodetype *h)
{
nodetype *p=h;
cout <<" ";
if (p==NULL)
cout <<"空表";
while (p!=NULL)
{
cout << p->data<<" ";
p=p->next;
}
cout << endl;
}
//*******************************计算链表长度******************************
int len(nodetype *h)
{
int i=0;
nodetype *p=h;
while (p!=NULL)
{
p=p->next;
i++;
}
return i;
}
//******************************就地逆置单链表*****************************
nodetype *invert(nodetype *h)
{
nodetype *p,*q,*r;
if (len(h)<=1)
{
cout <<"至少为2个接点的链表"<<endl;
return NULL;
}
else
{
p=h;
q=p->next;
while (q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
h->next=NULL;
h=p;
return h;
}
}
//*******************************销毁单链表********************************
void dispose(nodetype *h)
{
nodetype *pa=h, *pb;
if (pa!=NULL)
{
pb=pa->next;
if (pb==NULL)
free(pa);
else
{
while (pb!=NULL)
{
free(pa);
pa=pb;
pb=pb->next;
}
free(pa);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -