forked from vishnu2k60/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList_AppendNode.cpp
92 lines (81 loc) · 1.02 KB
/
LinkedList_AppendNode.cpp
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
#include<iostream>
using namespace std;
class Node
{
public :
int data;
Node *next;
Node(int data)
{
this -> data = data;
next = NULL;
}
};
Node* takeInput()
{
int data;
cin>>data;
Node *head = NULL;
Node *tail= head;
while(data!=-1)
{
Node *newNode = new Node(data);
if(head==NULL)
{
head=newNode;
tail= head;
}
else
{
tail->next=newNode;
newNode->next= NULL;
tail=tail->next;
}
cin>>data;
}
return head;
}
Node * AppendNode(Node* head)
{
int N;
cin>>N;
int c=0;
if(head==NULL)
{
return head;
}
else
{
Node *temp=head;
Node *tail=head;
while(temp!=NULL)
{
tail=temp;
temp=temp->next;
c++;
}
temp = head;
for(int i=0;i<c-N-1;i++)
temp=temp->next;
tail->next=head;
head= temp->next;
temp->next=NULL;
return head;
}
}
void printNode(Node *head)
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
int main()
{
Node *head = takeInput();
head=AppendNode(head);
printNode(head);
delete head;
return 0;
}