Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my uni assignment.i tried to complete it,but stŕuck here.first point is okay.
Second point its work like alphabatically but i need list from head yo tail.
All other points are still there kindly any one give me solution.i shell be very thankfull to you,forever in my whole life.
This is final assignement if i faild in this i lost my cgpa.million thousands thanks in advance.

Your program should implement a doubly linked list of strings. Each node should contain a string (choose a suitable size), next node pointer, and previous node pointer. Then write functions to do the following linked list operations:
• A function named printFor that prints the items in the list starting at the head.
• A function named printRev that prints the items in the list starting at the tail.
• A function named size that returns the size (number of nodes) in the linked list.
• A function lookup that checks if a given string is contained in the linked list. It should return a node pointer if the string was found, or NULL if not found.
• An add function that adds a string at the end of the list if it is not already contained in the linked list. If the string is already contained in the list the add function should print “Duplicate entry” and do nothing else.
• A remov function that removes a given string from the list. If the string is not found in the list the remov function should print “Not found”. Note: We call it remov without the ‘e’ to avoid a conflict with a function called remove in stdio.h.
Also write a main function to test the other functions. Your main function should do the following in the order shown. – Initialize an empty linked list. – Add the following 5 strings to the list: one, two, three, four, five – (Attempt to) remove the following 2 strings: four, seven – Print the size of the list. – Add the following 5 strings to the list: two, five, six, seven, eight – Print the list forwards, then print the list backwards.

What I have tried:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct info {
    char         name[10];
    struct info *next;
} info;

/* List Functions */
/* -------------- */
info *newnode ( void ) {
    info *new = malloc( sizeof *new );
    if ( new ) {
        new->next = NULL;
    }
    return new;
}

info *list_prepend ( info *head, char *name ) {
    info *new = newnode( );
    if ( new ) {
        new->next = head;
        strcpy( new->name, name );
    }
    return new;
}

void list_print ( info *head ) {
    while ( head ) {
        printf( "%s\n", head->name );
        head = head->next;
    }
}

int list_count ( info *head ) {
    int result = 0;
    while ( head ) {
        result++;
        head = head->next;
    }
    return result;
}


int compare ( const void *a, const void *b ) {
    info * const *pa = a;
    info * const *pb = b;
    return strcmp( (*pa)->name, (*pb)->name );
}

info *list_sort ( info *head ) {
    int i, n;
    info **array;

    /* create array out of a list */
    n = list_count( head );
    array = malloc( n * sizeof *array );
    for ( i = 0 ; i < n ; i++ ) {
        array[i] = head;
        head = head->next;
    }

    /* sort it */
  //  qsort( array, n, sizeof(array[0]) );
  
    /* create list out of array */
   
   qsort( array, n, sizeof(array[0]), compare );
   for ( i = 0 ; i < n-1 ; i++ ) {
        array[i]->next = array[i+1];
    }
    array[n-1]->next = NULL;
    head = array[0];

    free( array );
    return head;
}

int main ( ) {
    info *list = NULL;
    char *names[] = { "one", "two", "three", "four", "five","six", "sev", "eight" };
    int i;
int xxx= sizeof(names);
    /* create a list */
    for ( i = 0 ; i < sizeof(names)/sizeof(names[0]) ; i++ ) {
        list = list_prepend( list, names[i] );
    }
    printf( "\nStarting at the tttail\n" );
    
    list_print( list );
  
   
  printf( "\nStarting at the head\n" );
    list = list_sort( list );
    list_print( list );
    
    return 0;
}
Posted
Updated 18-Apr-18 21:34pm
Comments
CPallini 19-Apr-18 3:44am    
Why did you write a sort function?

This is your homework and we will not provide you a solution. We will answer specific questions though.

One significant thing : nowhere in the problem definition is it stated that the listed has to be maintained in alphabetical order. Therefore, why bother? This means you don't need a sort function. You are supposed to add items to the END of the list, not the head so you need an add function, not a prepend function.
 
Share this answer
 
Comments
Waqas Ali 18-Apr-18 14:32pm    
Thanks for your advice, as i told you earlier i tried.next i have no time.
Rick York 18-Apr-18 16:39pm    
This will probably sound harsh but if you want to be a programmer then you need to make time. You will not learn anything if someone else does your work for you.

I recommend doing a search for "doubly linked list" or a similar phrase and you will see lots of tutorials on how they work and how to implement them. In fact, I believe there are a few on this site. Starting reading soon as your time is running out. Here are some articles at this site on the topic : Linked Lists
You must figure it out yourself, so learn better about the linked list.

When you need alphabetical order, you implement it in such way, that on the insertion the order is respected or sort the filled linked list.

Learn to use the debugger!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900