c program day-15
TRANSCRIPT
-
7/31/2019 C Program Day-15
1/15
-
7/31/2019 C Program Day-15
2/15
Explain link list
Explain linked list declaration
Traverse a linked list
Types of linked list
Insert nodes into a link list
Explain Data Structures
-
7/31/2019 C Program Day-15
3/15
A data structure is a group of data elements grouped together under one
name. These data elements, known as members, can have different types and different
lengths.
syntax
struct structure_name {member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
} Stru_varname;
Where structure_name is a name for the structure type, stru_name can be a
set of valid identifiers for objects that have the type of this structure. Within braces { }
there is a list with the data members, each one is specified with a type and a valid
identifier as its name.
-
7/31/2019 C Program Day-15
4/15
// program to Accept the employee details from the End user
#include
#include
#include
struct emp
{
int eno;
char ename[25];
int sal;};
void main()
{
struct emp *e;
char ch='y';
clrscr();
while(ch=='y')
-
7/31/2019 C Program Day-15
5/15
{
e=(struct emp *) malloc(sizeof(struct emp));
printf("\n Enter the employee number");
scanf("%d",&e->eno);
printf("\n Enter the employee name");
scanf("%s",e->ename);
printf("\n Enter the employee salary");
scanf("%d",&e->sal);
printf("\n Do you wish to continue[Y/N]?");
fflush(stdin);scanf("%c",&ch);
}
getch();
}
-
7/31/2019 C Program Day-15
6/15
LINKED LIST :
It is a dynamic data structure. This implementation uses pointers. Each element of the list is
called as node. Each element points to the next element. In linked list, a node can be inserted
or deleted at any position.
Basic Terms :
(i) Node (ii) Null Pointer (iii) External pointer
(iv) Empty List
Operation on Linked list :
(i) Creation (ii) Traversing
(iii) Insertion
(a) Insertion at beginning
(b) Insertion at end
(c) Insertion at any position
(iv) Searching
(v) Deletion
(a) Deletion at beginning
(b) Deletion at end
(c) Deletion at any position
-
7/31/2019 C Program Day-15
7/15
Linked List Declaration
A linked list storing information of players of a cricket teamcan be declared as :
Note That next is declared
as a pointer to the structureplayer itself.
Remember, that the last
node in the list should have
its pointer, next, set to the
value NULL.
-
7/31/2019 C Program Day-15
8/15
Diagrammatically
-
7/31/2019 C Program Day-15
9/15
// program to Add New Records in Single linked list using First to// Last method simply defined as to form the new Datastructure#include#include#include
void accept();struct emp{int eno;char ename[25];int sal;struct emp *next;
};// start - Ist Record, New1 - Current record, Temp - prevoius recordstruct emp *start=NULL,*new1=NULL,*temp=NULL,*new2,*dptr;void main(){char ch='y';
int no,recno;clrscr();while(ch=='y'){new1=(struct emp *) malloc(sizeof(struct emp));printf("\n Enter the employee number");scanf("%d",&new1->eno);
Continued,
-
7/31/2019 C Program Day-15
10/15
printf("\n Enter the employee name");
scanf("%s",new1->ename);
printf("\n Enter the employee salary");
scanf("%d",&new1->sal);
if (start==NULL) // Give Ist record as Input,to enter the following if true
{
start=new1; //current record is assigned to start
start->next=NULL; //current record next is set to NULL.because it is a
// first record
temp=new1; //current record is assigned to temp(point previous record)
}
else
{
temp->next=new1; // new1 have a current record &assigned to prevoius
next
// have a NULL value changed to addresstemp=new1; //current i/p record is assigned to temp ex. 1st record to II
temp->next=NULL;
}
printf("\n Do you wish to continue[Y/N]?");
fflush(stdin);
scanf("%c",&ch);} Continued,
-
7/31/2019 C Program Day-15
11/15
-
7/31/2019 C Program Day-15
12/15
else if (recno==new2->eno && new2->next !=NULL) {
temp->next=new1;
new1->next=new2;
}
// to check if it is last record &also to insert that place.
else if (recno==new2->eno && new2->next==NULL)
{
new2->next=new1;
new1->next=NULL;}
temp=new2;
}
// display all records
for(new2=start;new2!=NULL; new2=new2->next) // new2 initialize Ist record
{
printf("\n %d %s %d",new2->eno,new2->ename,new2->sal);
}
Continued,
-
7/31/2019 C Program Day-15
13/15
//Deletion process
printf("\n Enter the record number to delete");
scanf("%d",&recno);
for(new2=start;new2!=NULL;new2=new2->next)
{
if(recno==start->eno){
start=start->next;
}
else if (recno==new2->eno && new2->next !=NULL)
{
temp->next=new2->next;}
else if(recno==new2->eno && new2->next==NULL)
{temp->next=NULL;}
temp=new2;
}
//display all the records
for(new2=start;new2!=NULL; new2=new2->next) // new2 initialize Ist record
{
printf("\n %d %s %d",new2->eno,new2->ename,new2->sal);
}
Continued,
-
7/31/2019 C Program Day-15
14/15
// Modify process
printf("\n Enter the employee number to be modified");
scanf("%d",&no);
for(new1=start;new1!=NULL; new1=new1->next) //new1 init.Ist record & search
{
if (no==new1->eno)
{
printf("\n Enter the Empno,Empname and salary");
scanf("\n %d %s %d",&new1->eno,new1->ename,&new1->sal);
}
}
//display all the records
for(new1=start;new1!=NULL; new1=new1->next) // new1 initialize Ist record
{
printf("\n %d %s %d",new1->eno,new1->ename,new1->sal);
}
//Memory deallocating process
new1=start;
Continued,
-
7/31/2019 C Program Day-15
15/15
while(1){
if(new1==NULL)
break;
dptr=new1;
new1=new1->next;
free(dptr);}
dptr=NULL;
new1=NULL;
temp=NULL;
start=NULL;
}void accept()
{
new1=(struct emp *) malloc(sizeof(struct emp));
printf("\n Enter the employee number");
scanf("%d",&new1->eno);
printf("\n Enter the employee name");
scanf("%s",new1->ename);
printf("\n Enter the employee salary");
scanf("%d",&new1->sal);}}