forked from intrinsi/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_node_from_end.cpp
73 lines (68 loc) · 1.61 KB
/
delete_node_from_end.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
#include<iostream>
using namespace std;
struct node {
int num;
struct node * next;
}*head;
void deleteEnd()//fuction to delete last node from the circular linked list
{
struct node *cur,*prev;
cur=head;
while(cur->next!=head)
{
prev=cur;
cur=cur->next;
}
prev->next=head;
free(cur);
}
void build(int n)//function to build circular linked list
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
head = (struct node *)malloc(sizeof(struct node));
cout<<"Enter data of the list:\n"; cin>>num;
head->num = num;
head->next = NULL;
preptr = head;
for(i=2; i<=n; i++) { newnode = (struct node *)malloc(sizeof(struct node)); cin>>num;
newnode->num = num;
newnode->next = NULL;
preptr->next = newnode;
preptr = newnode;
}
preptr->next = head; //linking last node with head node
}
}
void display()//function to display list
{
struct node *tmp;
int n = 1;
if(head == NULL)
{
cout<<"List is empty";
}
else
{
tmp = head;
cout<<"\nCircular linked list data:\n";
do {
cout<<tmp->num<<" "; tmp = tmp->next;
n++;
}while(tmp != head);
}
}
int main()//main function
{
int n,pos;
head = NULL;
cout<<"Enter the size of circular linked list: "; cin>>n;
build(n);
display();
cout<<"\nAfter deleting last node, new list is:";
deleteEnd();
display();
return 0;
}