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 / 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 |
different between overloading and overriding
write function to reverse char array ... without using second array
Find the highest of three numbers and print them using ascending orders?
main() { int i = 10; printf(" %d %d %d ", ++i, i++, ++i); }
What is a lvalue
Why do u use # before include in a C Progam?
WAP – represent a char in binary format
wt is diference between int and int pointer as same as float and float pointer and char and char pointer
write a c programming using command line argument,demonstrate set operation(eg;union,intersection,difference) example output is c:>setop 12 34 45 1 union 34 42 66 c:>setop 12 34 1 42 66 c:>setop 12 34 diff 12 56 67 78 setop 12 34
What is meant by gets in c?
How are 16- and 32-bit numbers stored?
int a[3][5]={ {1,2,3,4,5],{2,3,4,5,6},{10,11,12,13,14}}; int *p=&a; printf(ā%dā,*(*(x+1)+3));