删除链表中指定结点

来源:互联网 发布:开淘宝网需要垫资金吗 编辑:程序博客网 时间:2024/04/29 20:21

原题:使用后置递归法求解删除带有头结点的单链表中所有值为x的数据元素的算法。

(1)采用后置递归算法删除指定结点

#include<iostream.h>#include<stdlib.h>#include<time.h>typedef struct LNode{//定义表结点结构char data;struct LNode *next;}LNode,*LinkList;int CreateList(LinkList &head)//生成链表{srand((unsigned)time(NULL));LinkList p=NULL,q=NULL;head=NULL;for(int i=0;i<20;i++){p=new LNode;p->data=(char)(rand()%26+'a');if(head == NULL)head=p;elseq->next=p;q=p;}if(head!=NULL){q->next=NULL;}head->data='*';//头结点数据域为特殊字符*号return 1;}void DelListNode(LinkList &head,char ch){LinkList h,p;if(head->next){if(head->next->data == ch){//删除head->nextp=head->next;head->next=p->next;delete p;h=head;//h为线性链表(a2,a3, ... an)的头指针}elseh=head->next;//h为线性链表(a2,a3, ... an)的头指针DelListNode(h,ch);//处理线性链表(a2,a3, ... an)}}int DisplayList(LinkList head)//打印链表中所有结点的数据{if(head==NULL){cout<<"当前链表为空!"<<endl<<endl;return -1;}LinkList p=head;while(p->next != NULL){cout<<p->data<<" -> ";p=p->next;}cout<<p->data<<endl<<endl;//打印最后一个结点的数据return 1;}void main(){LinkList head;CreateList(head);cout<<"初始生成的链表的数据为:"<<endl;DisplayList(head);char ch;cout<<"你要删除链表中的哪个字母,请输入:";cin>>ch;DelListNode(head,ch);cout<<"删除字母后"<< ch <<"的链表的数据为:"<<endl;DisplayList(head);}


(2)在遍历过程中删除指定结点

#include<iostream.h>#include<stdlib.h>#include<time.h>typedef struct LNode{//定义表结点结构char data;struct LNode *next;}LNode,*LinkList;int CreateList(LinkList &head)//生成链表{srand((unsigned)time(NULL));LinkList p=NULL,q=NULL;head=NULL;for(int i=0;i<20;i++){p=new LNode;p->data=(char)(rand()%26+'a');if(head == NULL)head=p;elseq->next=p;q=p;}if(head!=NULL){q->next=NULL;}head->data='*';//头结点数据域为特殊字符*号return 1;}void DelListNode(LinkList &head,char ch){//在遍历过程中删除指定结点(数据域值为ch的结点)LinkList h,p,pre=head;while(pre->next){if(pre->next->data == ch){p=pre->next;pre->next=p->next;delete p;}if(pre->next != NULL){pre=pre->next;}}}int DisplayList(LinkList head)//打印链表中所有结点的数据{if(head==NULL){cout<<"当前链表为空!"<<endl<<endl;return -1;}LinkList p=head;while(p->next != NULL){cout<<p->data<<" -> ";p=p->next;}cout<<p->data<<endl<<endl;//打印最后一个结点的数据return 1;}void main(){LinkList head;CreateList(head);cout<<"初始生成的链表的数据为:"<<endl;DisplayList(head);char ch;cout<<"你要删除链表中的哪个字母,请输入:";cin>>ch;cout<<endl;DelListNode(head,ch);cout<<"删除字母后"<< ch <<"的链表的数据为:"<<endl;DisplayList(head);}


(3)消除递归(1)中的递归算法

#include<iostream.h>#include<stdlib.h>#include<time.h>typedef struct LNode{//定义表结点结构char data;struct LNode *next;}LNode,*LinkList;int CreateList(LinkList &head)//生成链表{srand((unsigned)time(NULL));LinkList p=NULL,q=NULL;head=NULL;for(int i=0;i<20;i++){p=new LNode;p->data=(char)(rand()%26+'a');if(head == NULL)head=p;elseq->next=p;q=p;}if(head!=NULL){q->next=NULL;}head->data='*';//头结点数据域为特殊字符*号return 1;}void DelListNode(LinkList &head,char ch){//将(2)中的递归算法改为非递归算法(即消除递归),删除指定结点(数据域值为ch的结点)LinkList h,p,la=head;while(la->next){if(la->next->data == ch){p=la->next;la->next=p->next;delete p;//删除la->nexth=la;//h为线性链表(a2,a3, ... an)的头指针}elseh=la->next;//h为线性链表(a2,a3, ... an)的头指针la=h;//la为线性链表(a2,a3, ... an)的头指针}}int DisplayList(LinkList head)//打印链表中所有结点的数据{if(head==NULL){cout<<"当前链表为空!"<<endl<<endl;return -1;}LinkList p=head;while(p->next != NULL){cout<<p->data<<" -> ";p=p->next;}cout<<p->data<<endl<<endl;//打印最后一个结点的数据return 1;}void main(){LinkList head;CreateList(head);cout<<"初始生成的链表的数据为:"<<endl;DisplayList(head);char ch;cout<<"你要删除链表中的哪个字母,请输入:";cin>>ch;cout<<endl;DelListNode(head,ch);cout<<"删除字母后"<< ch <<"的链表的数据为:"<<endl;DisplayList(head);}


 

原创粉丝点击