c语言中的菜单驱动程序,用于对链表执行各种操作
以下代码适用于除deleteEnd函数之外的所有函数。当我执行程序时,所有函数都执行它们应该执行的任务,但是deleteEnd函数什么也不做。在执行deleteEnd函数时,链表中没有任何更改。请帮忙c语言中的菜单驱动程序,用于对链表执行各种操作,c,function,linked-list,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
#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();
}
#包括
#包括
结构节点{
int数据;
结构节点*链接;
}*head=NULL,*新节点,*ptr=NULL,*上一个ptr,*临时;
void insertBeg(){
国际信息;
新节点=(结构节点*)malloc(sizeof(结构节点));
printf(“\n输入数据:”);
scanf(“%d”、&info);
新建_节点->数据=信息;
新建节点->链接=NULL;
if(head==NULL){
head=新的_节点;
}
否则{
新建节点->链接=头部;
head=新的_节点;
}
}
void insertEnd(){
国际信息;
新节点=(结构节点*)malloc(sizeof(结构节点));
printf(“\n输入数据:”);
scanf(“%d”、&info);
新建_节点->数据=信息;
if(head==NULL){
head=新的_节点;
新建节点->链接=NULL;
}
否则{
prev_ptr=水头;
ptr=头部->链接;
while(ptr!=NULL){
prev_ptr=ptr;
ptr=ptr->link;
}
prev_ptr->link=新建_节点;
新建节点->链接=NULL;
}
}
void displayNode(){
printf(“\n链接列表为:”);
ptr=头部;
while(ptr!=NULL){
printf(“%d-->”,ptr->data);
ptr=ptr->link;
}
}
void deletebege(){
if(head==NULL){
printf(“\nUnderflow”);
}
否则{
温度=水头;
头=头->链接;
免费(临时);
}
}
void deleteEnd(){
if(head==NULL){
printf(“\nUnderflow”);
}
否则{
prev_ptr=水头;
ptr=头部->链接;
while(ptr!=NULL){
prev_ptr=ptr;
ptr=ptr->link;
}
prev_ptr->link=NULL;
免费(ptr);
}
}
无效遍历(){
整数计数=0;
ptr=头部;
while(ptr!=NULL){
ptr=ptr->link;
计数++;
}
printf(“\n列表中的元素数为:%d”,计数);
}
void main(){
int-choice,ch='y';
clrsc();
标签:
printf(“\n按1在beg处插入\n按2在末尾插入”);
printf(“\n3从beg中删除\n4从end中删除”);
printf(“\n5显示列表\n6遍历链接列表:”;
scanf(“%d”,选择(&C);
开关(选择){
案例1:insertBeg();
打破
案例2:insertEnd();
打破
案例3:deleteBeg();
打破
案例4:deleteEnd();
打破
案例5:displayNode();
打破
案例6:导线();
打破
默认值:printf(“\n无效选项”);
}
printf(“\n按y键继续,或按任何其他键退出:”;
scanf(“%s”&ch);
if(ch=='y'| ch=='y'){
后藤标签;
}
getch();
}
循环
while(ptr!=NULL){
运行直到ptr
为NULL
,这意味着后面的调用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);
}
}
您可能想考虑在结束元素为“代码>标题
的情况下,将其扩展为特殊处理。请正确地缩进代码。几行空白也不错。