在C菜单驱动程序链表上执行各种操作 [英] Menu driven program in c to perform various operations on a linked list

查看:170
本文介绍了在C菜单驱动程序链表上执行各种操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code工作正常,除了deleteEnd功能的所有功能。当我执行该程序的所有功能执行他们应该的任务,但deleteEnd函数什么都不做。有上执行deleteEnd功能中的链表没有变化。请帮助!

 #包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;
结构节点{
   int数据;
   结构节点*链接;
} *头= NULL,* new_node,* PTR = NULL,* prev_ptr,*温度;无效insertBeg(){
    诠释信息;
    new_node =(结构节点*)malloc的(的sizeof(结构节点));
    的printf(\\ n输入数据:);
    scanf函数(%d个,&安培;信息);
    new_node->数据=信息;
    new_node->链接= NULL;
        如果(头== NULL){
            头= new_node;
        }
        其他{
            new_node->链接=头;
            头= new_node;
        }
}无效insertEnd(){
    诠释信息;
    new_node =(结构节点*)malloc的(的sizeof(结构节点));
    的printf(\\ n输入数据:);
    scanf函数(%d个,&安培;信息);
    new_node->数据=信息;
    如果(头== NULL){
        头= new_node;
        new_node->链接= NULL;
    }
    其他{
        prev_ptr =头;
        PTR =流浆>链接;
        而(PTR!= NULL){
            prev_ptr = PTR;
            PTR = ptr->链接;
        }
        prev_ptr->链接= new_node;
        new_node->链接= NULL;
    }
}无效displayNode(){
    的printf(\\ N-连接名单是:);
    PTR =头;
    而(PTR!= NULL){
        的printf(%d个 - >中,ptr->数据);
        PTR = ptr->链接;
    }
}无效deleteBeg(){
    如果(头== NULL){
        的printf(\\ nUnderflow);
    }
    其他{
        TEMP =头;
        头=流浆>链接;
        免费(TEMP);
    }
}无效deleteEnd(){
    如果(头== NULL){
        的printf(\\ nUnderflow);
    }
    其他{
        prev_ptr =头;
        PTR =流浆>链接;
        而(PTR!= NULL){
            prev_ptr = PTR;
            PTR = ptr->链接;
        }
        prev_ptr->链接= NULL;
        免费(PTR);
    }
}无效移动(){
    诠释计数= 0;
    PTR =头;
    而(PTR!= NULL){
        PTR = ptr->链接;
        算上++;
    }
    的printf(列表中的元素\\ n已接收的数:%d,计数);
}无效的主要(){
    INT的选择,CH ='Y';
    clrscr();
    标签:
    的printf(\\ n preSS 1到乞求插入\\ N2在末尾插入);
    的printf(\\ N3从BEG删除\\ N4从最终删除);
    的printf(\\ N5来显示列表\\ N6遍历链接列表:);
    scanf函数(%d个,&安培;选择);
    开关(选择){
        案例1:insertBeg();
            打破;
        案例2:insertEnd();
            打破;
        案例3:deleteBeg();
            打破;
        情况4:deleteEnd();
            打破;
        情况5:displayNode();
            打破;
        案例6:移动();
            打破;
        默认:输出(\\ n无效选项);
    }
    的printf(\\ n preSS y继续或任何其他键退出:);
    scanf函数(%S,&安培; CH);
    如果(CH =='Y'|| CH =='Y'){
        转到标号;
    }
    残培();
}


解决方案

循环

 而(PTR!= NULL){

运行,直到 PTR NULL ,这意味着以后调用免费(PTR) 没有效果。

您可以通过退出循环一次迭代更快,而不是解决这个问题。

 无效deleteEnd(){
    如果(头== NULL){
        的printf(\\ nUnderflow);
    }
    其他{
        prev_ptr =头;
        PTR =头;
        而(ptr->!链接= NULL){
            prev_ptr = PTR;
            PTR = ptr->链接;
        }
        prev_ptr->链接= NULL;
        免费(PTR);
    }
}

您可能要考虑的特殊待遇,其中最终元素是头的情况下扩展该

The following code works fine for all the functions except the deleteEnd function. When i execute the program all the functions perform the tasks they are supposed to but the deleteEnd function does nothing. There is no change in the linked list on executing the deleteEnd function. Please Help!!!!

#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();
}

解决方案

The loop

while(ptr!=NULL){

runs until ptr is NULL, meaning that the later call free(ptr) has no effect.

You could fix this by exiting the loop one iteration sooner instead

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

You may want to consider extending this with special treatment for the case where the end element is head

这篇关于在C菜单驱动程序链表上执行各种操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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