初学数据结构,求大家帮忙看下,把每行代码注释下就OK,顺便说下功能。
谢谢了#include<iostream>
using namespace std;
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
int del(linklist *s,int x)
{
linklist *p,*q,*r;
int i = 0;
r = s;
while(i<x)
{
r = r->next;
i++;
}
p = r;
while(p->next->next !=r)
p = p->next;
q = p->next;
p->next = r;
free(q);
return q->data;
}
void main()
{
linklist *p,*q,*s,*r;
datatype x;
int y;
s = (linklist *)malloc(sizeof(linklist));//不加(linklist *)就错!!
s->next = NULL;
q = s;
cout<<"请输入要创建的链表的数据"<<endl;
cin>>x;
s->data = x;
while(x!=0)
{
p = (linklist *)malloc(sizeof(linklist));
p->data = x;
q->next = p;
q = p;
cin>>x;
}
q->next = s;
cout<<"请输入要删除第y个结点的结点的前一个结点"<<endl;
cin>>y;
del(s,y);
r = s->next;
while(r != s)
{
cout<<r->data<<endl;
r = r->next;
}
}