0

我使用自引用结构在C中创建了一个列表,我想出了如何用列表做所有可能的事情,但删除最后一个元素,这让我困惑了几个星期,请帮助我,这就是我所做的。要了解这个程序的概念,您只需要查看函数main,而且 Delete,也许 insert。请注意,tis 代码是在 linux 上使用 ubuntu 中的 gcc 编译的。

/* self-referencial structure */
struct ListNode
{
    char data;
struct ListNode *nextNode ;
};


typedef struct ListNode ListNode;
typedef ListNode *ListNodePtr ;

/* function protypes */
void insert(ListNodePtr *sptr, char value); // insert element in list in alphabet order
void Delete (ListNodePtr *sptr);// PROBLEM HERE; remove last element from list
void printList(ListNodePtr);// output the contents of the list
void instructions(); // print instruction on screen

int main(void)
{
ListNodePtr startPtr = NULL ; // there are initially no nodes
int choice;
char item ;// char to be placed in list

/* Give instruction s and allow user to make a choice
 * insert , print, delete.
 */
instructions();
printf("?");
scanf("%d", &choice);

/* loop until sentinel value 3 */
while (!(choice == 4) )
{
    switch(choice)
    {
    case 1:
        printf("Enter a character");
        scanf("\n%c",&item);
        insert(&startPtr, item);
        printList(startPtr);
        break ;

    case 2:
        /* If the list is not empty */
        Delete(&startPtr);
        printList(startPtr);
        // if it is printf("the list is empty");
        break;
    case 3:
        printList(startPtr);
        break;
    default:
        printf("Invalid choice");
        break;
    }// end switch
    printf("?");
    scanf("%d",&choice);
}// end while
return 0 ;
}// END FUNCTION MAIN

/* display program choices/instruction to user */
void instructions(void)
{
     printf( "Enter your choice:\n"
    "1 to insert an element into the list.\n"
    "2 to delete an element from the list.\n"
    "3 to print the list\n"
    "4 to end.\n" );
 }

void insert(ListNodePtr *sptr, char value)
{
ListNodePtr currentPtr;
ListNodePtr previousPtr;
ListNodePtr newNode ;


/* create an area in memory the size of ListNode and
 *allocate to newPtr */
newNode = malloc(sizeof(ListNode)); // create new node

// if space is available
if (newNode != NULL)
{
    newNode->data = value;
    newNode->nextNode = NULL;

    previousPtr = NULL ;
    currentPtr = *sptr ;

    /* walk to correct location in the list */
    while (currentPtr != NULL && value > currentPtr->data)
    {
        previousPtr = currentPtr;
        currentPtr = currentPtr->nextNode;
    }// end while

    /* insert node at beginning of the list */
    if(previousPtr == NULL)
    {
        newNode->nextNode = *sptr;
        *sptr = newNode;
    }// end if

    else
    {
        previousPtr->nextNode = newNode;
        newNode->nextNode = currentPtr ;
    }
}// end if
else
{
    printf("%c not inserted , memory not available", value);
}

 }

void Delete(ListNodePtr *sptr)
{
ListNodePtr previousPtr;
ListNodePtr currentPtr;
ListNodePtr tempPtr;

// if there is only  one element
if((*sptr)->nextNode == NULL)
{
    tempPtr = *sptr; // hold on to Node being removed
    *sptr = (*sptr)->nextNode;
    free(tempPtr);
}
else
{
    previousPtr = *sptr ;
    currentPtr = (*sptr)->nextNode;

    // walk to end of the list
    while(currentPtr->nextNode != NULL)
    {
        previousPtr = *sptr;
        currentPtr = currentPtr->nextNode;
    }

    if(currentPtr != NULL)
    {
        // WHAT DO I DO HERE ??!!??
        // if the list has more than one two elements, 
        // all but the first element is deleted

        previousPtr->nextNode = currentPtr->nextNode;
        currentPtr = NULL ;
        free(currentPtr);
    }// end if
}// end else 


 }// END FUNCTION DELETE

 void printList(ListNodePtr currentPtr)
{
if(currentPtr == NULL)
printf("List is empty");
else
{
    while(currentPtr != NULL)
    {
        printf( "%c --> ", currentPtr->data );
        currentPtr = currentPtr->nextNode;
    } /* end while */
        printf( "NULL\n\n" );
    /* end else */


}
} 
4

1 回答 1

1

可能这有帮助

void Delete(ListNodePtr *sptr)
{
 ListNodePtr previousPtr;
 ListNodePtr currentPtr; 
 ListNodePtr tempPtr;

 if((*sptr)->nextNode == NULL)
 {
   tempPtr = *sptr; // hold on to Node being removed
   *sptr = (*sptr)->nextNode;
   free(tempPtr);
 }
else  
{
previousPtr = *sptr ;
currentPtr = (*sptr)->nextNode;

// walk to end of the list
while(currentPtr->nextNode != NULL)
{
    previousPtr=previousPtr->nextNode;//this always points to the previous node 
    currentPtr = currentPtr->nextNode;
}

//after loop ends currentPtr points to the last node

if(currentPtr != NULL)
{
    previousPtr->nextNode=NULL;//This deletes the currentPtr from the list
    free(currentPtr);
}// end if
}// end else 
}// END FUNCTION DELETE
于 2012-12-09T19:10:03.137 回答