#include <iostream.h>
typedef char datatype;
struct node{ //定义结构体node,建表
datatype data;
node * next;
};
struct linklist{
node *head;
int len;
};
linklist L;
node * creat(void){ //建立一个链表
node *head,*p,*q;char ch; int i=0;
head = new node;
head->next = NULL; p = head;
cout<<"=============================================\n";
cout<<"请随机输入数字,输入#+回车键结束!\n";
cout<<"=============================================\n";
cin>>ch;
while (ch!=''#'')
{
q = new node;
q->data = ch;
q->next = NULL;
p->next = q;
p = q;
cin>>ch;
i++;
}
L.head = head;
L.len = i;
return(head);
}
void out(node *head) //输出一个链表
{
head = L.head ;
node *a;
a = head->next;
while(a!=NULL)
{
cout<<a->data<<" ";
a = a->next;
}
}
node * Loc(linklist D,int i) //定位第i个结点
{
node *p;int j;
j = 0;p = D.head;
if((i>=1)&&(i<=D.len))
while(j<i)
{p=p->next;
j++;
}
return(p);
}
void Insert(linklist &C,int i,datatype x) //插入一个结点
{
node *q,*p;
if(i<0||i>C.len) cout<<"there is a error";
else{
p = Loc(C,i);
q = new node;
q->data = x;
q->next = p->next ;
p->next = q;
C.len++;
}
}
void Del(linklist &D,int i) //删除一个结点
{
node *p,*q;
if((i<1)||(i>D.len))
cout<<"error";
else
{
p=Loc(D,i-1);
q = p->next;
p->next = q->next;
delete q;
D.len--;}
}
void main(void)
{
creat();
cout<<"=============================================\n";
cout<<"由您输入的数字构成的原始链表为:\n"<<endl;
out(L.head);
cout<<endl;
Insert(L,3,''c'');
cout<<"=============================================\n";
cout<<"插入一个数字后的结果:\n"<<endl;
out(L.head);
cout<<endl;
Del(L,3);
&