Upload
arpit-jain
View
213
Download
0
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;
}