-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep25.cpp
More file actions
77 lines (61 loc) · 1.3 KB
/
practicep25.cpp
File metadata and controls
77 lines (61 loc) · 1.3 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
#include <iostream>
using namespace std;
struct node{
int data;
struct node* next;
};
struct node2{
int data;
struct node2* next;
};
//function for inserting
void insert_begin(node2** head_ref,int data){
node2* newnode = new node2;
newnode->data = data;
newnode->next=*head_ref;
*head_ref=newnode;
}
//function for executing, printing linked list.
void usingforloop(){
node2* head=NULL;
int n;
cout<<"How many nodes: ";
cin>>n;
//using for loop.
for(int i=0;i<n;i++){
node2* newnode=new node2;
cout<<"Enter data for node "<<i+1<<": ";
int data;
cin>>data;
insert_begin(&head,data);
};
node2* temp=head;
cout<<"Data inserted :";
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main(){
//without using functions and for loops.
node* head=NULL;
node* newnode=new node;
newnode->data=101;
newnode->next=head;
head=newnode;
newnode=new node;
newnode->data=102;
newnode->next=head;
head=newnode;
newnode=new node;
newnode->data=103;
newnode->next=head;
head=newnode;
node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
usingforloop();
}