-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppendLastNToFirst.cpp
More file actions
109 lines (105 loc) · 1.81 KB
/
AppendLastNToFirst.cpp
File metadata and controls
109 lines (105 loc) · 1.81 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
#include<iostream>
#include "Node1.cpp"
using namespace std;
int length(Node1 *head)
{
if(head==NULL)
{
return 0;
}
int len=length(head->next);
return len+1;
}
/*
n should be global
lastNodeAddress should be global
NewNodeAddress should be global
Node* AppendLastNToFirst(Node1 *h,int* n,Node1* lastNodeAddress,Node1* NewNodeAddress,int i=1)
{
if(h==NULL)
{
return NULL;
}
Node* c=AppendLastNToFirst(h->next,n,lastNodeAddress,i+1);
n--;
i--;
if(c==NULL)
{
lastNodeAddress=h;
}
if(n==-1)
{
head=h->next;
h->next=NULL;
}
if(i==0)
{
lastNodeAddress->next=h;
}
return h;
}
*/
Node1* AppendLastNToFirst(int pos,Node1 *head)
{
while(pos!=0)
{
Node1 *t=head->next;
Node1 *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
head->next=NULL;
head=t;
pos--;
}
return head;
}
Node1* takeInput(){
int dt;
cin>>dt;
if(dt==-1){
return NULL;
}
Node1 *head=NULL,*tail=NULL;
Node1 *newnode=new Node1(dt);
if(head==NULL ){
head=newnode;
tail=newnode;
}
Node1 *temp=head;
cin>>dt;
while(dt!=-1){
Node1 *newnode=new Node1(dt);
tail->next=newnode;
tail=newnode;
cin>>dt;
}
return head;
}
void print(Node1 *head)
{
if(head==NULL)
{
cout<<"\nSorry no Linked List....\n";
return;
}
while(head!=NULL)
{
cout<<head->a<<" ";
head=head->next;
}
cout<<endl;
}
int main()
{
Node1 *ptr=takeInput();
print(ptr);
int pos;
cin>>pos;
int temp=length(ptr);
Node1 *temp1=AppendLastNToFirst((temp-pos),ptr);
print(temp1);
return 0;
}