Write a program in C to manipulate linked lists. The input to the program will be a sequence of instructions of the following three categories:
The list should always be maintained in sorted order in increasing order of age. If there is more than one person of same age, the order should be as in the input. You may assume that the input is valid and correct. While executing remove or print, if the list does not have enough elements, the command should be
ignored.
You may assume that the name is always a single word with no spaces within. The age will be a positive integer. Terminate each line output with a newline character.
- insert name age: create a node with item as the info field and insert into the current list.
- remove N : remove the Nth item from the list.
- print N : print the Nth item from the list. The name should be printed first and then the age with a space in between.
- stop : end of instructions
The list should always be maintained in sorted order in increasing order of age. If there is more than one person of same age, the order should be as in the input. You may assume that the input is valid and correct. While executing remove or print, if the list does not have enough elements, the command should be
ignored.
You may assume that the name is always a single word with no spaces within. The age will be a positive integer. Terminate each line output with a newline character.
Logic:
Available soon .....
Solution:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct node{ char name[35]; int age; struct node *next; } *str_ptr,*new_ptr,*temp_ptr; //give previous address by value struct node *get_adrs(int x); int main() { struct node *temp_2; int i=0,j; char option[15],nam[35]; while(1) { scanf("%s",option); if(strcmp(option,"insert")==0) option[0]='i'; else if(strcmp(option,"remove")==0) option[0]='r'; else if(strcmp(option,"print")==0) option[0]='p'; else if(strcmp(option,"stop")==0) option[0]='s'; else option[0]='w'; if(option[0]=='i') //Add an ite { //printf("Insert"); new_ptr=(struct node *)malloc(sizeof(struct node)); scanf("%s %d",new_ptr->name,&(new_ptr->age)); if(str_ptr==NULL) //First node { //printf("First node"); str_ptr=new_ptr; str_ptr->next=NULL; } else //Not a first node { if(str_ptr->age > new_ptr->age) //add before first. { new_ptr->next=str_ptr; str_ptr=new_ptr; } else //More than one nodes are available. { temp_ptr=get_adrs((new_ptr->age)); if(temp_ptr->next==NULL) //Insert at last { temp_ptr->next=new_ptr; new_ptr->next=NULL; } else //insert after { temp_2=temp_ptr->next; temp_ptr->next=new_ptr; new_ptr->next=temp_2; } } } } else if(option[0]=='r') //remove an item { scanf("%d",&j); temp_ptr=str_ptr; if(j==1 && str_ptr!=NULL) //delete first { temp_ptr=str_ptr->next; str_ptr->next=NULL; free(str_ptr); str_ptr=temp_ptr; } else if(j>1) { for(i=2;(i<j && temp_ptr!=NULL);i++) temp_ptr=temp_ptr->next; //previous address is obtained if(temp_ptr==NULL || temp_ptr->next==NULL) ; else { temp_2=temp_ptr->next; temp_ptr->next=temp_2->next; free(temp_2); } } } else if(option[0]=='p') //print { scanf(" %d",&j); temp_ptr=str_ptr; for(i=1;(i<j && temp_ptr!=NULL);i++) temp_ptr=temp_ptr->next; //address is obtained if(temp_ptr==NULL) ; else printf("%s %d\n",temp_ptr->name,temp_ptr->age); } else if(option[0]=='s') //stop { break; } } return 0; }
//Finding the address of previous node struct node *get_adrs(int x) { struct node *prev; temp_ptr=str_ptr; prev=temp_ptr; while(temp_ptr!=NULL && temp_ptr->age <= x) { prev=temp_ptr; temp_ptr=temp_ptr->next; } return(prev); }
No comments:
Post a Comment