-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathDeleteAtAnyPos.cpp
84 lines (79 loc) · 1.81 KB
/
DeleteAtAnyPos.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
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
using namespace std;
struct node {
int num;
struct node * next;
}*head;
void deleteSpecific(int pos)//function to delete any node from the list
{
struct node *p,*q;
int del,k=0;
del=pos-1;
p=head;
while(k!=del)
{
q=p;
p=p->next;
k++;
}
q->next=p->next;
free(p);//deleting specific node
}
void build(int n)//function to build 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<<"\nEnter position from where you want to delete the node: ";
cin>>pos;
cout<<"\nAfter deleting node from specific position, new list is:";
deleteSpecific( pos);
display();
return 0;
}