-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonLinkedList391GfGMethod2.cpp
More file actions
88 lines (87 loc) · 1.43 KB
/
AmazonLinkedList391GfGMethod2.cpp
File metadata and controls
88 lines (87 loc) · 1.43 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
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
void push(node **head,int data)
{
node *temp=new node;
temp->data=data;
temp->next=(*head);
(*head)=temp;
}
void printList(node *head)
{
node *curr=head;
while(curr!=NULL)
{
cout<<curr->data<<" ";
curr=curr->next;
}
}
void insert_at_end(node **head,node *temp)
{
if(head==NULL)
return ;
node *curr=*head;
while(curr->next!=NULL)
curr=curr->next;
temp->next=NULL;
curr->next=temp;
}
void subtract(node *head,int n)
{
node *curr=head;
node *temp;
stack<node *>st;
if(n==1)
return;
if(1&n)
n=n+1;
for(int i=1;i<n/2;i++)
curr=curr->next;
temp=curr->next;
while(temp)
{
st.push(temp);
temp=temp->next;
}
curr=head;
while(!st.empty())
{
node *temp1=st.top();
temp1->data-=curr->data;
st.pop();
curr=curr->next;
}
printList(head);
}
int len(node *head)
{
node *curr=head;
int cnt=0;
while(curr)
{
curr=curr->next;
cnt++;
}
return cnt;
}
int main()
{
node *head1=NULL;
push(&head1,1);
push(&head1,15);
push(&head1,11);
push(&head1,5);
push(&head1,9);
push(&head1,3);
push(&head1,2);
printList(head1);
cout<<"\n\n";
int n=len(head1);
subtract(head1,n);
return 0;
}