File tree Expand file tree Collapse file tree 7 files changed +197
-0
lines changed
Expand file tree Collapse file tree 7 files changed +197
-0
lines changed Original file line number Diff line number Diff line change 11#include < iostream>
22using namespace std ;
33
4+ // to create and print the linked list
5+
46class node
57{
68public:
Original file line number Diff line number Diff line change 11#include < iostream>
22using namespace std ;
33
4+ // to delete an element when an element is given to delete
5+
46class node
57{
68public:
Original file line number Diff line number Diff line change 11#include < iostream>
22using namespace std ;
33
4+ // To delete an element at a certain position
5+
46class node
57{
68public:
Original file line number Diff line number Diff line change 11#include < iostream>
22using namespace std ;
33
4+ // to insert a node in linked list
5+
46class node
57{
68public:
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ // This program is for finding the length of linked list
5+
6+ class node {
7+ public:
8+ int data;
9+ node* next;
10+ };
11+
12+ void push (node **head_ref, int new_data)
13+ {
14+ node *new_node = new node ();
15+ new_node->data = new_data;
16+ new_node->next = (*head_ref);
17+ (*head_ref) = new_node;
18+ }
19+
20+ // iterative method
21+ int count (node *head){
22+ int count = 0 ;
23+ node* current = head;
24+ while (current != NULL ){
25+ count++;
26+ current = current->next ;
27+ }
28+ return count;
29+ }
30+
31+ // recursive method
32+ int getcount (node* head){
33+ if (head == NULL ){
34+ return 0 ;
35+ }
36+ else {
37+ return 1 + getcount (head->next );
38+ }
39+ }
40+
41+ int main (){
42+ node* head = NULL ;
43+ push (&head, 10 );
44+ push (&head, 5 );
45+ push (&head, 0 );
46+
47+ cout<<count (head); // 3
48+ cout<<getcount (head); // 3
49+ return 0 ;
50+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ // this program is to reverse linked list
5+
6+ class node
7+ {
8+ public:
9+ int data;
10+ node *next;
11+ };
12+
13+ void push (node **head_ref, int new_data)
14+ {
15+ node *new_node = new node ();
16+ new_node->data = new_data;
17+ new_node->next = (*head_ref);
18+ (*head_ref) = new_node;
19+ }
20+
21+ void printList (node *i)
22+ {
23+ while (i != NULL )
24+ {
25+ cout << i->data << " " ;
26+ i = i->next ;
27+ }
28+ cout<<endl;
29+ }
30+
31+ // iterative method
32+ node* reverse (node* &head){
33+ node* prevptr = NULL ;
34+ node* currptr = head;
35+ node* nextptr;
36+
37+ while (currptr != NULL ){
38+ nextptr = currptr->next ;
39+ currptr->next = prevptr;
40+
41+ prevptr = currptr;
42+ currptr = nextptr;
43+ }
44+ return prevptr;
45+ }
46+
47+ // recursive method
48+ node* reverseRecursive (node* &head){
49+ if (head == NULL || head->next ==NULL ){
50+ return head;
51+ }
52+ node* newhead = reverseRecursive (head->next );
53+ head->next ->next = head;
54+ head->next = NULL ;
55+
56+ return newhead;
57+ }
58+
59+ int main ()
60+ {
61+ node *head = NULL ;
62+ push (&head, 5 );
63+ push (&head, 4 );
64+ push (&head, 3 );
65+ push (&head, 2 );
66+ push (&head, 1 );
67+
68+ printList (head); // 1 2 3 4 5
69+
70+ node* newhead = reverse (head);
71+ printList (newhead); // 5 4 3 2 1
72+
73+ node* recurrsiveHead = reverseRecursive (newhead);
74+ printList (recurrsiveHead); // 1 2 3 4 5
75+
76+ return 0 ;
77+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ // this program is to reverse K nodes of linked list
5+
6+ class node {
7+ public:
8+ int data;
9+ node* next;
10+ };
11+
12+ void printList (node* i){
13+ while (i!=NULL ){
14+ cout<<i->data <<" " ;
15+ i = i->next ;
16+ }
17+ cout<<endl;
18+ }
19+
20+ void push (node** head, int new_data){
21+ node* new_node = new node ();
22+ new_node->data = new_data;
23+ new_node->next = *head;
24+ *head = new_node;
25+ }
26+
27+ node* reverseK (node* &head, int k){
28+ node* prevptr = NULL ;
29+ node* currptr = head;
30+ node* nextptr;
31+
32+ int count = 0 ;
33+ while (currptr!=NULL && count<k){
34+ nextptr = currptr->next ;
35+ currptr->next = prevptr;
36+ prevptr = currptr;
37+ currptr = nextptr;
38+ count++;
39+ }
40+
41+ if (nextptr!=NULL ){
42+
43+ head->next = reverseK (nextptr, k);
44+ }
45+ return prevptr;
46+ }
47+
48+ int main (){
49+ node *head = NULL ;
50+ push (&head, 5 );
51+ push (&head, 4 );
52+ push (&head, 3 );
53+ push (&head, 2 );
54+ push (&head, 1 );
55+
56+ printList (head); // 1 2 3 4 5
57+ int k = 3 ;
58+ node* newhead = reverseK (head, k);
59+ printList (newhead); // 3 2 1 5 4
60+
61+ return 0 ;
62+ }
You can’t perform that action at this time.
0 commit comments