问题描述:
根据给定的整型数组,以尾插法建立一个单链表,并实现以下操作:
① 查找:输入一个欲查找的整数,找到则显示第一个相匹配的整数在单链表中所处的位置,若不存在,则显示提示信息。
② 删除:输入一个欲删除的整数e ,若存在则在单链表中删除第一个值为e 的元素。
③ 插入:输入一个欲插入位置i 和欲插入元素e,将e 插入到第i 个整数之前(注意i 的合法性)。
源代码:
#include <iostream>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
using namespace std;
typedef struct node{
int data;//数据域,int可换成任意数据元素,通常为struct结构体数据。
struct node* next;
}Node,*Link;
//struct node st; 等价于 Node st;
//struct node *p; 等价于 Link p;
//p=(Link)malloc(sizeof(Node));//malloc动态分配内存空间,返回void*,Link用来转换void*类型
//尾插法创建链表,就是多定义个可移动的指针,在尾部不断插入新节点,最后一个节点的指针域赋为null
Link newList(int a[],int n){
Link head =NULL;
head=(Link)malloc(sizeof(Node)); //生成头结点
head->next = NULL;
Link rear =NULL;
rear=(Link)malloc(sizeof(Node));
rear = head;
for(int i = 0; i < n; i++ ){
Link node = NULL;
node = (Link)malloc(sizeof(Node));
node->data = a[i];
rear->next = node;
rear = rear->next;
}
rear->next=NULL;
return head;
}
void displayNode(Link head)//遍历
{
Link p =NULL;
p = (Link)malloc(sizeof(Node));
p=head->next;
while(p!=NULL){
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}
bool findNode(Link head,int x){//查找
Link p =NULL;
p= (Link)malloc(sizeof(Node));
p=head->next;
int count=0;
while(p!=NULL){
if(p->data == x){
cout<<"找到了!位置为:"<<count<<endl;
return true;
}
else{
p=p->next;
count++;
}
}
return false;//循环结束没找到, 没这个元素
}
bool deleteNode(Link head,int x){//删除
if(head==NULL || head->next == NULL){//判断空表,提前返回False
return false;
}
Link q=NULL;
Link p=NULL;
q=(Link)malloc(sizeof(Node));
p=(Link)malloc(sizeof(Node));
q=head;
p=head->next;//p用来查找要删除的结点,q是p的前驱节点
while(p!=NULL){
if(p->data==x){
q->next = p->next;//找到删除节点
free(p);
return true;
}
else{//没找到,继续往后移动
q = p;
p = p->next;
}
}
return false;//循环结束,仍没找到,则此链表没有这个数据;
}
bool insertNode(Link head,int i,int e){//i位置,e元素 ,将e插入位置i前
Link p = head;
int count = 0;
while(p!=NULL && count <i-1){//让p移动到要插入的节点位置前一个节点
//NULL防止链表移动过头,count用来找到p的位置
p=p->next;
count++;
}
if(p==NULL){//没有这个节点
return false;
}
else{//找到位置,插入新节点
Link node = NULL;
node = (Link)malloc(sizeof(Node));
node->data = e;
node->next = p->next;//node连接p的下一个节点
p->next=node;//p连接node,原本p与p->next的连接自动断开
return true;
}
}
void clearLink(Link head){
Link p;
while(p!=NULL){
p=head;
head=head->next;
free(p);
}
}
void myMenu(){
printf(" * * * * * * * * * 菜 单 * * * * * * * * * *\n");
printf(" 1 查找 \n");
printf(" 2 删除 \n");
printf(" 3 插入 \n");
printf(" q 退出系统 \n");
printf(" * * * * * * * * * * * * * * * * * * * * * * * *\n");
}
int main(){
int a[10];
for(int i=1;i<=10;i++){
a[i-1]=i;
}
Link head = newList(a,10);
displayNode(head);
printf("\n");
while(1)
{
myMenu();
printf("\n请输入你的选择(1-3),q退出:"); //显示提示信息
char select;
scanf("%c",&select);
switch(select)
{
case '1'://查找
cout<<"请输入要查找的整数:";
int x;
cin>>x;
if(findNode(head,x)){
}
else{
cout<<"没有找到这个整数!"<<endl;
}
break;
case '2'://删除
cout<<"请输入要删除的整数:";
int x1;
cin>>x1;
if(deleteNode(head,x1)){
cout<<"已删除"<<x1;
cout<<"删除后的链表数据为:" <<endl;
displayNode(head);
}
else{
cout<<"没有找到这个整数!"<<endl;
}
break;
case '3'://插入
cout<<"请输入要插入整数的位置,数据:";
int i,x2;
cin>>i>>x2;
if(insertNode(head,i,x2)){
cout<<"已插入"<<x2;
cout<<"插入后的链表数据为:" <<endl;
displayNode(head);
}
else{
cout<<"这个位置不合法!,插入失败!"<<endl;
}
break;
case 'q'://退出
clearLink(head);
return 0;
default:
printf("输入不正确,应该输入1-3之间的数。\n\n");
break;
}
getchar();
}
}
查找:
删除:
插入: