Data Structure and Algorithms Using C++ by Unknown

Data Structure and Algorithms Using C++ by Unknown

Author:Unknown
Language: eng
Format: epub
Published: 2021-01-09T00:00:00+00:00


ALGORITHM FOR SEARCHING

7.4.10 /* SEARCH A NODE INTO A SIMPLE LINKED LIST WITH INFORMATION IS KNOWN */

#include<iostream> #include<iomanip> #include<stdlib.h> using namespace std; struct link { int info; struct link *next; }; struct link start, *new1,*node; /* Function main */ void create(struct link *); void display (struct link *); void search(struct link *); int main() { create(node); printf(“
THE CREATED LINKED LIST IS :
”); display(node); search (node); } void create(struct link *node) /*LOGIC TO CREATE A LINK LIST*/ { char ch=’y’; start.next = NULL; node = &start; /* Point to the start of the list */ while(ch ==’y’ || ch==’Y’) { node->next = (struct link* ) malloc(sizeof(struct link)); node = node->next; cout<<”
ENTER A NUMBER : “; cin>>node->info; node->next = NULL; cout<<”
DO YOU WANT TO CRTEATE MORE NODES: “; cin>>ch; } } void display(struct link *node) { /*DISPLAY THE LINKED LIST*/ node = start.next; while (node) { cout<<setw(5)<<node->info; node = node->next; } } void search(struct link *node) { int val; int flag = 0,n=0; node = &start ; cout<<”
ENTER THE NUMBER TO SEARCH”; cin>>val; if (node == NULL) { cout<<”
List is empty”; } while(node) { if( val == node->info ) { cout<<”
THE NUMBER “<<val<<” IS AT “<<n<<” POSITION IN THE LIST”; node = node->next; flag = 1; break; } else { node = node->next; } n++; } if(!flag) { cout<<”
THE NUMBER %d IS NOT FOUND IN THE LIST”<<val; } }

OUTPUT



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.