4
Singly linked list - insert, remove, add, count source code This snippet submitted by Girish Amara on 2010-08-25. It has been viewed 299734 times. Rating of 7.3 with 1143 votes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 #include<stdio.h> #include<stdlib.h> s s t t rr u u c c t t node { i i n n t t data; s s t t r r u u c ct t node *next; }*head; v v o o ii d d append(i i n n t t num) { s s t t r r u u c ct t node *temp,*right; temp= (s s t t r r u u c c t t node *)m m aa l ll l o o c c (s s i i zz e e o o f f (ss t t r r uu c c t t node)); temp->data=num; right=(s s t t r r u u c c t t node *)head;  w whhiillee (right->next != NULL) right=right->next; right->next =temp; right=temp; right->next=NULL; } v v o o ii d d add( ii n nt t num ) { s s t t r r u u c ct t node *temp; temp=(ss t t r r u u c c t t node *)m m a a l l l l o o c c (ss i i z z e e oo f f (s s tt r r u u cc t t node)); temp->data=num; i i f f (head== NULL) { head=temp; head->next=NULL; } e e l l s s e e { temp->next=head; head=temp; } } v v o o i i d d addafter(i i n n t t num, i i n nt t loc) { i i n n t t i; s s t t r r u u c ct t node *temp,*left,*right; right=head; f f o o r r (i=1;i<loc;i++) { left=right; right=right->next; } temp=(ss t t r r u u c c t t node *)m m a a ll l l o o c c (ss i i z z e e oo f f (s s tt r r u u cc t t node)); temp->data=num; left->next=temp; left=temp; left->next=right; r r e e t t u u r r n n ; }

Singly Linked List Insert Remove Add Count

Embed Size (px)

Citation preview

8/19/2019 Singly Linked List Insert Remove Add Count

http://slidepdf.com/reader/full/singly-linked-list-insert-remove-add-count 1/3

Singly linked list - insert, remove, add, count source

code

This snippet submitted by Girish Amara on 2010-08-25. It has been viewed 299734 times.Rating of 7.3 with 1143 votes

123456789

101112131415161718192021

2223242526272829303132333435

3637383940414243444546474849505152535455565758596061626364

6566676869

#include<stdio.h>#include<stdlib.h>

ssttrruucctt node{

iinntt data;ssttrruucctt node *next;

}*head;

vvooiidd append(iinntt num){

ssttrruucctt node *temp,*right;temp= (ssttrruucctt node *)mmaalllloocc(ssiizzeeooff(ssttrruucctt node));temp->data=num;right=(ssttrruucctt node *)head;

 wwhhiillee(right->next != NULL)right=right->next;right->next =temp;right=temp;

right->next=NULL;}

vvooiidd add( iinntt num ){

ssttrruucctt node *temp;temp=(ssttrruucctt node *)mmaalllloocc(ssiizzeeooff(ssttrruucctt node));temp->data=num;iiff (head== NULL){head=temp;head->next=NULL;

}eellssee{temp->next=head;head=temp;}

}vvooiidd addafter(iinntt num, iinntt loc){

iinntt i;ssttrruucctt node *temp,*left,*right;right=head;ffoorr(i=1;i<loc;i++){left=right;right=right->next;}temp=(ssttrruucctt node *)mmaalllloocc(ssiizzeeooff(ssttrruucctt node));temp->data=num;left->next=temp;left=temp;left->next=right;rreettuurrnn;

}

vvooiidd insert(iinntt num){

iinntt c=0;ssttrruucctt node *temp;temp=head;iiff(temp==NULL){

8/19/2019 Singly Linked List Insert Remove Add Count

http://slidepdf.com/reader/full/singly-linked-list-insert-remove-add-count 2/3

707172737475767778

798081828384858687888990919293949596979899

100101102103104105106107

108109110111112113114115116117118119120121

122123124125126127128129130131132133134135136137138139140141142143144145146147148149150

151152153154155

add(num);}eellssee{

 wwhhiillee(temp!=NULL){

iiff(temp->data<num)c++;temp=temp->next;

}iiff(c==0)add(num);

eellssee iiff(c<count())addafter(num,++c);

eellsseeappend(num);

}}

iinntt ddeelleettee(iinntt num){

ssttrruucctt node *temp, *prev;temp=head;

 wwhhiillee(temp!=NULL){iiff(temp->data==num){

iiff(temp==head){head=temp->next;ffrreeee(temp);rreettuurrnn 1;}eellssee{prev->next=temp->next;

ffrreeee(temp);rreettuurrnn 1;}

}eellssee{

prev=temp;temp= temp->next;

}}rreettuurrnn 0;

}

vvooiidd   display(ssttrruucctt node *r){r=head;iiff(r==NULL){rreettuurrnn;}

 wwhhiillee(r!=NULL){pprriinnttff("%d ",r->data);r=r->next;}pprriinnttff("\n");

}

iinntt count(){

ssttrruucctt node *n;iinntt c=0;n=head;

 wwhhiillee(n!=NULL){n=n->next;c++;}rreettuurrnn c;

}

iinntt   main(){

iinntt i,num;ssttrruucctt node *n;

8/19/2019 Singly Linked List Insert Remove Add Count

http://slidepdf.com/reader/full/singly-linked-list-insert-remove-add-count 3/3

10 Rate it

More C and C++ source code snippets

Add a snippet!

156157158159160161162163164

165166167168169170171172173174175176177178179180181182183184185186187188189190191192193

194195196197198199200201202203204205206

head=NULL; wwhhiillee(1){pprriinnttff("\nList Operations\n");pprriinnttff("===============\n");pprriinnttff("1.Insert\n");pprriinnttff("2.Display\n");pprriinnttff("3.Size\n");pprriinnttff("4.Delete\n");

pprriinnttff("5.Exit\n");pprriinnttff("Enter your choice : ");iiff(ssccaannff("%d",&i)<=0){

pprriinnttff("Enter only an Integer\n");eexxiitt(0);

} eellssee {sswwiittcchh(i){ccaassee 1: pprriinnttff("Enter the number to insert : ");

ssccaannff("%d",&num);insert(num);bbrreeaakk;

ccaassee 2: iiff(head==NULL){pprriinnttff("List is Empty\n");}eellssee{pprriinnttff("Element(s) in the list are : ");}display(n);bbrreeaakk;

ccaassee 3: pprriinnttff("Size of the list is %d\n",count());bbrreeaakk;

ccaassee 4: iiff(head==NULL)pprriinnttff("List is Empty\n");eellssee{pprriinnttff("Enter the number to delete : ");ssccaannff("%d",&num);

iiff(ddeelleettee(num))pprriinnttff("%d deleted successfully\n",num);

eellsseepprriinnttff("%d not found in the list\n",num);

}bbrreeaakk;

ccaassee 5: rreettuurrnn 0;ddeeffaauulltt: pprriinnttff("Invalid option\n");}

}}rreettuurrnn 0;

}