0% found this document useful (0 votes)
29 views19 pages

Self Refrential Structure

Uploaded by

apc78044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
29 views19 pages

Self Refrential Structure

Uploaded by

apc78044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 19
[ADVERTISEMENT Self-referential structure The self-referential structure is a structure that points to the same type of structure. It contains one or more pointers that ultimately point to the same structure. ADVERTISEMENT ADVERTISEMENT Amazon microsoft office - Low Software Online © Structures are a user-defined data structure type in C and C++ ©. The main benefit of creating structure is that it can hold the different predefined data types. © It is initialized with the struct keyword and the structure's name followed by this struct keyword. © We can easily take the different data types like integer value, float, char, and others within the same structure, © It reduces the complexity of the program. © Using a single structure can hold the values and data set in a single place, © Inthe C++ programming language, placing struct keywords is optional or not mandatory, but it is mandatory in the C programming language. © Self-referential structure plays a very importar’ * we Linked list © In this type of structure, the object of the same s refers to the data types of the same structure. © It can have one or more pointers pointing to the © The self-referential structure is widely used in ¢ lists, etc. Self Referential Structures struct node { int data‘; char data2; struct node* link; Why do we require a Self-referential structure? Self-referential structure plays a vital role in the linked list, trees, graphs, and many more data structures, By using the structure, we can easily implement these data structures efficiently. For defining a particular node, the structure plays a very important role. In a linked list, the structure is used to store the data information and the address of the next node in the given linked list. Mainly the data part is of integer type, and the link address part is of pointer type, which holds the address of the next node, so that it can for the Link ultimately. As we all know, the role of a linked list is used to store the data elements of the same type, in which address locations are not at the consecutive difference. In this linear linked list, the current node holds the address of the next node so that it ultimately forms the Link between the current node and the next node. Here in the linked list, we will define a common structure, which contains the data and address pointer to carry the address of the next node, so to form the multiple nodes, we from each node using the node structure. ADVERTISEMENT Unlike a static data structure such as an array where the size of the array limits the number of elements that can easily to inserted into the array, a self-referential structure can dynamically be expanded or contracted Operations like the insertion or deletion of nodes in a self-referential structure involve straightforward alteration of pointers. Linked list © A linked list is a useful data storage method, and it is very easy to implement it in C programming Language. © Several kinds of linked lists, including single linked lists, double linked lists, and binary trees. © Each type is suited for certain types of data storage. © The one thing that these lists have in common is that the links between data items are defined by the information contained in the items themselves, in the form of pointers. © The linked list is distinctly different from arrays, in which the links between data items result from the layout and storage of the array. Here, we will discuss the self-referential structure in more detail using the linked list concepts. Let's understand the concept of self-referential structure in more detail using the example mentioned below: Example 1: Creating a random self-referential structure in the C programming language. © // Dummy Self-referential structure in C language struct node // Creating an // part of integer type and a pointer of structure type // hold the address of the next node { # Creating a object of node type, this object is # consists of three parts one is data 1 and # second is of data 2 and third is of link type pointer # carries the address of the next node Types of self-referential structure 1, Single Link self-referential structure 2. Multiple Link self-referential structures Single Link self-referential structure As the name suggests, we can easily predict that these structures consist of a single Link, mainly the only one pointer link of structure type pointing to the same structure. To better understand it, we can connect this concept with the singly linked list. Ina singly linked list, there is only a single pointer that carries the address of the next node, and that pointer variable is of the structure node type itself. It is mainly used when we want to store the different data items by fetching out them from various addresses and connecting all of them by storing the address of one data item into the linked part of the other node. In this way, we can easily connect all the data items by using these connecting links. Let's look at the working of single link self-referential structure with the help of an example: // Single link self-referential structure implementation in C language include include struct node { int info ; struct node “link ; // Self-referential structure link is pointing the same // structure of the type node y ® struct node *START = NULL; //start pointer to cont struct node* createnode() ; void insertatlast0 ; //insert node at last // void deleteatfirst; _—_// delete node at first // void viewlist( ; void insertattirst0 ; int getlength0 ; int menu( ; void insertafteranynode0 ; void deleteatlast0 ; void deleteatposition9 ; struct node* createnode() // create node dynamically // { struct node *n ; n= malloc( sizeof(struct node) ) ; return (n); } void insertatlast) { struct node *temp *t; temp = createnode() ; printf( “Enter the data in node \n "); scanf(" %d" , &itemp->info ) ; temp->link = NULL; if (START == NULL) START = temp ; else { t= START; while ( t->link != NULL) { t= tolink; ) t->link = temp ; } void deleteatfirst0) // delete node at first // { printf (“List is empty \n"); { struct node *q; = START; START = START->link ; free(q); ) void viewlist0 {_ struct node* t; if (START { printf (“No List \n"); ) else { t= START; while (t!= NULL) { printf (" %d" t=tolink; ULL) t->info); ) void insertattirst { struct node*New ; New = createnode0 ; printf ( “Enter the data in node \n"); scanf (" %d", &New-> info ) ; if (START == NULL) START = New; else { New->link = START ; START = New; } ) int getlength0 { int count = 0; struct node* t; NULL) printf ("List is empty \n"); else { t =START; while (t!= NULL) { count = count +1; t= t-olink; } return count; ) void insertafteranynode) { int position ; struct node* newnode ,*t if (START == NULL) printf ( “List is empty \n "); else { printf ("Enter position after which you want to add: \n "); scanf (" %d" , &.position ) ; if (position > getlengthd ) { print ( "Wrong position \n"); insertafteranynodeQ ; —// recursion // d { inti= newnode = createnode() ; printf ( "Enter Data \n"); scanf (“ %d", &newnode-> info ) ; newnode->link = NULL; if (START == NULL) START = newnode ; else { t= START ; while (i < position ) ‘ t=tlink; itt; } newnode->link = t->link ; t->link = newnode ; ) void deleteatlast0 { struct node* t, *q; if (START == NULL) { printf (“List is empty \n"); ) else { t= START; = START; while ( t->link |= NULL) { qzt t= tolink; } if (t == START) { START == NULL; ) { g-link = NULL; free(t); ) ) void deleteatposition) { struct node *t, int position , t= START; if (START { printf ( "List is empty \n "); } else { printf ( “Enter position after which you want to delete: \n" ); scanf (" %d", &position ); NULL) if (position > getlength0 ) { printf ("Wrong position \n "); deleteatposition( ; // recursion // ) else { while (i < position ) ‘ q=t; t= telink; ise; ) if (t == START) { START == NULL; ) else { q->link = t-> link ; free(t); ) int menu) { int ch; printf (" \t\t\t LADD NODE LAST IN LINK \n"); printf (“\t \t \t 2ADD NODE AT FIRST IN LINK \n "); printf (" \t \t \t 3.VIEW LIST IN LINK \n "); printf (" \t \t \t 4.DELETE NODE AT FIRST IN LINK \n ") ; printf("\t\t\t 5. TO SEE THE LENGTH OF LIST \n"); printf (" \t\t\t 6. INSERT NODE IN BETWEEN \n "); printf (“ \t \t \t 7-.DELETE NODE AT LAST IN LINK \n"); printf (" \t \t \t 8.DELETE NODE AT SPECIFIC POSITION IN LINK \n ") ; printf ("\t\t\t 9.EXIT \n"); printf ("ENTER THE CHOICE \n"); scanf ("%d", &ch) ; return( ch); ) void main { intk; while (1) { switch (menu() ) { case 1: insertatlast0 ; break ; case 2: insertatfirst() ; break ; case 3: viewlist0 ; break ; case 4: deleteattirst0 ; break; case 5: k = getlength0 ; printf (“THE LENGTH OF THE LIST IS %d \n", k); break; case 6 insertafteranynode0 ; break; case7 deleteatlast0 ; break; case 8: deleteatposition( ; break; case 9: exit( 0); break; default : printf (" Not available \n "); The output of the above program is: ‘ADD NODE LAST IN LINK Pa eC cme Clg VEEW LIST IN LINK Coa aac mec rs LENGTH OF LI! NODE IN BETWEEN arcs Cs aac baat s ENTER THE CHOICE ‘i eos UmCT TS 14 Poa eee une Pra eee Gm Une ld mec mea Cea eens caer eo es este iat) DELETE NODE AT LAST TN LINK DELETE NODE AT SPECIFIC POSITION eal Pusan ec A Sct ume fy Pe aoe et Pra eee Gm ne cld iST IN LINK Cea ea ones see one ec eset Saar seo Resear oneal pas eon I Ayala ya eC me Tg ayia eee sta ap One a ie ue eae ced 3 ees Pe ae meg food ear ala cme OMe C Ta ecm cmecTg Pata aces To SEE THE LENGTH OF LIST pismo see na) arog are salsa ole Ome ee rrr aerate rsd Poa nec ees oe ore Weems omecmoMeng anaes te a ees # Single link self-referential structure implementation in Python language class node: def _init_(selt) self.d1=0 selfd2=0 selflink=None if __name_ == '_main_’ obj1=node) # Nodet # Initialization obji.link = None objt.d1 = 17 objt.d2 = 14 obj2=node() # Node2 # Initialization obj2.link = None obj2.d1 = 36 obja.d2 = 24 # Linking obj1 and obj2 obj.link = obj2 # Accessing data members of obj2 using obj1 print(obj1.link.d1) print(obj1.link.d2) The output of the above program is: 36 24 Multiple Link self-referential structures As the name suggests, we can easily predict that these types of the structure consist of multiple Link, here we will make use of two pointer links of structure type which is pointing to the same structure, to understand it better, we can connect this concept with a doubly-linked list. In a doubly-linked list, two single pointers carry the address of the next node and the previous node, and that pointer variable is of the structure node type itself. It is mainly used when we want to store the different data items by fetching out them from various addresses and connecting all of them by storing the address of one data item into the linked part of the other node; in this way, we can easily connect all the data items by using these connecting links [AOVERTISEMENT [ADVERTISEMENT Let us look at the working of, multiple link self-referential structure with the help of an example: // Multiple link self-referential structure implementation in C language #include #include struct node { int info ; struct node “prev; // Self-referential structure link is pointing the same // structure of the type node but to the previous node © struct node *next; // Self-referential struc // structure of the type node but to the next node ye struct node * START = NULL; // Start pointer of the list and initially it must be null, // represet void insertnodeatfirst() ; void deleteattirstQ ; void viewlist0 ; int menud ; void insertnodeatfirst) // Inserting the node at first { struct node * newnode ; newnode = malloc( sizeof( struct node )) ; printf (“Enter Data: \n"); scanf ( " %d" , &inewnode->info ) ; newnode->prev = NULL; newnode->next = NULL; if ( START == NULL) START = newnode ; else { START-> prev = newnode ; newnode->next = START; START = newnode ; ) void deleteattirst() // deleting the first node ( struct node * temp; if ( STAR ULL) printf ( “List is empty \n"); else { temp = START; START = START->next ; START-> prev = NULL; ® free( temp ) ; ) } void viewlist( // displaying all the { struct node *t; if (START == NULL) printf (“List is empty \n"); else { t= START; while (t != NULL) ( printf ("9d \n"", info); t=tonext; ) ) ) int menu) { intn; printf (" \t\t\t 1. VIEW LIST \n ") ; printf (“\t\t \t 2. INSERT AT FIRST IN LIST \n"); printf ("\t\t \t 3. DELETE AT FIRST IN LIST \n "); printf (" \t\t\t 4. EXIT\n"); printf ("ENTER YOUR CHOICE \n"); scanf (" %d", &n); return (n); ) int main0 { while (1) { switch ( menu( ) { case 1: viewlist0 ; break; case 2: insertnodeatfirst0 ; break ; case 3: deleteattirst0 ; break; case 4: exit (1); printf ( ) The output of the above program is: mE Brn Peat ans exr ET a Col Menoted Bese sy creat Peas EXIT areca Peat eas Taos charac ana Coe EXIT Sen Marta Meee Peo Piacoa og Teo 4 node: def _init_(self) selfdata = 0 selfprev = None selfinext = None if _name_ == ‘_main_’ obj1=noded) obj1.prev = None ‘obj1.next = None objt.data = 15 obj2=noded obj2 prev = None obj2next = None obj2.data = 20 obj3 = node() obj3.prev = None obj3.next = None obj3.data = 25 objt.next = obj2 obj2next = obj3 obj2 prev = obj1 obj3.prev = obj2 print(obj1.data , end='\t) print(objt.nextdata , end="\t’) print(obj1.next.next.data) #Node1 # Initialization # Node2 # Initialization # Node3 # Initialization # Forward links # Backward links # Accessi # Accessir

You might also like