Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_STATE 50
- typedef struct usa_primaries *ptr;
- typedef struct usa_primaries{
- struct primaries_date{
- int month;
- int hour;
- }primaries_date;
- char state[MAX_STATE];
- int open;
- ptr next;
- }usa_primaries;
- void add2list(ptr *head, int month, int hour,int open, char *state){
- ptr node = (ptr)malloc(sizeof(usa_primaries));
- if(!node){
- exit(1);
- }
- node->open = open;
- strcpy(node->state,state);
- node->primaries_date.month = month;
- node->primaries_date.hour = hour;
- if(*head == NULL){
- node->next = NULL;
- }
- else{
- node->next = *head;
- }
- *head = node;
- }
- void freelist(ptr *head){
- ptr curr;
- if(*head != NULL){
- curr = (*head)->next;
- for(;curr;curr=curr->next){
- free(*head);
- *head = curr;
- }
- free(*head);
- }
- }
- void print_list(ptr head){
- for(;head;head = head->next){
- printf("\nmonth: %d, hour: %d, state: %s",head->primaries_date.month, head->primaries_date.hour, head->state );
- }
- printf("\n");
- }
- void del_dates(ptr *head, int month1, int hour1, int month2, int hour2){
- ptr from, fromprev, to;
- if(*head == NULL){
- printf("\nno nodes" );
- return;
- }
- if((month1 > month2) || ((month1 == month2)&&(hour1 > hour2))){
- del_dates(head, month2,hour2,month1, hour1);
- return;
- }
- from = *head;
- for(;from;from = from->next){
- if((from->primaries_date.month == month1) && (from->primaries_date.hour == hour1)){
- break;
- }
- }
- if(!from) {
- printf("\nno such date: month %d, hour: %d",month1, hour1 );
- return;
- }
- if(from != *head){
- fromprev = *head;
- for(;fromprev->next != from;fromprev = fromprev->next)
- ;
- }
- to = *head;
- for(;to;to = to->next){
- if((to->primaries_date.month == month2) && (to->primaries_date.hour == hour2)){
- break;
- }
- }
- if(!to) {
- printf("\nno such date: month %d, hour: %d",month2, hour2 );
- return;
- }
- if(from == *head){
- *head = to->next;
- return;
- }
- fromprev->next = to->next;
- }
- #define NUM_STATES 10
- int main(){
- ptr list1 = NULL;
- int m,h,o;
- int month_to_delete1, month_to_delete2, hour_deleate1, hour_deleate2;
- char c;
- char state[MAX_STATE];
- int i;
- printf("\nPlease enter data, thanks" );
- for(i=0;i<NUM_STATES;i++){
- scanf("%d %d %d %s",&m,&h,&o,state);
- add2list(&list1, m,h,o,state);
- }
- /*
- while (scanf("%d %d %d %s",&m,&h,&o,state)!=EOF) {
- add2list(&list1, m,h,o,state);
- } */
- print_list(list1);
- printf("\n" );
- while((c=getchar()) != '\n') /*cleaning the buffer after the last scanf*/
- ;
- putchar(c);
- scanf("%d %d %d %d", &month_to_delete1, &hour_deleate1, &month_to_delete2, &hour_deleate2);
- del_dates(&list1, month_to_delete1,hour_deleate1,month_to_delete2, hour_deleate2);
- print_list(list1);
- freelist(&list1);
- print_list(list1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement