Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- const int STRING_MAX = 100;
- struct list_node_s {
- char* data;
- struct list_node_s* prev_p;
- struct list_node_s* next_p;
- };
- struct list_s {
- struct list_node_s* h_p;
- struct list_node_s* t_p;
- };
- void Insert(struct list_s* list_p, char string[]);
- void Print(struct list_s* list_p);
- void Remove(struct list_s* list_p, char string[]);
- void Empty_node(struct list_node_s* node_p);
- struct list_node_s* A_node(int size);
- struct list_node_s* A_node(int size) {
- struct list_node_s* temp_p;
- temp_p = malloc(sizeof(struct list_node_s));
- temp_p->data = malloc(size*sizeof(char));
- temp_p->prev_p = NULL;
- temp_p->next_p = NULL;
- return temp_p;
- }
- void Insert(struct list_s* list_p, char string[]) {
- struct list_node_s* curr_p = list_p->h_p;
- struct list_node_s* temp_p;
- while (curr_p != NULL)
- if (strcmp(string, curr_p->data) == 0) {
- printf(" Dieser Name befindet sich bereits in der Liste:%s", string);
- return;
- } else if (strcmp(string, curr_p->data) < 0) {
- break;
- } else {
- curr_p = curr_p->next_p;
- }
- temp_p = A_node(strlen(string) + 1);
- strcpy(temp_p->data, string);
- if ( list_p->h_p == NULL ) {
- /* Liste ist leer */
- list_p->h_p = list_p->t_p = temp_p;
- } else if ( curr_p == NULL) {
- /* am Ende der Liste einfügen */
- temp_p->prev_p = list_p->t_p;
- list_p->t_p->next_p = temp_p;
- list_p->t_p = temp_p;
- } else if (curr_p == list_p->h_p) {
- /* am Anfang der Liste einfügen */
- temp_p->next_p = list_p->h_p;
- list_p->h_p->prev_p = temp_p;
- list_p->h_p = temp_p;
- } else {
- /* middle of list, string < curr_p->data */
- temp_p->next_p = curr_p;
- temp_p->prev_p = curr_p->prev_p;
- curr_p->prev_p = temp_p;
- temp_p->prev_p->next_p = temp_p;
- }
- }
- void Print(struct list_s* list_p) {
- struct list_node_s* curr_p = list_p->h_p;
- printf(" Namen:\n");
- while (curr_p != NULL) {
- printf("%s", curr_p->data);
- curr_p = curr_p->next_p;
- }
- printf("\n");
- }
- void Empty_node(struct list_node_s* node_p) {
- free(node_p->data);
- free(node_p);
- }
- void Delete(struct list_s* list_p, char string[]) {
- struct list_node_s* curr_p = list_p->h_p;
- /* gesuchten String finden */
- while (curr_p != NULL)
- if (strcmp(string, curr_p->data) == 0) {
- break;
- } else if (strcmp(string, curr_p->data) < 0) {
- /* printf("%s befindet sich nicht in der Liste\n", string)*/;
- return;
- } else {
- curr_p = curr_p->next_p;
- }
- if (curr_p == NULL) {
- /*printf("%s befindet sich nicht in der Liste\n", string)*/;
- } else { /* curr_p != NULL */
- if (curr_p->prev_p == NULL && curr_p->next_p == NULL) {
- list_p->h_p = list_p->t_p = NULL;
- } else if (curr_p->prev_p == NULL) {
- /* Erste node in liste */
- list_p->h_p = curr_p->next_p;
- list_p->h_p->prev_p = NULL;
- } else if (curr_p->next_p == NULL) {
- /* letzter node in der Liste */
- list_p->t_p = curr_p->prev_p;
- list_p->t_p->next_p = NULL;
- } else {
- /* Node in middle of list */
- curr_p->prev_p->next_p = curr_p->next_p;
- curr_p->next_p->prev_p = curr_p->prev_p;
- }
- Empty_node(curr_p);
- }
- }
- int main()
- {
- char * pch;
- struct list_s list ;
- list.h_p = list.t_p = NULL;
- FILE *ptr_file;
- char laenge[100];
- char * substring;
- char plus ='+';
- char minus ='-';
- int counter = 1;
- ptr_file = fopen("D:/Users/Nima/Desktop/test123.txt","r");
- if (!ptr_file)
- return 1;
- while (fgets(laenge,1000, ptr_file)!=NULL)
- {
- if (strlen(laenge)<4)
- {
- printf("\nName fehlt in Zeile: %d",+counter, "\n");
- return -1;
- }
- else if (strchr(laenge, plus))
- {
- counter++;
- pch = strstr (laenge,"+");
- strncpy (pch," ",1);
- Insert(&list,laenge);
- }
- else if (strchr(laenge, minus))
- {
- counter++;
- pch = strstr (laenge,"-");
- strncpy (pch," ",1);
- Delete(&list,laenge);
- }else{
- printf("\nZeichen fuer Add oder Remove fehlt in Zeile: %d",+counter, "\n");
- return -1;
- }
- }
- Print(&list);
- fclose(ptr_file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement