Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int num;
- int occ;
- struct node *next;
- struct node *prev;
- };
- void printlist(struct node *list_head){
- struct node *iteratore = list_head;
- while (iteratore != NULL){
- printf("{num: %d, occ: %d}->", iteratore->num, iteratore->occ);
- iteratore = iteratore->next;
- }
- printf("\b\b \n");
- return;
- }
- struct node *createnode(int x){
- struct node *node = (struct node*)malloc(sizeof(struct node));
- node->num = x;
- node->occ = 1;
- return node;
- }
- void insertnode(struct node **list_head, int x){
- // inserimento in testa
- if (*list_head == NULL){
- struct node *node = createnode(x);
- node->next = NULL;
- node->prev = NULL;
- *list_head = node;
- return;
- }
- // inserimento in lista
- struct node *iteratore = *list_head;
- struct node *node = createnode(x);
- while(iteratore->next != NULL){
- if (x == iteratore->num){
- iteratore->occ +=1;
- node->occ = iteratore->occ;
- }
- iteratore = iteratore->next;
- }
- node->next = NULL;
- node->prev = iteratore;
- iteratore->next = node;
- return;
- }
- struct node *list(int n, int A[n]){
- struct node *list_head = NULL;
- int i;
- for (i = 0; i < n; i++){
- insertnode(&list_head, A[i]);
- }
- return list_head;
- }
- void reverse(struct node **list_head){
- if (*list_head == NULL){
- printf ("Lista vuota!");
- return;
- }
- struct node *iteratore = *list_head;
- while (iteratore->next != NULL){
- struct node *temp = iteratore->next;
- // reversing list head
- if (iteratore == *list_head){
- iteratore->prev = iteratore->next;
- iteratore->next = NULL;
- }
- else{
- struct node *app = iteratore->prev;
- iteratore->prev = iteratore->next;
- iteratore->next = app;
- }
- iteratore = iteratore->prev;
- }
- iteratore->next = iteratore->prev;
- iteratore->prev = NULL;
- *list_head = iteratore;
- return;
- }
- int main(void){
- int n = 7;
- int array[7] = {2,7,8,2,1,5,2};
- struct node *list_head;
- list_head = list(n, array);
- printlist(list_head);
- reverse(&list_head);
- printlist(list_head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement