mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Update Linked List.cpp
This commit is contained in:
parent
16e1bcdb52
commit
1e78677a77
@ -32,17 +32,33 @@ void insert(int x)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove(int x)
|
void remove(int x){
|
||||||
{
|
|
||||||
node *t=start;
|
if( start == NULL ){
|
||||||
node *p;
|
cout<<"\nLinked List is empty\n";
|
||||||
while(t->val!=x)
|
return ;
|
||||||
{
|
|
||||||
p=t;
|
|
||||||
t=t->next;
|
|
||||||
}
|
}
|
||||||
p->next=t->next;
|
else if( start->val == x ){
|
||||||
delete t;
|
node *temp = start;
|
||||||
|
start = start->next;
|
||||||
|
delete temp;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
node *temp = start, *parent = start;
|
||||||
|
|
||||||
|
while( temp != NULL && temp->val != x ){
|
||||||
|
parent = temp;
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( temp == NULL ){
|
||||||
|
cout <<endl <<x <<" not found in list\n";
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
parent->next = temp->next;
|
||||||
|
delete temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void search(int x)
|
void search(int x)
|
||||||
@ -85,6 +101,7 @@ int main()
|
|||||||
cout<<"\n2. Delete";
|
cout<<"\n2. Delete";
|
||||||
cout<<"\n3. Search";
|
cout<<"\n3. Search";
|
||||||
cout<<"\n4. Print";
|
cout<<"\n4. Print";
|
||||||
|
cout<<"\n5. Exit";
|
||||||
cout<<"\n\nEnter you choice : ";
|
cout<<"\n\nEnter you choice : ";
|
||||||
cin>>choice;
|
cin>>choice;
|
||||||
switch (choice)
|
switch (choice)
|
||||||
@ -99,6 +116,7 @@ int main()
|
|||||||
cin>>x;
|
cin>>x;
|
||||||
search(x); break;
|
search(x); break;
|
||||||
case 4 : show(); break;
|
case 4 : show(); break;
|
||||||
|
case 5 : exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while(choice!=0);
|
while(choice!=0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user