将节点插入链表中的第n个位置

时间:2019-02-24 21:06:34

标签: linked-list

此代码具有许多功能,它们都可以工作,但不能称为位置功能。当我输入字符“ i”时,我应该能够先输入位置,然后输入数字,该数字将在链接列表的第n个位置插入数字,而且我不知道我的代码有什么问题,主要是位置函数,它输入位置和号码后崩溃。

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

struct list {
int info ;
struct list *next ; /* self reference */
};
struct list* position(struct list **head, int data, int pos) {
    if ((!head) || (pos < 0)) return NULL;

    struct list *h = *head;
    struct list *prev = NULL;

    for(int i = 0; i < pos; ++i) {
        if (!h) return NULL;
        prev = h;
        h = h->next;
    }

    struct list *p = (struct list *) malloc(sizeof(struct list));
    if (!p) return NULL;

    p->info = data;

    p->next = h;
    if (prev) prev->next = p;
    if (*head == h) *head = p;

    return p;
}

struct list * deleteNode(struct list *my_list)
{
    struct list *remove;
    if(my_list != NULL)
    {
        remove= my_list;
        my_list = (my_list)->next; //this removes head node from the list
        free(remove);

    }
    return my_list;
}
void print_list ( struct list *my_list ) {
    struct list *p ;
    for ( p = my_list ; p ; p = p -> next ) { //this prints the list
        printf ("%d ", p -> info ) ;
    }
 printf("\n");
 }

 struct list *push_front ( struct list * my_list , int value ) {
    struct list *newel ;
    newel = ( struct list *) malloc ( sizeof ( struct list )); //pushes the element to the front
    if ( newel == NULL ) {
        printf (" Error allocating memory \n");
        return my_list ;
    }
 newel -> info = value ;
 newel -> next = my_list ;
 return newel ;
 }

 struct list * push_back ( struct list * my_list , int value ) {
    struct list * cursor , * newel ;
    cursor = my_list ;
    newel = ( struct list *) malloc ( sizeof ( struct list ));//pushes the element to the back
    if ( newel == NULL ) {
        printf (" Error allocating memory \n");
        return my_list ;
    }
    newel -> info = value ;
    newel -> next = NULL ;
    if ( my_list == NULL )
    return newel ;
    while ( cursor -> next != NULL )
    cursor = cursor -> next ;
    cursor -> next = newel ;
    return my_list ;
  }

  void dispose_list ( struct list * my_list ) {
    struct list * nextelem ;
    while ( my_list != NULL ) { //disposes the memory
    nextelem = my_list -> next ;
    free ( my_list ) ;
    my_list = nextelem ;
    }
}

int main () {
    char input;
    int info, position1;
    struct list *my_list = NULL ;
    for(; ;)
    {
        scanf("%c", &input);
        switch(input)
        {
            case 'i':
                scanf("%d", &position1);
                scanf("%d", &info);
                my_list=position(my_list, info, position1);
                break;
            case 'b':
                scanf("%d", &info);
                my_list=push_front(my_list, info);
                break;

            case 'a':
                scanf("%d", &info);
                my_list=push_back(my_list, info);
                break;
            case 'p':
                print_list(my_list);
                break;
            case 'q':
                dispose_list(my_list);
                exit(0);
                break;
            case 'r':
                my_list=deleteNode(my_list);
                break;
        }
    }
}**

0 个答案:

没有答案
相关问题