Docsity

Data Structure - Linked Lists - Notes, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structure - Linked Lists - Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Linked Lists

 List: A set of items organized sequentially

 Array can be used for it.Sequential

 organization is provided by the index.

 Index is used for accessing and manipulating

elements.

 Array implementation has no. of drawbacks

 Linked List: a completely different way to

represent a list.

 Each element of list is contained in a

structure.

 Another field of this structure is a link,giving

the address of another structure containing

the next element of the list.

Nod1.item Node1.next

Node2.item Node2.next

node

node

Node1 can be attached with node as Node1.next=&node

Node1.item Node1.next

node

Node2.item Node.next

node

Node2.next =^ Node1.item = Node2.item=

 (^) Circular list  (^) Given a pointer p to a node in a linear linked list we can not reach any of the nodes that precede node (p).  (^) So if the next field of last node is changed to point back to the first node rather than null ptr, such a list is called circular list.  (^) Now in it it is possible to reach any other point in the list.  (^) However see the difficulty to add a node in the beginning or at the end.

 This problem can be sorted out by

pointing this circular list at the last node

instead of the first one.

Basic List operations

 Creating a list

 Traversing a list

 Counting the items in the list

 Printing the list(or sublist)

 Looking up an item for editting or printing

 Inserting an item

 Deleting an item

 Concatenating two lists

Insertion in the linked list

 The process of insertion precedes a search for the place of

insertion,i.e locating a node after which the new item is to be

inserted.

 A general algorithm

 if the list is empty or the new node comes before the head node

then

 Insert the new node as an head node

 Else

 If the new node comes after last node then insert new node as

the last node

 Else

 Insert the new in the body of the list.

 (^) Cin>>N;   (^) If(N<=0)  {  Cout<<“\n error”;  Return();}  (^) First = new stud;  (^) Cout<<“\n enter data”;  (^) Cin>>first->name>>first->rollno;   (^) First->next = NULL;  Last =first;  For(I=1;I<n;I++)  {X= new stud;  (^) Cout<<“\n Entere data”;  (^) Cin>>x->name>>x->rollno;  (^) X->next = NULL;   (^) Last->next = x;  Last = x;  } }

 (^) For reversing the list  (^) Node reverse(list)  (^) {  (^) Nodeback, ahead, ptr;  (^) If(list==NULL)  (^) {  (^) cout<<“\n error”;  (^) return(list);  (^) }  (^) ptr = list;  (^) Back = list;

 (^) We can construct a class of linked list

 (^) Class link-list

 (^) {

 (^) Struct stud

 (^) { char name[15];

 (^) Int rollno;

 (^) Stud*next;

 (^) };

 (^) Stud first,last,x;

 (^) Int I,n;

 (^) Public:

 (^) Linked-list()

 (^) {first = last = x = NULL;}

 (^) Header lists are generally organized in the form

of circular lists. The advantage of header lists is that all link pointers contain valid addresses.  (^) Void create();

 (^) Void traverse();

 (^) };

Stack

It is defined as a list in which all insertions and deletions are made at one end,called to of the stack. Stack is a linear data structure with a variable size in the sense that the stack size depends upon the number of items currently present in it. With additions and deletions the size of the stack changes therefore it is a dynamic data structure. Stack operations Push- when an item is added Pop- when an item is deleted

Array implementation of stacks

# define stack size 100;

Struct stack {

Int top;

Int items[stacksize];

Struct stack s;

If s.top = 4 itmeans there are five elements in

the stack, these items are

s.items[0] ,s.items[1]], s.items[2], … s.items[4]

When stack has no element then

Top = -