forked from mohitarora3/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprogram3.cpp
More file actions
136 lines (127 loc) · 2.41 KB
/
program3.cpp
File metadata and controls
136 lines (127 loc) · 2.41 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<iostream>
#include<conio.h>
using namespace std;
class node
{
/*
objective: Create a class for implementing node
input parameters: none
output value: none
description: class definition
approach: Class defines data member and member function of the node class
*/
int info;
node *next;
friend class linkedlist;
public:
node()
{
next=0;
}
node(int ele,node *ptr=0)
{
info=ele;
next=ptr;
}
};
class linkedlist
{
/*
objective: Create a class for implementing linklist
input parameters: none
output value: none
description: class definition
approach: Class defines data member and member function of the linkedlist class
*/
node *head;
node *tail;
public:
linkedlist()
{
head=0;
tail=0;
}
void add_to_tail(int ele);
void reverse_consec();
void display();
};
void linkedlist::add_to_tail(int ele)
{
/*
objective:to insert element at the end of the linklist
input parameters:
ele-(integer value)-element to be inserted into linklist
return value:none
*/
if(head==0)
{
head=new node(ele);
tail=head;
}
else
{
tail->next=new node(ele);
tail=tail->next;
}
}
void linkedlist::display()
{
/*
objective:to display elements of linklist
input parameters:none
return value:none
approach:-starting from head display every element of linklist
*/
node *temp=head;
while(temp)
{
cout<<temp->info;
temp=temp->next;
}
}
void linkedlist::reverse_consec()
{
/*
objective:reverse the linklist in pairs
input parameters:none
return value:none
approach:-starting from head swap elements of every pair
*/
int t;
node *temp=head;
while(temp&&temp->next!=0)
{
t=temp->info;
temp->info=temp->next->info;
temp->next->info=t;
temp=temp->next->next;
}
}
int main()
{
/*
objective:reverse the linklist in pairs
input parameters:
ele-(integer value)-element to be inserted into linklist
return value:none
approach:-by invoking function reverse_consec()
*/
linkedlist l;
int ele;
char ch;
do
{
cout<<"\n\tENTER ELEMENT:";
cin>>ele;
l.add_to_tail(ele);
cout<<"\n\twant to enter more(enter y or Y)";
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<"\n\tLINKLIST IS:";
l.display();
l.reverse_consec();
cout<<"\n\tAFTER REVERSING CONSECUTIVE ELEMENTS:";
cout<<"\n\tLINKLIST IS:";
l.display();
getch();
}