-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linklist.cpp
More file actions
89 lines (87 loc) · 1.57 KB
/
Copy pathsingly_linklist.cpp
File metadata and controls
89 lines (87 loc) · 1.57 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
#include <iostream>
using namespace std;
struct node
{
int data;
node * next;
};
node * insertbeg(node * head)
{
int value;
cout<<"\n Enter the value ";
cin>>value;
node * first;
first = new node;
first->data = value;
first->next = head;
head = first;
return head;
}
void display(node *head)
{
node * temp = head;
while(temp!=NULL)
{
cout<<endl<<temp->data;
temp = temp->next;
}
}
void insertend(node* head)
{
cout<<"\n Inserting at the end";
node* current=head;
node*last=new node;
cout<<"\n Enter the value";
cin>>last->data;
last->next=NULL;
if(head==NULL)
{
head=last;
}
else
while(current->next!=NULL)
{
current=current->next;
}
current->next=last;
}
node * deletebeg(node * head)
{
cout<<"\n Deleting the beginning node";
node *temp=head;
cout<<"\n The deleted element is "<<temp->data;
head = head->next;
delete temp;
return head;
}
void deleteend(node*head)
{
cout<<"\n Deleting the ending node";
node *temp=head;
node * temp1;
while(temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
cout<<"\n The deleted element is "<<temp->data;
temp1->next=NULL;
delete temp;
}
int main()
{
int num;
int i;
cout<<"\n Enter the number of nodes";
cin>>num;
node * head = NULL;
for(i=0;i<num;i++){
head = insertbeg(head);
}
insertend(head);
display(head);
head = deletebeg(head);
display(head);
deleteend(head);
display(head);
}