Skip to content

Commit 31f8ec9

Browse files
authored
Add files via upload
1 parent 1f49d79 commit 31f8ec9

6 files changed

+738
-0
lines changed

circular linked list.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include <iostream>
2+
#include <stdlib.h>
3+
using namespace std;
4+
struct node{
5+
int data;
6+
struct node *next;
7+
}*tail;
8+
9+
void insert_begin(int value)
10+
{
11+
struct node *curr;
12+
curr=(struct node *)malloc(sizeof(struct node));
13+
curr->data=value;
14+
if(tail==0)
15+
{
16+
curr->next=curr;
17+
tail=curr;
18+
}
19+
curr->next=tail->next;
20+
tail->next=curr;
21+
}
22+
23+
void insert_end(int value)
24+
{
25+
struct node *new_node;
26+
new_node=(struct node *)malloc(sizeof(struct node));
27+
new_node->data=value;
28+
if (tail==0)
29+
{
30+
tail=new_node;
31+
tail->next=new_node;
32+
}
33+
else
34+
{
35+
new_node->next=tail->next;
36+
tail->next=new_node;
37+
tail=new_node;
38+
}
39+
}
40+
41+
void del_begin()
42+
{
43+
struct node *temp;
44+
temp=tail->next;
45+
cout<<"Deleted item: "<<temp->data<<endl;
46+
if (temp->next==temp)
47+
{
48+
tail=0;
49+
free(temp);
50+
}
51+
else
52+
{
53+
tail->next=temp->next;
54+
free(temp);
55+
}
56+
}
57+
58+
void del_end()
59+
{
60+
struct node *current,*prev;
61+
current=tail->next;
62+
if (current->next==current)
63+
{
64+
cout<<"Deleted item: "<<current->data<<endl;
65+
tail=0;
66+
free(current);
67+
}
68+
else
69+
{
70+
while (current->next!=tail->next)
71+
{
72+
prev=current;
73+
current=current->next;
74+
}
75+
cout<<"Deleted item: "<<current->data<<endl;
76+
prev->next=tail->next;
77+
tail=prev;
78+
free(current);
79+
}
80+
}
81+
82+
void display()
83+
{
84+
struct node *temp;
85+
temp=tail->next;
86+
cout<<"LIST: ";
87+
while (temp->next!=tail->next)
88+
{
89+
cout<<temp->data<<"->";
90+
temp=temp->next;
91+
}
92+
cout<<temp->data;
93+
}
94+
95+
int main ()
96+
{
97+
int choice,val;
98+
tail=0;
99+
do{
100+
cout<<"1 for insert at beginning"<<endl;
101+
cout<<"2 for insert at end"<<endl;
102+
cout<<"3 for display"<<endl;
103+
cout<<"4 for delete from beginning"<<endl;
104+
cout<<"5 for delete from end"<<endl;
105+
cout<<"0 to exit"<<endl;
106+
cin>>choice;
107+
switch(choice)
108+
{
109+
case 1:
110+
cout<<"Enter the data: ";
111+
cin>>val;
112+
insert_begin(val);
113+
break;
114+
case 2:
115+
cout<<"Enter the data: ";
116+
cin>>val;
117+
insert_end(val);
118+
break;
119+
case 3:
120+
if (tail==0)
121+
cout<<"List is Empty."<<endl;
122+
else
123+
display();
124+
cout<<endl;
125+
break;
126+
case 4:
127+
if (tail==0)
128+
cout<<"List is Empty."<<endl;
129+
else
130+
del_begin();
131+
cout<<endl;
132+
break;
133+
case 5:
134+
if (tail==0)
135+
cout<<"List is Empty."<<endl;
136+
else
137+
del_end();
138+
cout<<endl;
139+
break;
140+
case 0:
141+
return 0;
142+
}
143+
}while (choice<6);
144+
return 0;
145+
}
146+
147+

queue using array.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <iostream>
2+
using namespace std;
3+
#define MAX 5
4+
5+
int arr[MAX];
6+
int front=-1;
7+
int rear=-1;
8+
9+
void enQueue ()
10+
{
11+
int item;
12+
if (rear==MAX-1) //Queue is full
13+
cout<<"Queue is Full."<<endl;
14+
else if (front==-1 && rear==-1) //Queue is empty
15+
{
16+
front=0;
17+
rear=0;
18+
cout<<"Inset the element in queue : ";
19+
cin>>item;
20+
arr[rear]=item;
21+
}
22+
else
23+
{
24+
rear++;
25+
cout<<"Inset the element in queue : ";
26+
cin>>item;
27+
arr[rear]=item;
28+
}
29+
}
30+
31+
void deQueue ()
32+
{
33+
if (front==-1 && rear==-1) //Queue is empty
34+
cout<<"Queue is Empty."<<endl;
35+
else if (front==rear) // on;y one item present
36+
{
37+
cout<<"Deleted item: "<<arr[front]<<endl;
38+
front=-1;
39+
rear=-1;
40+
}
41+
else
42+
{
43+
cout<<"Deleted item: "<<arr[front]<<endl;
44+
front++;
45+
}
46+
}
47+
48+
void peek ()
49+
{
50+
if (front==-1 && rear==-1)
51+
cout<<"Queue is Empty."<<endl;
52+
else
53+
cout<<"Peeked value: "<<arr[front]<<endl;
54+
}
55+
56+
void display ()
57+
{
58+
int i;
59+
if (front==-1 && rear==-1)
60+
cout<<"Queue is Empty."<<endl;
61+
else
62+
{
63+
cout<<"Queue is:"<<endl;
64+
for (i=front;i<=rear;i++)
65+
cout<<arr[i]<<" ";
66+
cout<<endl;
67+
}
68+
}
69+
70+
int main()
71+
{
72+
int choice;
73+
do
74+
{
75+
cout<<"1.EnQueue"<<endl;
76+
cout<<"2.DeQueue"<<endl;
77+
cout<<"3.Peek"<<endl;
78+
cout<<"4.Display"<<endl;
79+
cout<<"Enter your choice : ";
80+
cin>>choice;
81+
switch (choice)
82+
{
83+
case 1:
84+
enQueue();
85+
break;
86+
case 2:
87+
deQueue();
88+
break;
89+
case 3:
90+
peek();
91+
break;
92+
case 4:
93+
display();
94+
break;
95+
default:
96+
cout<<"Wrong choice.\n";
97+
}
98+
}while (choice<5);
99+
return 0;
100+
}
101+

queue using linked list.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <iostream>
2+
#include <stdlib.h>
3+
using namespace std;
4+
5+
struct node{
6+
int data;
7+
struct node *next;
8+
};
9+
struct node *front=0;
10+
struct node *rear=0;
11+
12+
void enQueue(int val)
13+
{
14+
struct node *new_node;
15+
new_node=(struct node *)malloc(sizeof(struct node));
16+
new_node->data=val;
17+
new_node->next=0;
18+
if (front==0 && rear==0)
19+
{
20+
front=new_node;
21+
rear=new_node;
22+
}
23+
else
24+
{
25+
rear->next=new_node;
26+
rear=new_node;
27+
}
28+
}
29+
30+
void peek()
31+
{
32+
if (front==0)
33+
cout<<"List is empty."<<endl;
34+
else
35+
cout<<"Peeked value: "<<front->data<<endl;
36+
}
37+
38+
void deQueue()
39+
{
40+
struct node *temp;
41+
temp=front;
42+
cout<<"Deleted item: "<<front->data<<endl;
43+
front=front->next;
44+
free(temp);
45+
}
46+
47+
void display()
48+
{
49+
struct node *temp;
50+
temp=front;
51+
while(temp->next!=0)
52+
{
53+
cout<<temp->data<<"->";
54+
temp=temp->next;
55+
}
56+
cout<<temp->data<<endl;
57+
}
58+
59+
int main ()
60+
{
61+
int choice,value;
62+
do{
63+
cout<<"1 for enQueue"<<endl;
64+
cout<<"2 for deQueue"<<endl;
65+
cout<<"3 for peek"<<endl;
66+
cout<<"4 for display"<<endl;
67+
cout<<"0 to exit"<<endl;
68+
cout<<"Enter your choice: ";
69+
cin>>choice;
70+
switch(choice)
71+
{
72+
case 1:
73+
cout<<"Enter the data: ";
74+
cin>>value;
75+
enQueue(value);
76+
break;
77+
case 2:
78+
if (front==0)
79+
cout<<"List is empty."<<endl;
80+
else
81+
deQueue();
82+
cout<<endl;
83+
break;
84+
case 3:
85+
peek();
86+
cout<<endl;
87+
break;
88+
case 4:
89+
if (front==0)
90+
cout<<"List is empty."<<endl;
91+
else
92+
display();
93+
cout<<endl;
94+
break;
95+
case 0:
96+
return 0;
97+
}
98+
}while (choice<5);
99+
return 0;
100+
}
101+
102+

0 commit comments

Comments
 (0)