Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
29 views
19 pages
Self Refrential Structure
Uploaded by
apc78044
AI-enhanced title
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
Download
Save
Save self refrential structure For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
29 views
19 pages
Self Refrential Structure
Uploaded by
apc78044
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save self refrential structure For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save self refrential structure For Later
You are on page 1
/ 19
Search
Fullscreen
[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 24Multiple 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 yestruct 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 = Noneselfinext = 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
Data Structures - Module 4
PDF
No ratings yet
Data Structures - Module 4
81 pages
Linked List
PDF
No ratings yet
Linked List
81 pages
Self-Referential Structures and Linked List
PDF
No ratings yet
Self-Referential Structures and Linked List
64 pages
CS23231 DS Unit I Reg - 2023
PDF
No ratings yet
CS23231 DS Unit I Reg - 2023
180 pages
Data Structure - Ppt-Notes
PDF
No ratings yet
Data Structure - Ppt-Notes
209 pages
Linked List
PDF
No ratings yet
Linked List
90 pages
DSA2
PDF
No ratings yet
DSA2
150 pages
Module 2
PDF
No ratings yet
Module 2
111 pages
Linkedlist 2
PDF
No ratings yet
Linkedlist 2
113 pages
Linked List
PDF
No ratings yet
Linked List
104 pages
Module 2 DSA T4 List SLL DLL CLL
PDF
No ratings yet
Module 2 DSA T4 List SLL DLL CLL
92 pages
Unit 1
PDF
No ratings yet
Unit 1
86 pages
Ds 3
PDF
No ratings yet
Ds 3
68 pages
Singly and Doubly Linked List
PDF
No ratings yet
Singly and Doubly Linked List
72 pages
Linked List
PDF
No ratings yet
Linked List
55 pages
Linkedlist 1
PDF
No ratings yet
Linkedlist 1
80 pages
Linked Lists
PDF
No ratings yet
Linked Lists
29 pages
Lecture 5 - Linked List
PDF
No ratings yet
Lecture 5 - Linked List
58 pages
DS Unit 2
PDF
No ratings yet
DS Unit 2
62 pages
10 Linked Lists
PDF
No ratings yet
10 Linked Lists
96 pages
Dsa File Sem 3
PDF
No ratings yet
Dsa File Sem 3
37 pages
Finallinked Listppt
PDF
No ratings yet
Finallinked Listppt
69 pages
UNITIIIofDSpdf 2024 11 23 23 52 01
PDF
No ratings yet
UNITIIIofDSpdf 2024 11 23 23 52 01
52 pages
Module 4 DSA
PDF
No ratings yet
Module 4 DSA
62 pages
Module 4
PDF
No ratings yet
Module 4
62 pages
2 - DSA-Linked List (PART-1)
PDF
No ratings yet
2 - DSA-Linked List (PART-1)
47 pages
Spring 2013: CSE2421 Systems1
PDF
No ratings yet
Spring 2013: CSE2421 Systems1
63 pages
Chapter 3 Part One Linked List
PDF
No ratings yet
Chapter 3 Part One Linked List
35 pages
CH6 - Linked List
PDF
No ratings yet
CH6 - Linked List
67 pages
Linked List
PDF
No ratings yet
Linked List
36 pages
Self Referential Structure
PDF
100% (1)
Self Referential Structure
2 pages
Singly Linked List Union Command Line Arg
PDF
No ratings yet
Singly Linked List Union Command Line Arg
20 pages
Chapter 4. Linked List - Notes
PDF
No ratings yet
Chapter 4. Linked List - Notes
25 pages
Structures and Union
PDF
No ratings yet
Structures and Union
33 pages
Unit 2
PDF
No ratings yet
Unit 2
55 pages
Unit2 - LinkedList - DST
PDF
No ratings yet
Unit2 - LinkedList - DST
32 pages
DSA Chapter 3 Part-One
PDF
No ratings yet
DSA Chapter 3 Part-One
10 pages
Data Structures
PDF
No ratings yet
Data Structures
10 pages
Linked List
PDF
No ratings yet
Linked List
13 pages
Linked List - Part I
PDF
No ratings yet
Linked List - Part I
30 pages
Module 3
PDF
No ratings yet
Module 3
14 pages
DS Unit1 LinkedList
PDF
No ratings yet
DS Unit1 LinkedList
39 pages
Unit 5 Adv C
PDF
No ratings yet
Unit 5 Adv C
24 pages
Linked List
PDF
No ratings yet
Linked List
41 pages
Applications of Artificial Intelligence
PDF
No ratings yet
Applications of Artificial Intelligence
8 pages
Singly Linked List in C Programming Language
PDF
No ratings yet
Singly Linked List in C Programming Language
5 pages
Chapter - 10: Self Referential Structures and Linked Lists
PDF
No ratings yet
Chapter - 10: Self Referential Structures and Linked Lists
5 pages
Pointer Lo2
PDF
No ratings yet
Pointer Lo2
3 pages
Linked List
PDF
No ratings yet
Linked List
24 pages
Linked List Linked List Linked List Linked List
PDF
No ratings yet
Linked List Linked List Linked List Linked List
39 pages
DSPD Unit - III Notes PDF
PDF
No ratings yet
DSPD Unit - III Notes PDF
12 pages
02 Linked List
PDF
No ratings yet
02 Linked List
12 pages
Single Lin List
PDF
No ratings yet
Single Lin List
35 pages
Self-Referential Structures and Linked List: Programming and Data Structure 1
PDF
No ratings yet
Self-Referential Structures and Linked List: Programming and Data Structure 1
21 pages
C Programming Basic - Week 3
PDF
No ratings yet
C Programming Basic - Week 3
12 pages
Data Structure With C - Part 2
PDF
No ratings yet
Data Structure With C - Part 2
6 pages
Data Structurescript
PDF
No ratings yet
Data Structurescript
3 pages
C LinkedList
PDF
No ratings yet
C LinkedList
10 pages