Wyświetlenie indexu elementu listy

0

Witam, mam taki kod:

#include "faktura.h"

int main()
{
    ListElement_type *head;
    head = (ListElement_type *)malloc(sizeof(ListElement_type));
    head=NULL;


    int option=-1;
    int number=-1;
    int index=-1;

    while(option!=0)
    {

    system("cls");
    printf("\nAktualny stan listy: ");
    show(head);
    
    printf("\n\nDrogi uzytkowniku! Co chcesz zrobic?\n");
    printf("1. Dodac element na poczatek listy.\n");
    printf("2. Dodac element na koniec listy.\n");
    printf("3. Dodac element zgodnie z wybranym indeksem.\n");
    printf("4. Usunac element z poczatku listy.\n");
    printf("5. Usunac element z konca listy.\n");
    printf("6. Usunac element o wybranym indeksie.\n");
    printf("7. Wyswietlic liste w odwrotnej kolejnosci.\n");
    printf("0. Zakonczyc program.\n");
    
    
    

    scanf("%i", &option);

    switch (option)
    {
    case 0:
    	return 0;
    	break;
    
    case 1:
        printf("Wpisz liczbe jaka chcesz dodac: ");
        scanf("%i", &number);
        push_front(&head, number);
        break;
     case 2:
        printf("Wpisz liczbe jaka chcesz dodac: ");
        scanf("%i", &number);
        push_back(&head, number);
        break;
    case 3:
        printf("Wpisz liczbe jaka chcesz dodac: ");
        scanf("%i", &number);
        printf("Wpisz indeks: ");
        scanf("%i", &index);
        push_by_index(&head, number, index);
        break;
    case 4:
        pop_front(&head);
        break;
	case 5:
        pop_back(&head);
        break;

	case 6:
        printf("Wpisz indeks elementu, ktorego chcesz sie pozbyc raz na zawsze: ");
        scanf("%i", &index);
        pop_by_index(&head, index);
        break;
    
	case 7:
        show_reverse(head); Sleep(4000);
        break;
		    
    default:
        printf("Podaj wlasciwa opcje."); Sleep(2000);
        break;

    }
    
    }


    return 0;
}

void push_front(ListElement_type **head, int number)
{
	
    if(*head==NULL) {
    	*head = (ListElement_type *)malloc(sizeof(ListElement_type));
   		(*head)->data = number;
   		(*head)->previous=NULL;
    	(*head)->next = NULL;
	} else {
		ListElement_type *current;
    	        current=(ListElement_type *)malloc(sizeof(ListElement_type));
    	        current->data=number;
    	        current->previous=NULL;
    	        current->next=(*head);
    	        (*head)->previous=current;
    	        *head=current;
	}

}

void push_back(ListElement_type **head, int number)
{
	
	
	if(*head==NULL)
	{
		*head = (ListElement_type *)malloc(sizeof(ListElement_type));
   		(*head)->data = number;
   		(*head)->previous = NULL;
    	(*head)->next = NULL;
	}else
	{
		ListElement_type *current=*head;
		ListElement_type *new_element;
	
	    while (current->next != NULL) {
	        current = current->next;
	    }
	
	    current->next = (ListElement_type *)malloc(sizeof(ListElement_type));
	    current->next->data = number;
	    current->next->previous=current;
	    current->next->next = NULL;	
	}

    

}

void push_by_index(ListElement_type **head, int number, int position)
{
	if(position==0) push_front(head, number);
	else
	{
		if(position==list_size(*head)) push_back(head, number);
		else
		{
			ListElement_type *current=*head;
   			ListElement_type *tmp;

			int i=0;
  			while (current->next != NULL && i<position-1) {
            	current = current->next;
      			i++;
    		}	
    		
            tmp=current->next;
            current->next=(ListElement_type *)malloc(sizeof(ListElement_type));
            current->next->data=number;
            current->next->previous=current;
            tmp->previous=current->next;
            current->next->next=tmp;
           
        }
	}
		
		
}
    




void pop_front(ListElement_type **head)
{

    if (*head!=NULL) {
    	if((*head)->next==NULL) {
    		*head=NULL;
		} else {
			ListElement_type *tmp;
			tmp=(*head)->next;
   			free(*head);
   			*head=tmp;
   	 		(*head)->previous=NULL;
		}
   	 
	}
    
}

void pop_back(ListElement_type **head)
{
    
	if((*head)->next==NULL)
	{	
		*head=NULL;	
	}else
	{
		ListElement_type *current=*head;
		while (current->next->next!= NULL) {
        current = current->next;
    	}
   		 free(current->next);
   		 current->next=NULL;
	}   
}

 
void pop_by_index(ListElement_type **head, int position)
{
	if(position==0) pop_front(head);
	else
	{
		ListElement_type *current=*head;
	    ListElement_type *tmp;
		
		int i=0;
		while (current->next != NULL && i<position-1) {
	        current=current->next;
			i++;    
			}
			
		tmp = current->next;
	    current->next = tmp->next;
	    current->next->previous=current;
	    free(tmp);	
	}
	

}



void show(ListElement_type *head)
{
    printf("\n");
    if(head==NULL) printf("List is empty");
    else
    {
        ListElement_type *current=head;
        do {
            printf("%i", current->data); 
            printf("\n");
            current = current->next;
        }while (current != NULL);
    }
}

void show_reverse(ListElement_type *head)
{
    printf("\n");
    if(head==NULL) printf("List is empty");
    else
    {
        ListElement_type *current=head;
        while (current->next != NULL) {
            current = current->next; //idziemy na koniec listy
        }
        
        
        do {
        	printf("%i", current->data); 
            printf("\n");
            current = current->previous;
		}while(current!=NULL);

    }
}


int list_size(ListElement_type *head)
{
    int counter=0;
    if(head==NULL) return counter;
    else
    {
        ListElement_type *current=head;
            do {
            counter++;
            current = current->next;
            }while (current != NULL);
    }
    return counter;
}

i chciałbym zrobić aby przy show(head); wyświetlał się również index elementu. Jak mogę to zrobić?

1

Wywal i zrób od początku:

#include <stdio.h>
#include <mem.h>

typedef struct node_ { struct node_ *next,*prev; int data; } node;
typedef struct{ node *head,*tail; } list;

int isEmpty(list *lst) { return !lst->head; }
void show_forward(list *lst) { for(node *i=lst->head;i;i=i->next) printf("%d",i->data); }
void show_backward(list *lst) { for(node *i=lst->tail;i;i=i->prev) printf("%d",i->data); }

void show_forward_idx(list *lst)
{
	int order=0;
	for(node *i=lst->head;i;i=i->next) printf("%d: %d\n",++order,i->data); 
}

void show_backward_idx(list *lst)
{
	int order=0;
	for(node *i=lst->tail;i;i=i->prev) printf("%d: %d\n",++order,i->data);
}


node *make_node(int data,node *next,node *prev)
{
	node *nd=(node*)malloc(sizeof(node));
	nd->data=data;
	nd->next=next;
	nd->prev=prev;
	return nd;
}

void push_front(list *lst,int data)
{
	node *nd=make_node(data,lst->head,NULL);
	if(lst->head) lst->head->prev=nd; else lst->tail=nd;
	lst->head=nd;
}

void push_back(list *lst,int data)
{
	node *nd=make_node(data,NULL,lst->tail);
	if(lst->tail) lst->tail->next=nd; else lst->head=nd;
	lst->tail=nd;
}

int pop_front(list *lst,int *data)
{
	if(!lst->head) return 0;
	*data=lst->head->data;
	if(!(lst->head=lst->head->next)) lst->tail=NULL;
	return 1;
}

int pop_back(list *lst,int *data)
{
	if(!lst->tail) return 0;
	*data=lst->tail->data;
	if(!(lst->tail=lst->tail->prev)) lst->head=NULL;
	return 1;
}

void clear(list *lst)
{
	for(node *i=lst->head;i;free(i),i=lst->tail) lst->tail=i->next;
	lst->head=lst->tail=NULL;
}

Zauważ że kodu o wiele mniej.

1 użytkowników online, w tym zalogowanych: 0, gości: 1