void deleteL(LinkNode *head,char a)//此链表假定带头结点,否则还要加一条对第一个结点的操作。LinkNode为定义的链表类型,a为指定的要删除的字符。
{
ListNode *p,*q;
p=head;
while (p->next!=NULL)
{if (p->next->data==a)
{q=p->next;
p->next=p->next->next;//删除指定的字符
free(q);
else
p=p->next;//不删除,只是指针后移
}
}
#include
using namespace std;
typedef struct LNode
{
char data;
struct LNode *next;
}LNode,*LinkList;
void create(LinkList *L);
void print(LinkList L);
void del(LinkList *L, char a);
int main()
{
LinkList L;
create(&L);
print(L);
del(&L, 'b');
print(L);
system("pause");
return 0;
}
void create(LinkList *L)
{
int n;
*L = new LNode;
(*L)->next = NULL;
cout<<"input a number:";
cin>>n;
LinkList node,fnode;
node = fnode = *L;
for(int i = 0; i < n; i++)
{
node = new LNode;
cin>>node->data;
node->next = NULL;
fnode->next = node;
fnode = node;
}
}
void print(LinkList L)
{
if(L->next == NULL)
cout<<"Empty List"<
{
LinkList p = L->next;
while(p)
{
cout<< p->data <<" ";
p = p->next;
}
}
cout<
void del(LinkList *L, char a)
{
LinkList node,fnode;
fnode = *L;
node = (*L)->next;
while(node != NULL)
{
if(node->data == a)
{
fnode->next = node->next;
delete node;
node = fnode->next;
cout<<"delete successfully"<
{
fnode = node;
node = node->next;
}
}
if(node)
{
fnode->next = node->next;
delete node;
cout<<"delete successfully"<
else
cout<<"can not delete"<
在visual studio 2008里成功运行了。的