SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Linked List Implementation(search an element in a linked list iterative)
Linked List Implementation(search an element in a linked list iterative)
Implementation:
#include
#include
/* structure definition of the node of the linked list */ struct node { int data; struct node *next; }; /* function to create a new node in the linked list & returns the address of the new node */ struct node* newnode(int data) { struct node *new=(struct node*)malloc(sizeof(struct node)); new->data=data; new->next=NULL; return new; } /* function to insert every created node in the tail of the linked list */ void insertastail(struct node **head,int data) { struct node *temp; struct node *current=*head; if(current==NULL) { *head=newnode(data); } else { temp=newnode(data); while(current->next!=NULL) current=current->next; current->next=temp; } } /* function to print the data in the linked list */ void printlist(struct node *head) { struct node *current=head; while(current!=NULL) { printf("%d ",current->data); current=current->next; } } /* function to search an element in the linked list */ int searchlist(struct node *head,int data) { while(head!=NULL) { if(head->data==data) return 1; head=head->next; } return 0; } int main(void) { struct node *head=NULL; int data; int i,n; scanf("%d",&n); for(i=1;i<=n;i++) insertastail(&head,i); printlist(head); scanf("%d",&data); if(searchlist(head,data)) printf("\nelement found in the list"); else printf("\nelement not found in the list"); return 0; }
PREVIOUS
NEXT
HOME