具有双链接列表的字符串删除alphabatical订单 [英] String with double link list remove alphabatical order

查看:113
本文介绍了具有双链接列表的字符串删除alphabatical订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的uni任务。我试图完成它,但是stŕuckhere.first point is ok。

第二点它的工作像alphabatically但我需要从头尾的列表。

所有其他积分仍然在那里,任何人都给我解决方案。我非常感谢你,永远在我的一生。

这是最后的分配,如果我在这个faild我提前感谢你丢失了我的cgpa.million数。



你的程序应该实现一个双向链接的字符串列表。每个节点应包含一个字符串(选择合适的大小),下一个节点指针和前一个节点指针。然后编写函数来执行以下链表操作:

•一个名为printFor的函数,用于打印从头开始的列表中的项。

•一个名为printRev的函数,它从尾部开始打印列表中的项目。

•一个名为size的函数,它返回链表中的大小(节点数)。

•一个函数查找,用于检查链接列表中是否包含给定的字符串。如果找到字符串,它应该返回一个节点指针,如果没找到则返回NULL。

•添加函数,如果链表中尚未包含字符串,则在列表末尾添加字符串。如果字符串已经包含在列表中,则add函数应该打印Duplicate entry并且不执行任何其他操作。

•一个删除函数,用于从列表中删除给定的字符串。如果在列表中找不到该字符串,则删除功能应打印未找到。注意:我们将其称为删除而没有'e',以避免与stdio.h中名为remove的函数冲突。

另外写一个main函数来测试其他函数。您的主要功能应按所示顺序执行以下操作。 - 初始化空链表。 - 将以下5个字符串添加到列表中:一,二,三,四,五 - (尝试)删除以下两个字符串:四,七 - 打印列表的大小。 - 将以下5个字符串添加到列表中:两个,五个,六个,七个,八个 - 向前打印列表,然后向后打印列表。



我尝试过:



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

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

/ *列表函数* /
/ * -------------- * /
info * newnode(void){
info * new = malloc(sizeof * new);
if(new){
new-> next = NULL;
}
返回新的;
}

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

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;
}
返回结果;
}


int compare(const void * a,const void * b){
info * const * pa = a;
info * const * pb = b;
返回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;
}

/ * sort it * /
// qsort(array,n,sizeof(array [0]));

/ *创建列表中的数组* /

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);
返回头;
}

int main(){
info * list = NULL;
char * names [] = {one,two,three,four,five,six,sev,eight};
int i;
int xxx = sizeof(姓名);
/ *创建一个列表* /
for(i = 0; i< sizeof(names)/ sizeof(names [0]); i ++){
list = list_prepend(list,姓名[i]);
}
printf(\ n开始于tttail \ n);

list_print(list);


printf(\\\
Starting at the head \ n);
list = list_sort(list);
list_print(list);

返回0;
}

解决方案

这是你的作业,我们为你提供解决方案。我们将回答具体问题。



一个重要的事情:在问题定义中没有任何地方说明列出的内容必须按字母顺序维护。所以,为什么要这么麻烦?这意味着您不需要排序功能。您应该将项目添加到列表的END而不是头部,因此您需要添加函数,而不是前置函数。


您必须自己弄明白,所以要更好地了解链接列表。



当您需要按字母顺序排列时,您可以通过命令得到尊重或排序填充的链表。



学习使用调试器!! !

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;
}

解决方案

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.


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!!!


这篇关于具有双链接列表的字符串删除alphabatical订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆