Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b40a5be

Browse files
Create MinCostPath
1 parent abea6c9 commit b40a5be

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

‎Dynamic Programming 2/MinCostPath

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include<iostream>
2+
using namespace std;
3+
class Node{
4+
public:
5+
int data;
6+
Node *next;
7+
Node(int data){
8+
this->data=data;
9+
next=NULL;
10+
}
11+
};
12+
Node* takeInput(){
13+
int data;
14+
cin>>data;
15+
Node* head=NULL;
16+
Node* tail=NULL;
17+
while(data!=-1){
18+
Node *newnode=new Node(data);
19+
if(head==NULL){
20+
head=newnode;
21+
tail=newnode;
22+
}
23+
else{
24+
tail->next=newnode;
25+
tail=tail->next;
26+
}
27+
cin>>data;
28+
}
29+
return head;
30+
}
31+
int midpoint(Node *head){
32+
Node*slow=head;
33+
Node*fast=head->next; //or just head
34+
while(fast!=NULL&&fast->next!=NULL){
35+
slow=slow->next;
36+
fast=fast->next->next;
37+
}
38+
return slow->data;
39+
}
40+
void print(Node* head){
41+
Node *temp=head;
42+
while(temp!=NULL){
43+
cout<<temp->data<<" ";
44+
temp=temp->next;
45+
}
46+
}
47+
int main(){
48+
cout<<"enter the linked list: "<<flush;
49+
Node *head=takeInput();
50+
print(head);
51+
cout<<endl;
52+
cout<<"the middle element of the linked list is : "<<midpoint(head);
53+
return 0;
54+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /