fork download
  1. #include<iostream>
  2. using namespace std;
  3. struct sll
  4. {
  5. int data;
  6. struct node*next;
  7. };
  8. typedef struct sllnode;
  9. class list
  10. {
  11. public:
  12. node*create(int);
  13. node*creatennodes(int);
  14. void display(node*);
  15. int count(node*);
  16. node*find(node *, int);
  17. node*IAB(node*,int);
  18. node*IAE(node*,int);
  19. node*IAV(node*,int,int);
  20. node*IAP(node*,int,int);
  21. node*DAB(node*);
  22. node*DAE(node*);
  23. node*DAV(node*,int);
  24. node*DAP(node*,int);
  25. node*sort(node*);
  26. node*reverse(node*);
  27. node*concate(node*,node*);
  28. };
  29. node*list::create(intval)
  30. {
  31. node*temp=new node;
  32. temp->data=val;
  33. temp->next=NULL;
  34. return temp;
  35. }
  36. node* list::find(node *first,intele)
  37. {
  38. while(first!=NULL)
  39. {
  40. if(first->data==ele)
  41. break;
  42. first=first->next;
  43. }
  44. return first;
  45. }
  46. node* list::IAB(node *first,intele)
  47. {
  48. node *temp=create(ele);
  49. if(first!=NULL)
  50. temp->next=first;
  51. return temp;
  52. }
  53. node* list::IAE(node *first,intele)
  54. {
  55. node *temp=create(ele);
  56. node *t=first;
  57. while(t->next!=NULL)
  58. t=t->next;
  59. t->next=temp;
  60. return first;
  61. }
  62. node* list::DAB(node *first)
  63. {
  64. if(first!=NULL)
  65. return first->next;
  66. }
  67. node* list::DAV(node *first, intval)
  68. {
  69. node *pre=NULL;
  70. node *t=first;
  71. if(t->data==val)
  72. {
  73. t=DAB(t);
  74. return t;
  75. }
  76. while(t!=NULL)
  77. {
  78. if(t->data==val)
  79. break;
  80. pre=t;
  81. t=t->next;
  82. }
  83. if(t==NULL)
  84. cout<<"given value is not found";
  85. else
  86. pre->next=t->next;
  87. return first;
  88. }
  89. node *list::sort(node *first)
  90. {
  91. node *i,*j;
  92. int t;
  93. for(i=first;i!=NULL;i=i->next)
  94. for(j=i->next;j!=NULL;j=j->next)
  95. if(i->data>j->data)
  96. {
  97. t=i->data;
  98. i->data=j->data;
  99. j->data=t;
  100. }
  101. return first;
  102. }
  103. node *list::reverse(node *first)
  104. {
  105. node *rev=NULL;
  106. while(first!=NULL)
  107. {
  108. rev=IAB(rev,first->data);
  109. first=first->next;
  110. }
  111. return rev;
  112. }
  113. node* list ::creatennodes(int n)
  114. {
  115. node *temp,*first;
  116. intval,i;
  117. cout<<"Enter a first node value: ";
  118. cin>>val;
  119. first=temp=create(val);
  120. for(i=2;i<=n;i++)
  121. {
  122. cout<<"Enter a next node value: ";
  123. cin>>val;
  124. temp->next=create(val);
  125. temp=temp->next;
  126. }
  127. return first;
  128. }
  129. void list ::display(node *first)
  130. {
  131. cout<<"\nlist= ";
  132. while(first!=NULL)
  133. {
  134. cout<<first->data<<"\t";
  135. first=first->next;
  136. }
  137. cout<<endl;
  138. }
  139. int list::count(node *first)
  140. {
  141. int c=0;
  142. while(first!=NULL)
  143. {
  144. c++;
  145. first=first->next;
  146. }
  147. return c;
  148. }
  149. node* list::IAV(node *first, intele, intval)
  150. {
  151. node *temp=create(ele);
  152. node *t=find(first,val);
  153. if(t!=NULL)
  154. {
  155. temp->next=t->next;
  156. t->next=temp;
  157. }
  158. else
  159. cout<<"Element not found";
  160. return first;
  161. }
  162. node* list::IAP(node *first, intele, intpos)
  163. {
  164. node *temp=create(ele);
  165. node *t=first;
  166. inti,c=count(first);
  167. if(pos>c+1 ||c==0)
  168. {
  169. cout<<"IAP not possible";
  170. return first;
  171. }
  172. else if(pos==1)
  173. {
  174. first=IAB(first,ele);
  175. return first;
  176. }
  177. else if(pos==c+1)
  178. {
  179. first=IAE(first,ele);
  180. return first;
  181. }
  182. else
  183. {
  184. for(i=1; i<pos-1;i++)
  185. t=t->next;
  186. temp->next=t->next;
  187. t->next=temp;
  188. return first;
  189. }
  190. }
  191. node* list::DAE(node *first)
  192. {
  193. node *t=first;
  194. node *pre=NULL;
  195. if(t->next==NULL)
  196. return NULL;
  197. while(t->next!=NULL)
  198. {
  199. pre=t;
  200. t=t->next;
  201. }
  202. pre->next=NULL;
  203. return first;
  204. }
  205. node* list::DAP(node *first, intpos)
  206. {
  207. node *t=first;
  208. intc,i;
  209. c=count(first);
  210. if(c==0||pos>c)
  211. {
  212. cout<<"deletion is not possible";
  213. return first;
  214. }
  215. else if(pos==1)
  216. {
  217. first=DAB(first);
  218. return first;
  219. }
  220. else if(c==pos)
  221. {
  222. first=DAE(first);
  223. return first;
  224. }
  225. else
  226. {
  227. for(i=1;i<pos-1;i++)
  228. {
  229. t=t->next;
  230. }
  231. t->next=t->next->next;
  232. return first;
  233. }
  234. }
  235. node*list::concate(node*first,node*second)
  236. {
  237. node*t=first;
  238. while(t->next!=NULL)
  239. {
  240. t=t->next;
  241. }
  242. t->next=second;
  243. return first;
  244. }
  245.  
  246. int main()
  247. {
  248. list l;
  249. intn,ch,ele,c,val,pos;
  250. node *first,*temp,*second,*third;
  251.  
  252. do
  253. {
  254. cout<<"\n\n1.create n nodes\n
  255. 2.Display\n3.Count\n4.Find\n";
  256. cout<<"5.IAB\n6.IAE\n7.IAV\n8.IAP\n";
  257. cout<<"9.DAB\n10.DAE\n11.DAV\n12.DAP\n
  258. 13.sort\n 14.reverse\n";
  259. cout<<"15.concatenate\n16.Exit\n";
  260. cout<<"Enter your choice:";
  261. cin>>ch;
  262. switch(ch)
  263. {
  264. case 1:
  265. cout<<"Enter n value: ";
  266. cin>>n;
  267. first=l.creatennodes(n);
  268. break;
  269. case 2:
  270. l.display(first);
  271. break;
  272. case 3:
  273. c=l.count(first);
  274. cout<<"total no.of nodes="<<c;
  275. break;
  276. case 4:
  277. cout<<"Enter element to be find:";
  278. cin>>ele;
  279. temp=l.find(first,ele);
  280. if(temp!=NULL)
  281. cout<<"Element found";
  282. else
  283. cout<<"Element not found";
  284. break;
  285. case 5:
  286. cout<<"Enter element to be IAB:";
  287. cin>>ele;
  288. first=l.IAB(first,ele);
  289. cout<<"After IAB the elements are";
  290. l.display(first);
  291. break;
  292. case 12:
  293. cout<<"enter position to be delete:";
  294. cin>>pos;
  295. first=l.DAP(first,pos);
  296. cout<<"after deleting elements are:";
  297. l.display(first);
  298. break;
  299. case 13:
  300. first=l.sort(first);
  301. cout<<"after sorting the elements are";
  302. l.display(first);
  303. break;
  304.  
  305. case 14:
  306. first=l.reverse(first);
  307. cout<<"after sorting the elements are";
  308. l.display(first);
  309. break;
  310. case 15:
  311. cout<<"Enter second list n value:";
  312. cin>>n;
  313. second=l.creatennodes(n);
  314. third=l.concate(first,second);
  315. cout<<"After concatenate the list is:\n";
  316. l.display(third);
  317. break;
  318. case 16: exit(0);
  319. default:
  320. cout<<"Invalid choice";
  321. }
  322. }while(ch<17);
  323. }
  324.  
Success #stdin #stdout 0.02s 25872KB
stdin
Standard input is empty
stdout
#include<iostream>
using namespace std;
struct sll
{
int data;
struct node*next;
};
typedef struct sllnode;
class list
{
    public:
    node*create(int);
    node*creatennodes(int);
    void display(node*);
	int count(node*);
 	node*find(node *, int);
    node*IAB(node*,int);
    node*IAE(node*,int);
    node*IAV(node*,int,int);
    node*IAP(node*,int,int);
    node*DAB(node*);
    node*DAE(node*);
    node*DAV(node*,int);
    node*DAP(node*,int);
    node*sort(node*);
    node*reverse(node*);
    node*concate(node*,node*);
};
node*list::create(intval)
{
    node*temp=new node;
    temp->data=val;
    temp->next=NULL;
    return temp;
}
node* list::find(node *first,intele)
{
    while(first!=NULL)
    {
    	if(first->data==ele)
     	break;
        first=first->next;
    }
    return first;
}
node* list::IAB(node *first,intele)
{
    node *temp=create(ele);
    if(first!=NULL)
    temp->next=first;
    return temp;
}
node* list::IAE(node *first,intele)
{
    node *temp=create(ele);
    node *t=first;
    while(t->next!=NULL)
    t=t->next;
    t->next=temp;
    return first;
}
node* list::DAB(node *first)
{
    if(first!=NULL)
    return first->next;
}
node* list::DAV(node *first, intval)
{
 node *pre=NULL;
 node *t=first;
 if(t->data==val)
 {
  t=DAB(t);
  return t;
 }
 while(t!=NULL)
 {
  if(t->data==val)
  break;
  pre=t;
  t=t->next;
 }
 if(t==NULL)
 cout<<"given value is not found";
 else
 pre->next=t->next;
 return first;
}
node *list::sort(node *first)
{
  node *i,*j;
  int t;
  for(i=first;i!=NULL;i=i->next)
  for(j=i->next;j!=NULL;j=j->next)
  if(i->data>j->data)
  {
     t=i->data;
	 i->data=j->data;
     j->data=t;
  }
   return first;
}
node *list::reverse(node *first)
{
  node *rev=NULL;
  while(first!=NULL)
  {
    rev=IAB(rev,first->data);
    first=first->next;
  }
   return rev;
}
node* list ::creatennodes(int n)
{
    node *temp,*first;
	intval,i;
	cout<<"Enter a first node value: ";
	cin>>val;
    first=temp=create(val);
    for(i=2;i<=n;i++)
    {
		cout<<"Enter a next node value: ";
		cin>>val;
  		temp->next=create(val);
        temp=temp->next;
    }
    return first;
}
void list ::display(node *first)
{
	cout<<"\nlist= ";
    while(first!=NULL)
    {
			cout<<first->data<<"\t";
            first=first->next;
    }
	cout<<endl;
}
int list::count(node *first)
{
	int c=0;
    while(first!=NULL)
    {
			c++;
            first=first->next;
    }
    return c;
}
node* list::IAV(node *first, intele, intval)
{
    node *temp=create(ele);
    node *t=find(first,val);
    if(t!=NULL)
    {
		temp->next=t->next;
		t->next=temp;
    }
    else
	cout<<"Element not found";
    return first;
}
node* list::IAP(node *first, intele, intpos)
{
    node *temp=create(ele);
    node *t=first;
	inti,c=count(first);
    if(pos>c+1 ||c==0)
    {
		cout<<"IAP not possible";
        return first;
    }
    else if(pos==1)
    {
        first=IAB(first,ele);
        return first;
    }
    else if(pos==c+1)
    {
        first=IAE(first,ele);
        return first;
    }
    else
    {
        for(i=1; i<pos-1;i++)
        t=t->next;
        temp->next=t->next;
        t->next=temp;
        return first;
    }
}
node* list::DAE(node *first)
{
    node *t=first;
    node *pre=NULL;
    if(t->next==NULL)
    return NULL;
    while(t->next!=NULL)
    {
            pre=t;
            t=t->next;
    }
    pre->next=NULL;
    return first;
}
node* list::DAP(node *first, intpos)
{
  node *t=first;
  intc,i;
  c=count(first);
  if(c==0||pos>c)
  {
	cout<<"deletion is not possible";
    return first;
  }
  else if(pos==1)
  {
   	first=DAB(first);
   	return first;
  }
  else if(c==pos)
  {
   first=DAE(first);
   return first;
  }
  else
  {
   for(i=1;i<pos-1;i++)
   {
    t=t->next;
   }
   t->next=t->next->next;
   return first;
  }
}
node*list::concate(node*first,node*second)
{
	 node*t=first;
     while(t->next!=NULL)
 {
        t=t->next;
 }
 t->next=second;
 return first;
}

int main()
{
    list l;
intn,ch,ele,c,val,pos;
    node *first,*temp,*second,*third;

    do
    {
cout<<"\n\n1.create n nodes\n
2.Display\n3.Count\n4.Find\n";
cout<<"5.IAB\n6.IAE\n7.IAV\n8.IAP\n";
cout<<"9.DAB\n10.DAE\n11.DAV\n12.DAP\n 
13.sort\n 14.reverse\n";
cout<<"15.concatenate\n16.Exit\n";
cout<<"Enter your choice:";
cin>>ch;
        switch(ch)
        {
            case 1:
cout<<"Enter n value: ";
cin>>n;
                    first=l.creatennodes(n);
                    break;
            case 2:
l.display(first);
                    break;
            case 3:
                    c=l.count(first);
cout<<"total no.of nodes="<<c;
                    break;
            case 4:
cout<<"Enter element to be find:";
cin>>ele;
                    temp=l.find(first,ele);
                    if(temp!=NULL)
cout<<"Element found";
                    else
cout<<"Element not found";
                    break;
            case 5:
cout<<"Enter element to be IAB:";
cin>>ele;
                    first=l.IAB(first,ele);
cout<<"After IAB the elements are";
l.display(first);
                    break;
            case 12:    
	cout<<"enter position to be delete:";
cin>>pos;
                    first=l.DAP(first,pos);
cout<<"after deleting elements are:";
l.display(first);
                    break;
             case 13:   
first=l.sort(first);
cout<<"after sorting the elements are";
l.display(first);
                    break;

            case 14:   					
first=l.reverse(first);
cout<<"after sorting the elements are";
l.display(first);
                   break;
            case 15:
cout<<"Enter second list n value:";
cin>>n;
                   second=l.creatennodes(n);
                   third=l.concate(first,second);
cout<<"After concatenate the list is:\n";
l.display(third);
                   break;
            case 16:   exit(0);
            default:
		cout<<"Invalid choice";
        }
    }while(ch<17);
}