Hello. I need to describe a function that, in the list of identical elements from each group in succession, constitutes only one element. I don’t understand how to write a function even to delete just one particular element. While I wrote everything else.

struct list { int info; list *next; }; void New(list* &head, list* &tail) { int c; scanf("%d",&c); head=new list; head->info=c; head->next=NULL; tail=head; } void Add(list* &tail) { int c; list *T; scanf("%d",&c); T=new list; T->info=c; T->next=NULL; tail->next=T; tail=T; } void Delete() void Print(list *head, list *tail) { list *T; T=new list; T=head; while (T!=NULL) { printf("%d ",T->info); T=T->next; } } void main() { list *head, *tail; int i, n,a; printf("\nEnter number of elements: "); scanf("%d",&n); New(head,tail); for (i=1;i<=n-1;i++) { Add(tail);} printf("Your list: "); Print(head,tail); Delete(); printf("Your new list: "); Print(head,tail); getch(); } 

Help to understand the algorithm for constructing the delete function on some example (you can even on my :) with detailed comments.

    2 answers 2

    Without generalizations of functions of processing single-linked lists. Your task. Just trained, I hope these sketches will be useful to you. What is not clear, ask.

     #include <stdio.h> #include <stdlib.h> #include <errno.h> struct el { int info; struct el *next; }; static void fatal(char *txt) { perror(txt); exit(1); } static struct el * Add_new (struct el *elem, FILE *in) { int data = 0; for (;;) { printf ("Enter number: "); fflush(stdout); if (fscanf(in,"%d",&data) == 1) break; if (feof(in)) return NULL; if (ferror(in)) fatal("input"); char buf[1024]; fgets(buf,1024,in); if (buf[0] == '.') return NULL; printf ("Some error. Again "); } struct el *el = malloc(sizeof(*el)); el->info = data; el->next = NULL; if (elem) elem->next = el; return el; } void Pri_list (char *title,struct el *list) { puts(title); while (list) { printf ("%d\n",list->info); list = list->next; } } struct el * Get_seq (struct el * elem) { if (elem) { int info = elem->info; while (elem->next && elem->next->info == info) elem = elem->next; } return elem; } void Del_list (struct el *elem) { while (elem) { struct el *p = elem->next; free(elem); elem = p; } } main () { struct el *list, *elem; if (list = elem = Add_new(NULL,stdin)) while(elem = Add_new(elem,stdin)); Pri_list("",list); struct el *last; for (elem = list;last = Get_seq(elem);elem = elem->next) { if (elem != last) { struct el *p = elem->next; elem->next = last->next; last->next = NULL; Del_list(p); } } Pri_list("Result",list); exit(0); } 

    Some explanations on the code. The end of the list is defined by next == NULL.

    Add_new () reads an integer, creates a new list item and inserts it after the specified one. It is so long that it does not get stuck with a data entry error. It returns the address of a new item; this makes it very easy to build a list by adding items to the end.

    Pri_list () scrolls through the list and prints each item in a new line.

    Del_list () removes the list items from the specified to the end (frees the memory).

    Get_seq () returns the address of the data element in the sequence of elements with the same info, starting with the given one. Returns the last address of the last element in such a chain. If an element with another info follows the given (its argument) element, then it returns its argument (a chain of one link).

    Application logic in main () (and Get_seq ()). We read the list from standard input, we print it.

    In the for (;;) loop, we go through the chains of elements with the same info. Such a chain is allocated by Get_seq (). If it is longer than one element, then we cut out all the elements of the chain from the list except the first one (by changing the next pointer of the first element of the chain to the next last element in the chain). To delete the tail of the chain from memory, zero next its last element and call Del_list () with the second element of the chain.

    We print the result. (It seems to work).

    UPD Added specifically to remove the list items by the specified value of the element. You wondered how to remove the item?

     struct el * Del_info (struct el *list, int info, int *cnt, int *done) { *done = 0; struct el *p = list, *prev = NULL; while (list) { if (*cnt < 1) break; if (list->info == info) { if (prev) { prev->next = list->next; free(list); (*done)++; (*cnt)--; list = prev->next; } else { p = list->next; free(list); (*done)++; (*cnt)--; list = p; } } else { prev = list; list = list->next; } } return p; } 

    Removes from the list up to cnt elements with the specified value. The number of deleted returns in the done parameter. Returns a pointer to the new first item in the list (the previous first can be deleted).

    In general, the processing of simply-connected lists is usually embedded in the main logic of the algorithm, somewhere in the loop of iterating over the list elements. This is often more convenient than selecting special functions for primitive list operations.

    • Sorry for not asking. there was no time at all. right now I'll take to disassemble. - Dexter384

    If you don’t understand, you should still re-read what a single-linked list is. To delete a specific node element name access only to this element, you need (in pseudocode):

    1. node->data = node->next->data - copy the data from the next element.
    2. temp = node->next - remember the next item.
    3. node->next = node->next->next - transfer the link to the element following the next.
    4. free(temp) - freeing the memory of the former next element.