My teacher ask to make a program that can:
Insert record in front
Insert record at the end
Insert in between
Search node record
Delete record in front
Delete record at the end
Delete record in between
Using Data structure Linked List type. But I'm really
confused about the codes and I can't go through. Please help
Thanks in advance. Also here is my unfinished code if
someone can make changes it will be more good.
Answer Posted / domz tubalado
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void dugay(){
int x;
printf("\nLoading...");
for(x=0;x<1000;x++)
{
delay(100);
if(x%100==0)
printf(".");
}
}
/*STRUCTURE*/
struct Node{
int ID;
char name[10];
char surname[10];
char course[7];
struct Node *Link;
};
typedef struct Node *NodePointer;
/*INSERT NODE IN FRONT*/
void InsertInFront(NodePointer *Head, int iID, char iname[10]){
NodePointer NewNode;
NewNode=(NodePointer)malloc(sizeof(struct Node));
NewNode->ID=iID;
strcpy(NewNode->name,iname);
printf("ID: ");
scanf("%d",&NewNode->ID);
printf("Name: ");
scanf("%s",&iname);
printf("\n");
NewNode->Link=*Head;
*Head=NewNode;
}
/*DELETE FRONT*/
void DeleteFront(NodePointer *Head){
NodePointer Temp;
if(*Head==NULL)
printf("Link list empty...Cannot delete");
else{
Temp=*Head;
*Head=(*Head)->Link;
free(Temp);
}
}
/*SEARCHING*/
NodePointer Searching(NodePointer *Head,int Key)
{
NodePointer Here;
Here=*Head;
while (Here!=NULL)
if((Here->ID)==Key)
return (Here);
else
Here=Here->Link;
return(NULL);
}
/*INSERT END*/
void InsertAtEnd(NodePointer *Head,int Number)
{
NodePointer NewNode,Temp;
NewNode=(NodePointer)malloc(sizeof(struct Node));
NewNode->ID=Number;
printf("\nNew Node:%d",NewNode->ID);
NewNode->Link=NULL;
if(Head==NULL)
*Head=NewNode;
else
{
Temp=*Head;
while(Temp->Link!=NULL)
Temp=Temp->Link;
Temp->Link=NewNode;
}
}
/*DELETE END */
void DeleteEnd(NodePointer *Head)
{
NodePointer Before,After;
if(*Head==NULL)
printf("\nLink List is empty!...Cannot Delete");
else
{
Before=NULL;
After=*Head;
while (After->Link!=NULL)
{
Before=After;
After=After->Link;
}
if ((*Head)->Link==NULL)
Head=NULL;
else
Before->Link=NULL;
free(After);
}
}
/*INSERT BETWEEN*/
void InsertInBetween(NodePointer Before, NodePointer After,
int Number)
{
NodePointer NewNode;
NewNode=(NodePointer)malloc(sizeof(struct Node));
NewNode->Link=NULL;
NewNode->ID=Number;
NewNode->Link=After;
Before->Link=NewNode;
}
/*DELETE BETWEEN*/
void DeleteInBetween(NodePointer Discard, NodePointer Before)
{
(Before)->Link=(Discard)->Link;
free(Discard);
}
/*DISPLAY NODES*/
void display(NodePointer *Head)
{
NodePointer current;
int row=3;
clrscr();
gotoxy(35,1);printf("LINK LIST");
printf("\n MENU");
printf("\n1.Insert record in front.");
printf("\n2.Insert record at the end.");
printf("\n3.Insert in between.");
printf("\n4.Search node record.");
printf("\n5.Delete record in front.");
printf("\n6.Delete record at the end.");
printf("\n7.Delete record in between.");
current=*Head;
gotoxy(52,2);printf("ID NUMBERS");
while(current!=NULL)
{
gotoxy(48,row);printf("Registered ID:%d",current->ID);
current=current->Link;
row++;
}
}
/*MAIN PROGRAM*/
void main(){
NodePointer Head=NULL,current;
NodePointer After,Before,Discard;
int x=0,node=0,data;
char y,select,zname;
do{
display(&Head);
gotoxy(1,10);
printf("\n\nSELECT MENU");
fflush(stdin);
scanf("%c",&select);
switch(select){
case '1':printf("No. of nodes to be inserted in front:");
fflush(stdin);
scanf("%d",&node);
for(x=1;x<=node;x++)
InsertInFront(&Head,x,zname);
dugay();
display(&Head);
break;
case '2':printf("No of nodes to be inserted at end:");
fflush(stdin);
scanf("%d",&node);
for(x=1;x<=node;x++)
InsertAtEnd(&Head,x);
dugay();
display(&Head);
break;
case '3':printf("Node Before:");
scanf("%d",&node);
Before=Searching(&Head,node);
printf("\nNode After:");
scanf("%d",&node);
After=Searching(&Head,node);
printf("\nNumber data to be inserted:");
scanf("%d",&data);
InsertInBetween(Before,After,data);
dugay();
display(&Head);
break;
case '4':printf("Input data to be searched:");
fflush(stdin);
scanf("%d",&node);
current=Searching(&Head,node);
printf("\nFound data:%d",current->ID);
dugay();
display(&Head);
break;
case '5':printf("No. of nodes to be deleted in front of
the head:");
fflush(stdin);
scanf("%d",&node);
for(x=1;x<=node;x++)
DeleteFront(&Head);
dugay();
display(&Head);
break;
case '6':printf("No. of nodes to be deleted at the end:");
fflush(stdin);
scanf("%d",&node);
for(x=1;x<=node;x++)
DeleteEnd(&Head);
dugay();
display(&Head);
break;
case '7':printf("Node no. before the node no. to be
deleted:");
printf("%d",&node);
Before=Searching(&Head,node);
printf("\nInput node to be deleted:");
scanf("%d",&node);
Discard=Searching(&Head,node);
DeleteInBetween(Discard,Before);
dugay();
display(&Head);
break;
default:
printf("Invalid Selection...");
}
gotoxy(28,20);printf("Want to continue?[y/n]:");
fflush(stdin);
scanf("%c",&y);
}while(toupper(y)!='N');
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
How can you increase the size of a dynamically allocated array?
What are the different types of endless loops?
Write a program to input the price of 1 burger and the number of burgers eaten by a group of friends .print the total amount to be paid by the group?
What is the argument of a function in c?
When can you use a pointer with a function?
what are the advantages of a macro over a function?
Explain how do you determine a file’s attributes?
What is pre-emptive data structure and explain it with example?
What is an auto variable in c?
Explain what is the difference between #include and #include 'file' ?
How to write a program for machine which is connected with server for that server automatically wants to catch the time for user of that machine?
What is the difference between array and structure in c?
I have seen function declarations that look like this
Why doesnt long int work?
In c programming, explain how do you insert quote characters (? And ?) Into the output screen?