菜单驱动程序首先创建 'n' 个节点的单链表,然后显示创建的节点并进一步执行其他操作
Menu driven program to first create singly linked list of 'n' no of nodes and then display that created node and further do other operations
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node{
int data;
struct node *link;
}*head = NULL, *new_node, *ptr=NULL, *prev_ptr, *temp;
我想要一个函数来首先创建 'n' 个节点,以便进行进一步的操作。
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);
}
此外,如果我们可以要求用户先创建节点并向他展示创建的节点,那么该程序将更加用户友好。
然后在要求用户执行下面提到的任何操作之后。
int main(){
int choice,ch='y';
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();
return 0;
}
最后显示对最初创建的列表进行操作后创建的新列表。
我从问题和评论部分了解到,您希望在对链表进行进一步操作之前添加 n 个节点。我的回答基于这个假设。
为此,您将 main 修改如下:
int main(){
int choice, ch, n, i;
printf("Enter the number nodes you want to insert");
if(scanf("%d", &n) < 1){
printf("scanf failed while reading n\n");
}
for(i = 0; i < n; i++){
insertBeg();
}
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 1 to continue or any other key to exit : ");
if(scanf("%d",&ch) < 1){
printf("scanf failed while taking continue or not option\n");
exit(1);
}
if(ch == 1){
goto label;
}
return 0;
}
注意:如果这不是您要问的问题,请纠正我。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node{
int data;
struct node *link;
}*head = NULL, *new_node, *ptr=NULL, *prev_ptr, *temp;
我想要一个函数来首先创建 'n' 个节点,以便进行进一步的操作。
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);
}
此外,如果我们可以要求用户先创建节点并向他展示创建的节点,那么该程序将更加用户友好。 然后在要求用户执行下面提到的任何操作之后。
int main(){
int choice,ch='y';
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();
return 0;
}
最后显示对最初创建的列表进行操作后创建的新列表。
我从问题和评论部分了解到,您希望在对链表进行进一步操作之前添加 n 个节点。我的回答基于这个假设。
为此,您将 main 修改如下:
int main(){
int choice, ch, n, i;
printf("Enter the number nodes you want to insert");
if(scanf("%d", &n) < 1){
printf("scanf failed while reading n\n");
}
for(i = 0; i < n; i++){
insertBeg();
}
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 1 to continue or any other key to exit : ");
if(scanf("%d",&ch) < 1){
printf("scanf failed while taking continue or not option\n");
exit(1);
}
if(ch == 1){
goto label;
}
return 0;
}
注意:如果这不是您要问的问题,请纠正我。