#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
typedef struct node
{
char data;
struct node* next;
}node, * pnode;
pnode create_list()
{
int i;
pnode head = (pnode)malloc(sizeof(node));
if (!head)
{
cout << "未成功分配内存" << endl;
exit(-1);
}
pnode p = head;
for (i = 0; i < 25; i++)
{
p->data = 'A' + i;
p->next = (pnode)malloc(sizeof(node));
if (!p->next)
{
cout << "未成功分配内存" << endl;
exit(-1);
}
p = p->next;
}
p->data = 'A' + 25;
p->next = NULL;
return head;
}
int show_list(pnode p)
{
while (p!= NULL)
{
cout << p->data << '-' << '>';
p = p->next;
}
if (p== NULL)
{
cout << "NULL" << endl;
}
return 0;
}
int insert_list(pnode p)
{
char a;
cin >> a;
pnode pnew = (pnode)malloc(sizeof(node));
if (!pnew)
{
cout << "内存未分配成功" << endl;
exit(-1);
}
pnew->data = a;
pnew->next = NULL;
while (p != NULL)
{
if (p->data != a)
{
cout << "wrong" <<endl;
p = p->next;
}
else
{
pnew->next = p->next;
p->next = pnew;
break;
}
}
return 0;
}
int delete_list(pnode p)
{
char a;
cin >> a;
pnode q;
q = NULL;
while (p != NULL)
{
if (p->data != a)
{
cout << "wrong" << endl;
q = p;
p = p->next;
}
****if (p->data == a)****
{
q->next= p->next;
free(p);
}
}
return 0;
}
int main()
{
pnode phead;
phead=create_list();
show_list(phead);
insert_list(phead);
show_list(phead);
delete_list(phead);
show_list(phead);
return 0;
}