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
insert an element in a sorted linked list
insert an element in a sorted linked list
#include
#include
struct node { int data; struct node *next; }; struct node* newnode(int data) { struct node *new=(struct node*)malloc(sizeof(struct node)); new->data=data; new->next=NULL; return new; } void insertashead(struct node **head,int data) { struct node *temp=newnode(data); if((*head)==NULL) *head=temp; else { temp->next=(*head); (*head)=temp; } } void printlist(struct node *head) { while(head!=NULL) { printf("%d ",head->data); head=head->next; } } struct node* sortedinsert(struct node *head,int key) { struct node *temp=newnode(key); struct node *current=head; if(current==NULL) return temp; else if(key < current->data) { temp->next=current; current=temp; return current; } else { while(key > current->next->data) { current=current->next; } temp->next=current->next; current->next=temp; return head; } } int main(void) { struct node *head=NULL; int n,x,i,key; scanf("%d",&n); for(i=0;i
PREVIOUS
NEXT
HOME