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.



My teacher ask to make a program that can: Insert record in front Insert record at the end Inse..

Answer / 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

More C Interview Questions

simple c program for 12345 convert 54321 with out using string

7 Answers   TCS,


What are the various types of control structures in programming?

0 Answers  


Explain how do you determine the length of a string value that was stored in a variable?

0 Answers  


#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }

1 Answers  


How does the C program handle segmentation faults?

2 Answers  


Subtract Two Number Without Using Subtraction Operator

0 Answers  


i want to make a program in which we use input having four digits(4321) and get output its reciprocal(1234).

1 Answers  


How can I find the day of the week given the date?

0 Answers  


44.what is the difference between strcpy() and memcpy() function? 45.what is output of the following statetment? 46.Printf(“%x”, -1<<4); ? 47.will the program compile? int i; scanf(“%d”,i); printf(“%d”,i); 48.write a string copy function routine? 49.swap two integer variables without using a third temporary variable? 50.how do you redirect stdout value from a program to a file? 51.write a program that finds the factorial of a number using recursion?

6 Answers   Amdocs,


explain memory layout of a C program

2 Answers  


What is the output of below code? main() { static int a=5; printf("%3d",a--); if(a) main(); }

1 Answers  


why do we use # in c-language?

1 Answers  


Categories