Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*Napisz program z podzia³em na funkcje z parametrami, w którym u¿yjesz dwukierunkowej
- listy cyklicznej do przechowywania ci¹gów znaków o d³ugoœci nieprzekraczaj¹cej 20 znaków.
- Dodawanie nowych znaków nale¿y zakoñczy po tym jak u¿ytkownik wprowadzi ci¹g
- „koniec”.*/
- struct dll_node
- {
- char data[20];
- struct dll_node *prev, *next;
- };
- struct dll_node *create_list(char data[20])
- {
- struct dll_node *front=(struct dll_node*)malloc(sizeof(struct dll_node));
- if(front!=NULL)
- {
- front->data[20]=data;
- front->prev=front->next=NULL;
- }
- return front;
- }
- struct dll_node *insert_front(struct dll_node *front, struct dll_node *new_node)
- {
- new_node->next=front;
- front->prev=new_node;
- return new_node;
- }
- void insert_back(struct dll_node *back, struct dll_node *new_node)
- {
- back->next=new_node;
- new_node->prev=back;
- }
- void insert_after(struct dll_node *node, struct dll_node *new_node)
- {
- new_node->prev=node;
- new_node->next=node->next;
- node->next->prev=new_node;
- node->next=new_node;
- }
- struct dll_node *find_spot(struct dll_node *front, char data[20])
- {
- struct dll_node *prev=NULL;
- while((front!=NULL) && (front->data>data))
- {
- prev=front;
- front=front->next;
- }
- return prev;
- }
- struct dll_node *insert_node(struct dll_node *front, char data[20])
- {
- if(front==NULL)
- return NULL;
- struct dll_node *new_node=(struct dll_node*)malloc(sizeof(struct dll_node));
- if(new_node!=NULL)
- {
- new_node->data[20]=data;
- new_node->prev=new_node->next=NULL;
- if(front->data<=data)
- {
- return insert_front(front,new_node);
- }
- else
- {
- struct dll_node *node=find_spot(front,data);
- if(node->next!=NULL)
- {
- insert_after(node, new_node);
- }
- else
- {
- insert_back(node, new_node);
- }
- }
- }
- return front;
- }
- struct dll_node *find_node(struct dll_node *front, char data[20])
- {
- while((front!=NULL) && (front->data!=data))
- {
- front=front->next;
- }
- return front;
- }
- void remove_list(struct dll_node **front)
- {
- while(*front!=NULL)
- {
- struct dll_node *next=(*front)->next;
- free(*front);
- *front=next;
- }
- }
- void print_list(struct dll_node *front)
- {
- for(;front!=NULL;front=front->next)
- {
- printf("%s\n",front->data);
- }
- puts(" ");
- }
- int main(void)
- {
- struct dll_node *front=create_list(0);
- char input[20];
- while(1)
- {
- scanf("%s",&input);
- if(!strncmp(input,"koniec",6))
- break;
- insert_node(&(*front),input);
- }
- print_list(&(*front));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement