Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct node{
- int val;
- node *next;
- };
- struct SLList{
- node *head,*tail;
- SLList();
- void insert(int n);
- void addToHead(int n);
- void addToTail(int n);
- void delHead();
- void delTail();
- void print();
- void SLmaximum();
- void SLminimum();
- void SL2ndminimum();
- void SLsort();
- void SLfind(int n);
- };
- SLList :: SLList(){
- head=tail=NULL;
- }
- void SLList::insert (int x){
- node *n=new node();
- n->val=x;
- if(head == NULL){
- head=tail=n;
- tail->next=NULL;
- return;
- }
- tail->next=n;
- tail=n;
- tail->next=NULL;
- }
- void SLList :: addToHead(int x){
- node *t=new node();
- t->val=x;
- if(head==NULL){
- t->next=NULL;
- head=tail=t;
- }
- else{
- t->next=head;
- head=t;
- }
- }
- void SLList::delHead(){
- if(head==NULL)
- return;
- if (head==tail){
- delete head;
- head=tail=NULL;
- }
- else{
- node *t=head;
- head = head -> next;
- delete t;
- }
- }
- void SLList::print(){
- node *cur=head;
- while(cur!=NULL){
- cout<<cur->val<<" ";
- cur=cur->next;
- }
- cout << endl;
- }
- void SLList::addToTail(int x){
- node *t=new node();
- t->val=x;
- if(tail==NULL){
- t->next=NULL;
- tail=head=t;
- }
- else{
- tail->next=t;
- tail=t;
- t->next=NULL;
- }
- }
- void SLList::delTail(){
- if(tail==NULL)
- return;
- if(head==tail){
- delete tail;
- head=tail=NULL;
- }
- else{
- node *t=head,*m;
- while(t->next!=NULL){
- m=t;
- t=t->next;
- }
- m->next=NULL;
- delete t;
- }
- }
- void SLList::SLmaximum(){
- if(head==NULL)
- return;
- else{
- node *n=head;
- int m=-999999999;
- while(n!=NULL){
- if(n->val >m)
- m=n->val;
- n=n->next;
- }
- cout<<"Maximum = "<<m<<endl;
- if(head == tail)
- return;
- n = head;
- int m2 = -999999999;
- while(n != NULL){
- if(n->val > m2 && n->val < m)
- m2 = n->val;
- n=n->next;
- }
- if(m2!=-999999999)
- cout<<"Second Maximum = "<<m2<<endl;
- }
- }
- void SLList::SLminimum(){
- if(head==NULL)
- return;
- else{
- node *n=head;
- int m=999999999;
- while(n!=NULL){
- if(n->val < m)
- m=n->val;
- n=n->next;
- }
- cout<<"Minimum = "<<m<<endl;
- if(head==tail)
- return;
- n = head;
- int m2 = 999999999;
- while(n != NULL){
- if(n->val < m2 && n->val > m)
- m2 = n->val;
- n=n->next;
- }
- if(m2!=999999999)
- cout<<"Second Maximum = "<<m2<<endl;
- }
- }
- void SLList::SLsort(){
- if(head==NULL || head==tail)
- return;
- node *n=head,*t;
- int temp,ma;
- while(n!=NULL){
- t=n->next;
- while(t!=NULL){
- if(n->val > t-> val){
- temp=n->val;
- n->val=t->val;
- t->val=temp;
- }
- t=t->next;
- }
- n=n->next;
- }
- }
- void SLList::SLfind(int n){
- node *t=head;
- if(head==NULL){
- cout<<"Not Found"<<endl;
- return;
- }
- while(t!=NULL){
- if(t->val==n){
- cout<<"Found"<<endl;
- return;
- }
- t=t->next;
- }
- cout<<"Not Found"<<endl;
- }
- int main(){
- SLList m;
- m.insert(6);
- m.insert(4);
- m.insert(3);
- m.insert(9);
- m.insert(7);
- m.insert(1);
- m.delTail();
- m.delHead();
- m.SLfind(3);
- m.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment