求解释!!! 每个步骤的清晰解释
# include "stdafx.h"# include<iostream>
#include<conio.h>
using namespace std;
int len;
class node
{
struct Snode
{
int data;
struct Snode *next;
}S,*head;
public:
void create();
void search();
void delnode();
}L;
void node::create()
{
int n;
struct Snode *p,*q;
head=(Snode *)malloc(sizeof(S));
head->next=NULL;
q=head;
cout<<"要建立链表的大小为:";
cin>>n;
len=n;
cout<<"请依次输入链表元素:";
for(int i=0;i<n;i++)
{
p=(Snode *)malloc(sizeof(S));
cin>>p->data;
p->next=NULL;
p->next=q->next;
q->next=p;
q=p;
}
cout<<"建立成功,按任意键返回";
getch();
}
void node::search()
{
int n;
cout<<"请输入要查找元素的位置:";
cin>>n;
struct Snode *p;
p=head;
if(n<0||n>len-1)
cout<<"超出链表长度";
else
{
while(n--)
{
p=p->next;
}
cout<<"所查找的元素为:"<<p->data;
cout<<"请按任意键返回";
getch();
}
}
void node::delnode()
{
int i=1;
int n;
struct Snode *q,*p;
p=head;
cout<<"请输入要删除元素的位置:";
cin>>n;
if(n<0||n>len-1)
cout<<"超出链表长度";
else
{
while(i!=n)
{
i++;
p=p->next;
}
q=p->next;
p->next=q->next;
cout<<"删除成功";
cout<<"按任意键返回";
getch();
}
}
int main()
{
int n;
while(1)
{
cout<<"1.创建链表"<<endl;
cout<<"2.查找元素"<<endl;
cout<<"3.删除元素"<<endl;
cout<<"4.结束操作"<<endl;
cout<<"请选择要执行的操作:";
cin>>n;
switch(n)
{
case 1:L.create();break;
case 2:L.search();break;
case 3:L.delnode();break;
case 4:return 0;
}
system("cls");
}
return 0;
}