forked from lavinske/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXXIMovieCenter.cpp
More file actions
124 lines (116 loc) · 2.29 KB
/
Copy pathXXIMovieCenter.cpp
File metadata and controls
124 lines (116 loc) · 2.29 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
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Data{
char name[100];
int duration;
struct Data *next;
}*head, *tail, *curr;
struct Data *createNode(char name[], int duration){
struct Data *temp = (struct Data*)malloc(sizeof(struct Data));
strcpy(temp->name, name);
temp->duration = duration;
temp->next=NULL;
return temp;
}
void push(char name[], int duration){
struct Data *temp = createNode(name, duration);
if(head==NULL){
head=tail=temp;
}
else if(strcmp(temp->name, head->name) < 0){
temp->next=head;
head=temp;
}
else if(strcmp(temp->name, tail->name) > 0){
tail->next=temp;
tail=temp;
}
else{
struct Data *prev;
curr=head;
while(strcmp(curr->name, temp->name) < 0){
prev=curr;
curr=curr->next;
}
if(strcmp(curr->name, temp->name) == 0){
curr->duration=temp->duration;
}
else{
prev->next=temp;
temp->next=curr;
}
}
}
void pop(){
if(head!=NULL){
if(head->next==NULL){
free(head);
head=tail=NULL;
}
else{
curr=head;
while(curr->next!=tail){
curr=curr->next;
}
free(curr->next);
tail=curr;
tail->next=NULL;
}
}
}
void popAll(){
while(head!=NULL){
pop();
}
}
void view(){
printf("Movie Title : Duration (minutes)\n");
if(head!=NULL){
curr=head;
while(curr!=NULL){
printf("%-20s %d\n", curr->name, curr->duration);
curr=curr->next;
}
}
printf("\n");
}
int main(){
int menu, duration, length;
char name[100];
do{
system("cls");
view();
printf("XXI Movie Center\n");
printf("================\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Exit\n");
do{
printf("Input your choice : ");
scanf("%d", &menu); getchar();
}while(menu<1||menu>3);
printf("\n");
switch(menu){
case 1:
do{
printf("Input the movie title [min. 3 characters]: ");
scanf("%[^\n]", &name); getchar();
length = strlen(name);
}while(length<3);
do{
printf("Input the movie duration [must be a multiple of 30]: ");
scanf("%d", &duration); getchar();
}while(duration%30!=0);
push(name, duration);
break;
case 2:
pop();
break;
case 3:
popAll();
break;
}
}while(menu!=3);
return 0;
}