Repository navigation
Expand file tree
/
Copy pathcirularlist.cpp
More file actions
111 lines (98 loc) · 1.52 KB
/
Copy pathcirularlist.cpp
File metadata and controls
111 lines (98 loc) · 1.52 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *head;
struct node *prevnode;
void sethead()
{
head=NULL;
prevnode=NULL;
}
struct node* createnode(int data)
{
struct node* n=(struct node *)malloc(sizeof(struct node));
if(n!=NULL)
{n->data=data;
n->next=n;
return n;
}
else
cout<<"error occured during node creation";
}
void addnode(struct node* ptr)
{
struct node* temp=head;
if(head==NULL)
{
head=ptr;
return;
}
else
{
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=ptr;
ptr->next=head;
//return;
}
}
void traverse(struct node *ptr1)
{
while(ptr1->next!=head)
{
cout<<ptr1->data<<" ";
ptr1=ptr1->next;
}
cout<<ptr1->data<<"\n";
return;
}
void addnodebeg(struct node* ptr2)
{
if(head==NULL)
{
head=ptr2;
return;
}
else
{
struct node* temp=head;
//head=ptr2;
ptr2->next=head;
//struct node* temp1=head;
//head=ptr2;
while(temp->next!=temp)
temp=temp->next;
//head=ptr2;
head=ptr2;
temp->next=head;
}
}
int main()
{
int n,ele;
cout<<"Adding node at end\n";
cin>>n;
for(int i=0;i<n;i++)
{ cin>>ele;
struct node* nodeaddr=createnode(ele);
addnode(nodeaddr);
}
cout<<"enterd element is\n";
traverse(head);
cout<<"Adding node at begning\n";
sethead();
cin>>n;
for(int i=0;i<n;i++)
{ cin>>ele;
struct node* nodeaddr=createnode(ele);
addnodebeg(nodeaddr);
}
cout<<"enterd element is\n";
traverse(head);
}