c中的菜单驱动程序,用于在链表上执行各种操作

时间:2014-01-26 14:20:29

标签: c function linked-list

以下代码适用于除deleteEnd函数之外的所有函数。当我执行程序时,所有函数都执行它们应该执行的任务,但deleteEnd函数什么都不做。执行deleteEnd函数时链表中没有变化。请帮助!!!!

#include<stdio.h>
#include<conio.h>
struct node{
   int data;
   struct node *link;
}*head = NULL, *new_node, *ptr=NULL, *prev_ptr, *temp;

void insertBeg(){
    int info;
    new_node = (struct node*)malloc(sizeof(struct node));
    printf("\nEnter data : ");
    scanf("%d",&info);
    new_node->data=info;
    new_node->link=NULL;
        if(head==NULL){
            head=new_node;
        }
        else{
            new_node->link=head;
            head=new_node;
        }
}

void insertEnd(){
    int info;
    new_node = (struct node*)malloc(sizeof(struct node));
    printf("\nEnter data : ");
    scanf("%d",&info);
    new_node->data=info;
    if(head==NULL){
        head=new_node;
        new_node->link=NULL;
    }
    else{
        prev_ptr=head;
        ptr=head->link;
        while(ptr!=NULL){
            prev_ptr=ptr;
            ptr=ptr->link;
        }
        prev_ptr->link=new_node;
        new_node->link=NULL;
    }
}

void displayNode(){
    printf("\nLinked List is : ");
    ptr=head;
    while(ptr!=NULL){
        printf("%d--->",ptr->data);
        ptr=ptr->link;
    }
}

void deleteBeg(){
    if(head==NULL){
        printf("\nUnderflow");
    }
    else{
        temp=head;
        head=head->link;
        free(temp);
    }
}

void deleteEnd(){
    if(head==NULL){
        printf("\nUnderflow");
    }
    else{
        prev_ptr=head;
        ptr=head->link;
        while(ptr!=NULL){
            prev_ptr=ptr;
            ptr=ptr->link;
        }
        prev_ptr->link=NULL;
        free(ptr);
    }
}

void traverse(){
    int count=0;
    ptr=head;
    while(ptr!=NULL){
        ptr=ptr->link;
        count++;
    }
    printf("\nNumber of elements in the list are : %d",count);
}

void main(){
    int choice,ch='y';
    clrscr();
    label:
    printf("\nPress 1 to insert at beg\n2 to insert at end");
    printf("\n3 to delete from beg\n4 to delete from end");
    printf("\n5 to display the list\n6 to traverse the linked list : ");
    scanf("%d",&choice);
    switch(choice){
        case 1: insertBeg();
            break;
        case 2: insertEnd();
            break;
        case 3: deleteBeg();
            break;
        case 4: deleteEnd();
            break;
        case 5: displayNode();
            break;
        case 6: traverse();
            break;
        default: printf("\nInvalid Option");
    }
    printf("\nPress y to continue or any other key to exit : ");
    scanf("%s",&ch);
    if(ch=='y' || ch=='Y'){
        goto label;
    }
    getch();
}

1 个答案:

答案 0 :(得分:3)

循环

while(ptr!=NULL){

一直运行到ptrNULL,这意味着后来的调用free(ptr)无效。

你可以通过一次迭代退出循环来解决这个问题

void deleteEnd(){
    if(head==NULL){
        printf("\nUnderflow");
    }
    else{
        prev_ptr=head;
        ptr=head;
        while(ptr->link!=NULL){
            prev_ptr=ptr;
            ptr=ptr->link;
        }
        prev_ptr->link=NULL;
        free(ptr);
    }
}

对于最终元素为head

的情况,您可能需要考虑对此进行特殊处理。