Skip to content

Commit bcd76d4

Browse files
Hello World
0 parents  commit bcd76d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3305
-0
lines changed

Bubble_sort.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
int no;
4+
struct node{
5+
int data;
6+
struct node *next;
7+
};
8+
9+
struct node *head=NULL;
10+
11+
void insert_first(int item)
12+
{
13+
struct node *n;
14+
n=(struct node*)malloc(sizeof(struct node));
15+
n->data=item;
16+
n->next=head;
17+
head=n;
18+
}
19+
20+
void insert_last(int item)
21+
{
22+
if(head==NULL)insert_first(item);
23+
else{
24+
struct node *n;
25+
n=(struct node*)malloc(sizeof(struct node));
26+
n->data=item;
27+
n->next=NULL;
28+
struct node *ptr;
29+
ptr=head;
30+
while(ptr->next!=NULL)
31+
{
32+
ptr=ptr->next;
33+
}
34+
ptr->next=n;
35+
}
36+
}
37+
38+
void sort(int n)
39+
{
40+
int i,j,t,swap;
41+
struct node *ptr;
42+
for(i=0;i<n-1;i++)
43+
{
44+
swap=0;ptr=head;
45+
for(j=0;j<n-1-i;j++){
46+
if(ptr->data>(ptr->next)->data)
47+
{
48+
t=ptr->data;
49+
ptr->data=(ptr->next)->data;
50+
(ptr->next)->data=t;
51+
swap=1;
52+
}
53+
ptr=ptr->next;
54+
}
55+
if(swap==0)break;
56+
}
57+
}
58+
int main()
59+
{
60+
int i;
61+
printf("Enter number of elements\n");
62+
scanf("%d",&no);
63+
int item;
64+
65+
for(i=1;i<=no;i++)
66+
{
67+
printf("Insert item %d : ",i);
68+
scanf("%d",&item);
69+
insert_last(item);
70+
}
71+
sort(no);
72+
printf("Sorted list:\n");
73+
struct node *ptr=head;
74+
while(ptr!=NULL)
75+
{
76+
printf("%d ",ptr->data);
77+
ptr=ptr->next;
78+
}
79+
}

Bubble_sort.exe

42.1 KB
Binary file not shown.

Circular_doubly_ll.cpp

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
struct node{
4+
int data;
5+
struct node* next;
6+
};
7+
struct node *head=NULL;
8+
9+
struct node* Createnode(int item)
10+
{
11+
struct node* n;
12+
n=(struct node*)malloc(sizeof(struct node*));
13+
n->data=item;
14+
n->next=NULL;
15+
return n;
16+
}
17+
void cir_insert_begin(int item)
18+
{
19+
struct node* n=Createnode(item);
20+
n->next=head;
21+
head=n;
22+
struct node* ptr=head;
23+
if(head!=NULL)
24+
{
25+
while(ptr->next!=NULL){
26+
ptr=ptr->next;
27+
}
28+
ptr->next=n;
29+
}
30+
}//end of insert_begin
31+
32+
void cir_insert_last(int item)
33+
{
34+
struct node* n=Createnode(item);
35+
struct node* ptr=head;
36+
if(head==NULL){
37+
cir_insert_begin(item);
38+
}
39+
else
40+
{
41+
while(ptr->next!=head){
42+
ptr=ptr->next;
43+
}
44+
ptr->next=n;
45+
n->next=head;
46+
}
47+
}//end of insert_last
48+
49+
void cir_del_first()
50+
{
51+
struct node* del;
52+
if(head==NULL){
53+
printf("Empty list\n");
54+
return;
55+
}
56+
else if(head->next==head)
57+
{
58+
printf("Single element circular list\n");
59+
del=head;
60+
head=NULL;
61+
free(del);
62+
}
63+
else
64+
{
65+
struct node* ptr=head;
66+
del=head;
67+
while(ptr->next!=head){
68+
ptr=ptr->next;
69+
}
70+
head=head->next;
71+
ptr->next=head;
72+
free(del);
73+
}
74+
}//end of del_first
75+
76+
void cir_del_last()
77+
{
78+
struct node* ptr=head,*preptr;
79+
if(head==NULL)
80+
{
81+
printf("Empty list\n");
82+
return;
83+
}
84+
else if(head->next==head)
85+
{
86+
printf("Single element circular list\n");
87+
struct node* del=head;
88+
head=NULL;
89+
free(del);
90+
}
91+
else
92+
{
93+
while(ptr->next!=head){
94+
preptr=ptr;
95+
ptr=ptr->next;
96+
}
97+
preptr->next=head;
98+
free(ptr);
99+
}
100+
}//end of del_last
101+
102+
void cir_Traverse()
103+
{
104+
struct node* ptr=head;
105+
do{
106+
printf("%d ",ptr->data);
107+
ptr=ptr->next;
108+
}while(ptr!=head);
109+
}//end of cir_Traverse
110+
111+
struct d_node
112+
{
113+
int data;
114+
struct d_node *next;
115+
struct d_node *prev;
116+
};
117+
118+
struct d_node *head1=NULL;
119+
120+
struct d_node* Createnode1(int item)
121+
{
122+
struct d_node* n;
123+
n=(struct d_node*)malloc(sizeof(struct d_node*));
124+
n->data=item;
125+
n->prev=NULL;
126+
n->next=NULL;
127+
}
128+
129+
void doubly_insert_first(int item){
130+
struct d_node* n=Createnode1(item);
131+
if(head1==NULL)
132+
head1=n;
133+
else{
134+
n->next=head1;
135+
head1->prev=n;
136+
head1=n;
137+
}
138+
}
139+
140+
void doubly_insert_last(int item)
141+
{
142+
if(head1==NULL)doubly_insert_first(item);
143+
else{
144+
struct d_node *n=Createnode1(item);
145+
struct d_node *ptr=head1;
146+
while(ptr->next!=NULL)
147+
{
148+
ptr=ptr->next;
149+
}
150+
n->prev=ptr;
151+
ptr->next=n;
152+
}
153+
}
154+
155+
void d_del_before(int ele)
156+
{
157+
struct d_node* del,*ptr,*preptr;
158+
if(head1->data==ele)printf("First element. Node before cannot be deleted.\n");
159+
else if(head1==NULL)printf("Empty list\n");
160+
else if(head1->next==NULL){
161+
printf("Single element present.Node cannot be deleted before.\n");
162+
}
163+
else if((head1->next)->data==ele){
164+
del=head1;
165+
head1=head1->next;
166+
free(del);
167+
}
168+
else
169+
ptr=head1;
170+
while(((ptr->next)->next)->data!=ele){
171+
ptr=ptr->next;
172+
}
173+
del=ptr->next;
174+
(del->next)->prev=ptr;
175+
ptr->next=del->next;
176+
free(del);
177+
}
178+
void dTraverse(){
179+
struct d_node* ptr = head1;
180+
printf("--Current List--\n");
181+
while(ptr!=NULL){
182+
printf("%d ",ptr->data);
183+
ptr=ptr->next;
184+
}
185+
printf("\n");
186+
}
187+
188+
int main()
189+
{
190+
int no=5,i,ins;
191+
for(i=1;i<=5;i++)
192+
{
193+
printf("Insert element %d :\n",i);
194+
scanf("%d",&ins);
195+
doubly_insert_last(ins);
196+
}
197+
dTraverse();
198+
int conti,ele;
199+
do{
200+
printf("Enter element before which element is to be deleted: \n");
201+
scanf("%d",&ele);
202+
d_del_before(ele);
203+
dTraverse();
204+
printf("Do you want to continue?(1/0)\n");
205+
scanf("%d",&conti);
206+
}while(conti==1);
207+
}

Circular_doubly_ll.exe

132 KB
Binary file not shown.

0 commit comments

Comments
 (0)