Skip to content

Commit

Permalink
Merge pull request #82 from TG1999/master
Browse files Browse the repository at this point in the history
added code for searching in a link list
  • Loading branch information
ambujraj committed Oct 1, 2018
2 parents 6452ae2 + d9ac0b5 commit ae871c9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
60 changes: 60 additions & 0 deletions linklist/search.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
using namespace std;

class Node{
public:
int data;
Node* next;
Node(int d){
data = d;
next = NULL;
}
};

Node* insertAtEnd(Node* head, int x, Node* &tail){
if (head == NULL){
head = new Node(x);
tail = head;
return head;
}

tail->next = new Node(x);
tail = tail->next;
return head;
}

Node* createLL(){
int x;
Node* head = NULL;
Node* tail = NULL;

while(true){
cin >> x;
if (x == -1) break;
head = insertAtEnd(head, x, tail);
}
return head;
}

int searchLL(Node* head,int data){
Node* cur = head;
int count=0;
while(cur != NULL){
count++;
if(cur->data==data){
return count;
}
}
return -1;
}
int main(){
//enter -1 for last number
Node* head=createLL();
//To search an element in link list use searchLL if not present it will return -1
// otherwise it will return index of element in linked list
int data;
cout<<"enter the element data to be searched"<<endl;
cin >> data;
int index=searchLL(head,data);
cout<<"element present at index "<<index;
}
Binary file added prefect_square/.DS_Store
Binary file not shown.

0 comments on commit ae871c9

Please sign in to comment.