-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvertisement.c
177 lines (151 loc) · 3.21 KB
/
advertisement.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define max 100
struct node {
char advertisement[max];
struct node *prev;
struct node *next;
} *head, *tail;
struct node *CreateNode(char *ad)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
if(n==NULL)
{
printf("memory not allocated");
return NULL;
}
strcpy(n->advertisement, ad);
n->next=NULL;
n->prev=NULL;
return n;
}
struct node *add()
{
char ad[max];
printf("Enter advertisement: ");
scanf("%s", ad);
struct node *newNode=CreateNode(ad);
if(head==NULL)
{
head=newNode;
printf("Advertisement added. \n");
return head;
}
struct node *temp=head;
while(temp->next !=NULL)
{
temp=temp->next;
}
temp->next=newNode;
newNode->prev=temp;
printf("Advertisement added. \n");
tail=newNode;
return head;
}
struct node *DisplayAd() {
struct node *temp=head;
if (head == NULL)
{
printf("No advertisments. \n");
return head;
}
printf("%s \n", temp->advertisement);
return head;
}
struct node *DisplayAdBeforeAfter() {
int option;
struct node *temp=head;
if (!temp) {
printf("No advertisements.\n");
return head;
}
do
{
printf("\n1. Show next advertisement \n2. Show previous advertisemnt\n3. Exit\nEnter your choice: ");
scanf("%d", &option);
switch(option)
{
case 1:
if(temp->next == NULL)
{
printf("No advertisement ahead. \n");
}
else
{
temp=temp->next;
printf("%s ", temp->advertisement);
}
break;
case 2:
if(temp->prev == NULL)
{
printf("No advertisement behind. \n");
}
else
{
temp=temp->prev;
printf("%s ", temp->advertisement);
}
break;
case 3:
return head;
default:
printf("enter valid choice \n");
}
}while(option != 3);
return head;
}
struct node *removeAd() {
if (head == NULL) {
printf("No advertisements to remove.\n");
return NULL;
}
if (head->next == NULL) {
free(head);
head = tail = NULL;
}
else
{
struct node *temp = head;
while(temp->next !=NULL)
{
temp=temp->next;
}
struct node *toDel=temp;
temp=temp->prev;
temp->next=NULL;
toDel->prev=NULL;
free(toDel);
}
printf("Advertisement removed.\n");
return head;
}
int main() {
head = NULL;
tail = NULL;
int choice, item;
do
{
printf("\n1. Add advertisement \n2. Remove advertisemnt\n3. Display\n4.Exit \nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
head=add();
break;
case 2:
head=removeAd();
break;
case 3:
head=DisplayAd();
head=DisplayAdBeforeAfter();
break;
case 4:
exit(0);
default:
printf("\nPlease enter a valid choice\n");
}
} while(choice != 4);
}