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;
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;
n = list_count( head );
array = malloc( n * sizeof *array );
for ( i = 0 ; i < n ; i++ ) {
array[i] = head;
head = head->next;
}
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);
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;
}