问题描述
//声明标题
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<conio.h>
//创建有数据和下一个ptr的节点
struct Node{ //creating node
int data;
struct Node *next;
}node;
struct Node *head;
//函数声明
void start_insertion();
void end_insertion();
void display();
void start_deletion();
void end_deletion();
void searching();
//主函数
int main(){
int ch;
do{
printf("\n1: Insertion in begining\n2: Insertion in End\n");
printf("3: Deletion from beigning\n4: Deletion from End\n");
printf("5: search\n 0: Exit\n");
display();
printf("\nEnter the choice : ");
scanf("%1d",&ch);
switch(ch){
case 1: start_insertion();
break;
case 2: end_insertion();
break;
case 3: start_deletion();
break;
case 4: end_deletion();
break;
case 5: searching();
break;
case 0: exit(0);
break;
default:printf("Entered wrong choice\n");
}
//system("cls");
}while(ch!=0);
return 0;
}
//从开始插入
void start_insertion() {
struct Node *ptr,*temp;
ptr=(struct Node*)malloc(sizeof(struct Node));
if(ptr==NULL){
printf("overflow");
}
else{
int item;
printf("Enter the data: ");
scanf("%d",&item);
if(head==NULL){
ptr->data=item;
ptr->next=NULL;
head=ptr;
}
else {
temp=head;
ptr->data=item;
ptr->next=temp;
head=ptr;
}
}
}
//插入到最后
void end_insertion(){
struct Node *ptr,*temp;
ptr=(struct Node*)malloc(sizeof(struct Node));
if(ptr==NULL){
printf("Memory overflow");
}
else{
int item;
printf("\nEnter the data : ");
scanf("%d",&item);
if(head==NULL){
ptr->data=item;
ptr->next=NULL;
head=ptr;
}
else{
temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=ptr;
ptr->data=item;
ptr->next=NULL;
}
}
}
//从头开始删除
void start_deletion(){
if(head==NULL){
printf("\n************List is Empty******************\n");
}
else{
struct Node *temp;
temp=head;
head=temp->next;
free(temp);
}
}
//从末尾删除
void end_deletion(){
struct Node *temp,*temp1;
temp=head;
if(temp==NULL){
printf("\n**********List is empty************\n");
}
else{
if(head->next==NULL){
head=NULL;
free(head);
}
else{
while(temp->next!=NULL){
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
free(temp);
}
}
}
//遍历整个链表展示
void display(){
struct Node *temp;
temp=head;
if(temp==NULL){
printf("\nNo Node existed\n");
}
else{
while(temp!=NULL){
printf("-> %d ->",temp->data);
temp=temp->next;
}
}
}
void searching(){
if(head==NULL){
printf("List is Empty cant search");
}
else{
struct Node *temp;
temp=head;
int flag=0,p[flag],item,i=0;
printf("Enter the data you want to search: ");
scanf("%d",&item);
while(temp!=NULL){
if(temp->data==item){
p[flag]=i;
flag++;
}
i+=1;
temp=temp->next;
}
if(flag==0){ //if flag=0 the their is no data in the list
printf("no data element exist");
}
else{
for(int j=0;j<=flag;j++){
printf("*******found********%d",p[j]+1);
}
}
}
}
解决方法
void searching(){
if(head==NULL){
printf("List is Empty cant search");
}
else{
struct Node *temp;
temp=head;
int flag=0,item,i=0;
int p[100];
printf("Enter the data you want to search: ");
scanf("%d",&item);
while(temp!=NULL){
if(temp->data==item){
p[flag] = i;
flag++;
}
i+=1;
temp=temp->next;
}
if(flag==0){ //if flag=0 the their is no data in the list
printf("no data element exist");
}
else{
for(int j=0;j<flag;j++){
printf("*******found********%d",p[j]+1);
}
}
}
}
我像上面一样编辑您的搜索功能。
有两个问题:
首先,您必须使用元素数初始化数组,您的代码只是使用 flag
元素初始化数组,而此行中的 flag
为 0。
int flag=0,p[flag],i=0;
第二,因为您的跟踪数组 p
从 0 开始,带有 flag
元素,所以您只需从 0 -> flag-1 开始,所以我编辑您的 for 循环以打印结果:
for(int j=0;j<flag;j++)
但我认为你最好用其他方式在链表中搜索:
当你遍历你的链表时,如果遇到数据,就可以打印结果并打破循环。
如果您遇到列表的末尾(NULL 指针)但仍未找到匹配的数据,则表示列表不包含您想要的数据。
无需使用额外的变量 flag
或 p
数组来跟踪结果。
像这样:
void searching() {
if (head == NULL) {
printf("List is Empty cant search");
} else {
struct Node *temp;
temp = head;
int item,i = 0;
printf("Enter the data you want to search: ");
scanf("%d",&item);
while (temp != NULL) {
if (temp->data == item) {
printf(" found %d at %d",i);
break;
}
i += 1;
temp = temp->next;
}
if (temp == NULL) printf("no data element exist");
}
}
,
我使用在线编译器来测试代码
-> 65 ->-> 45 ->-> 89 ->-> 12 ->-> 8 ->-> 45 ->
Enter the choice : 5
Enter the data you want to search: 45
*******found********2*******found********6
添加它可以工作,但只需在此处删除 =
for(int j=0;j<flag;j++){ //j<flag not j<=flag
printf("*******found********%d",p[j]+1);
}