Professional Documents
Culture Documents
Unit 2 Linked List: Next Item"
Unit 2 Linked List: Next Item"
Linked List
• According to NIST(National Institute of Standards and Technology)
linked list is “a list implemented by each item having a link to the
next item”.
• Linked list and arrays are similar since they both store collections of
data.
• The specific type of element is not important since essentially the
same structure works to store elements of any type.
• A linked list is an ordered collection of data in which each
element contains the location of the next element; that is, each
element two parts: data and link.
• The data part holds the useful information, the data to be
processed.
• The link is used, to chain the data together. It contains a pointer that
identifies the next element in list.
• There is also a pointer variable that identifies the first element in the
list.
• The simple linked list we have described here is commonly known
as a singly linked list because it contains only one link to a single
successor.
• The major advantage of the linked list over other general list
structures is that data are easily inserted and deleted.
• It is not necessary to shift elements of a linked list to make room for
a new element or to delete an element.
• On the other hand, because the elements are no longer physically
sequenced, we are limited to sequential searches; we cannot use a
binary search.
• The link in each element, except the last, points to its successor.
• The link in the last element contains a null pointer, indicating the end
of the list.
• The elements in a linked list are traditionally called nodes.
• A node in linked list is a structure that has at least two fields one
contains the data, the other a pointer to the address of the next
node in the sequence.
Data Link Data Link Data Link Data Link
An empty
Head list
A link List with a head pointer
Types of Linked Lists
1. Singly-linked List:
The simplest kind of linked list is a singly-linked list, which has one
link per node. This link points to next node in the list, or to null value or
empty list if it is the final node.
12 99 37
A singly linked list containing three integer value
2. Doubly-linked list:
A more sophisticated kind of linked list is a doubly-linked list or two-
way linked list.
Each node has two links: one points to the previous node, or points
to a null value or empty list if it is the first node; and one points to the next,
or points to a null value or empty list if it is the final node.
*prev Data *next tail
Head
3. Circularly-linked list:
In a circularly-linked list, the first and final nodes are linked
together.This can be done for both singly and doubly linked lists.
To traverse a circular linked list, you begin at any node and follow
the list in either direction until you return to the original node.
(a) Singly-circularly-linked list:
In a singly-circularly-linked list, each node has one link, similar
to an ordinary singly-linked list, except that the next link of the last
node points back to the first node. As in a singly-linked list, new
nodes can only be efficiently inserted after a node we already have
a reference to.
Head
(b) Doubly-circularly-linked list:
In a doubly-circularly-linked list, each node has two links, similarly to a
doubly-linked list, except that the previous link of the first node points to the
last node and the next link of the last node points to first node. In a doubly-
linked lists, insertions and removals can be done at any point with access to
any nearby node.
Head
4. Head Linked Lists:
• A headed list is linked list, which always contain a special node,
called header node, at the beginning of the linked list. This header
node usually contains vital information about the linked list such as
the number of nodes in the list, whether the list is sorted or not.
Header node
Head
Header node
Head
Node:
• The type for the node which will make up the body of the list.
• Each node contains a single client data element and a pointer to the
next node in the list.
Type: struct node
typedef struct node
{
int data;
struct node* next;
}node;
• The type for pointers to nodes. This will be the type of the Head
pointer and the next fields inside each node. In C and C++, no
separate type declaration is required since the pointer type is just
the node type followed by a ‘ * ’. Type: struct node*
Dynamic Memory Management
Function Name Description
Malloc Allocates memory from heap
Calloc Allocates memory and initializes the allocated memory to zero
Realloc Readjust the existing block and copies the contents to the
new
free location
Deallocates a block allocated by malloc,calloc and realloc fn
1 Set PTR:=START
2. Repeat steps 3 and 4 while PTR!=NULL
3. Apply PROCESS to INFO[PTR]
4. Set PTR:=LINK[PTR] [PTR now points to next node]
[End of step 2 loop]
5. Exit.
Algorithm for searching a Linked List
Search(INFO,LINK,START,ITEM,LOC)
This algorithm finds location LOC of the node where ITEM first appears
in LIST,or sets LOC=NULL
1. Set PTR:=START
2. Repeat step 3 while PTR!=NULL
3. If ITEM=INFO[PTR],then
Set LOC:=PTR, and exit
Else:
Set PTR:=LINK[PTR]
[End of if structure]
4. [Search is unsuccessful] Set LOC:=NULL
5.Exit
Memory Allocation
1.[OVERFLOW?] If AVAIL=NULL,then:
Write:OVERFLOW and exit
2.Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
3.Set INFO[NEW]:=ITEM
4.If LOC=NULL,then:
Set LINK[NEW]:=START and START:=NEW
Else:
Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:=NEW
[End of Ifstructure]
5.Exit
Algorithm for deleting a node following a given
node
DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
This algorithm deletes the node N with location LOC. LOCP is the
location of the node which precedes N or, when N is the first
node,LOCP=NULL
1. If LOCP=NULL,then:
Set START:=LINK[START]
else:
Set LINK[LOCP]:=LINK[LOC]
[End of if structure]
2. [return deleted node to the AVAIL list]
Set LINK[LOC]:=AVAIL and AVAIL:=LOC
3.Exit.
• Creating a node:
• Creating a new node is simple. The memory needed to store the node is
allocated, and the pointers are set up.
• node *create( )
• { int item; node ptr;
• printf("enter the item");
• scanf("%d",&item);
• ptr= (node *)malloc(sizeof(node));
• ptr->info=item;
• ptr->next=NULL;
• return ptr; }
• Traversing a list:
• void traverseInOrder()
• { node *loc;
• loc=head;
• while(ptr!=NULL){
• printf("%d\n",loc->info);
• loc=loc->next; }
• }
• #include<stdio.h>
• #include<conio.h>
• typedef struct nodetype{
Node *create( )
• int info;
• struct nodetype *next; { int item;
• Polynomial Manipulation
• 4x3 + 6x2 + 10x + 6 Coefficient
Poly Power
4 3 6 2 10 1 6 0
Representation of a polynomial
• Addition of Polynomials
• 5x4 + 6x2 + 10x – 6
• + 7x3 + 3x2 + 2x + 7
• 5x4 + 7x3 + 9x2 + 12x + 1
• void addNode(node ptr, int coef, int powe)
• { node *tempPtr, *loc;
• tempPtr = (node *) malloc( sizeof (node));
• tempPtr->coeff = coef;
• tempPtr->power = powe;
• tempPtr->next=NULL;
• if( ptr==NULL)
• ptr= tempPtr;
• else
• loc = ptr;
• while(loc->next!= NULL)
• loc=loc->next;
• loc->next = tempPtr;
• }
• void printPolynomial(node ptr )
• { if( ptr != NULL )
• printf(“%dx^%d”,ptr->coeff, ptr->power);
• for(ptr= ptr->next; ptr!=NULL; ptr= ptr->next )
• {
• if(ptr->coeff > 0)
• printf(“ + “);
• if(ptr->power == 0)
• printf(“%d”, ptr->coeff);
• else if(ptr-> power == 1 )
• printf(“%dx”, ptr-> coeff);
• else
• printf(“%dx^%d”, ptr->coeff, ptr->power);
• } }
• void addPolynomial(node ptr1, node ptr2, node ptr3)
• { int powe;
• float coef;
• while( ( ptr1!= NULL) && ( ptr2!=NULL) )
• {
• if( ptr1->power > ptr2->power ) {
• coef = ptr1->coeff;
• powe = ptr1->power;
• ptr1 = ptr1->next; }
• else if ( ptr1->power < ptr2->power) {
• coef = ptr2->coeff;
• powe = ptr2->power;
• ptr2 = ptr2-> next; }
• else {
• coef = ptr1->coeff + ptr2->coeff;
• powe = ptr1->power;
• ptr1 = ptr1->next;
• ptr2 = ptr2->next; }
• if ( coef != 0 )
• addnode(ptr3, coef, pow );
• }
• if ( ptr1 == NULL ) {
• for( ; ptr2 != NULL; ptr2= ptr2->next )
• addnode( ptr3, ptr2->coeff, ptr2->power ); }
• else if ( ptr2 == NULL ) {
• for( ; ptr1 != NULL; ptr1 = ptr1->next )
• addnode( ptr3, ptr1-> coeff, ptr1->power); }
• } }
•
• void deletePolynomial( node ptr )
• { node *temp;
• while ( *ptr != NULL )
• { temp = *ptr;
• *ptr = (*ptr)->next;
• free(temp);
• }
• }
Multiplication of polynomials
• 5x4 + 6x2 + 10x - 6
• x 3x2 + 2x + 7
• 15x6 +18x4 +30x3 -18x2
• 10x5 +12x3 +20x2 -12x
• 35x4 +42x2 +70x -42
• 15x6 +10x5 +53x4 +42x3 +44x2 +58x -42
• Void multiplyPolynomial( node ptr1, node ptr2, node ptr3)
• {
• int powe, coef ;
• node *temp,*loc;
• while(ptr1!=NULL)
• { temp = ptr2;
• while(temp !=NULL)
• { coef = ptr1-> coeff * temp -> coeff;
• powe = ptr1 -> power + temp -> power;
• loc = search(ptr3 , powe);
• if( loc == NULL) Node *search( node ptr, int degree)
• insertNode( ptr3, coef, powe); { while ( ptr != NULL)
• { if ( ptr -> power == degree )
else
break;
• loc -> coeff += coef;
ptr = ptr -> next;
• temp = temp -> next; }
• } ptr1 = ptr1 -> next; return ptr;
• } } }
•
• void InsertNode ( node ptr, int coef, int powe )
• { node *tempPtr, *loc, *ploc ;
• tempPtr = ( node *) malloc (sizeof (node));
• tempPtr -> coeff = coef;
• tempPtr -> power = powe;
• if( ptr == NULL )
• { tempPtr -> next = NULL;
• ptr = tempPtr ; }
• else {
• ploc = NULL;
• loc = ptr;
• while( ( loc != NULL && powe < loc -> power ) )
• { ploc = loc;
• loc = loc -> next; }
• tempPtr -> next = ploc -> next;
• ploc -> next = tempPtr;
• }
• }
•
•
GENERALIZED LINKED LIST
0 3 1 X
0 4 0 5 X
Queues
• A queue is a linear list in which insertions can take place at one
end of the list, called the rear of the list, and delete can take place
only at other end, called the front of the list.
• The behaviour of a queue is like a First-In-First-Out (FIFO) system.
Operations On Queues
• CreateQueue( )- to create q as an empty queue.
• Enque(i)- to insert(add) element i in a queue q.
• Dequeue( )- to access and remove an element of queue q.
• Peek( )- to access the first element of the queue q without removing
it.
• isFull( )- to check whether the queue q is full.
• isEmpty( )- to check whether the queue q is empty.
0 1 2 3 4 5 6 7 8 9
-1 -1
Front Rear Representation of queue in memory
0 1 2 3 4 5 6 7 8 9
0 4 5 7 12 8 9
Front Rear Representation of queue in memory
0 1 2 3 4 5 6 7 8 9
2 4 12 8 9
Representation of queue in memory
Front Rear 0 1 2 3 4 5 6 7 8 9
2 7 12 8 9 3 10 11
Representation of queue in memory
0 1 2 3 4 5 6 7 8 9
Front Rear
2 9 12 8 9 3 10 11 15 20
Representation of queue in memory
Front Rear 0 1 2 3 4 5 6 7 8 9
0 8 12 8 9 3 10 11 15 20 60
Representation of queue in memory
Limitations of a linear queue
• int dequeue() {
• int temp;
• temp=q.element[q.front];
• if(q.front==q.rear)
• q.front=q.rear=-1;
• else
• q.front=(q.front+1)%capacity;
• return temp; }
• #define capacity 10
• typedef enum{false,true}boolean;
• typedef struct
• { int front, rear;
• int element[capacity];
• }queue; queue q;
• void createQueue();
• boolean isEmpty();
• boolean isFull();
• void enqueue(int);
• int dequeue();
• void main()
• { int choice, element;
• createQueue();
• do
• {
• printf("1. Enqueue\n");
• printf("2. Dequeue\n");
• printf("3. Peek\n");
• printf("4. Exit\n\n");
• printf("Enter choice(1-4):");
• scanf("%d",&choice);
• switch(choice)
• {
• case 1: clrscr();
• if(isFull()){
• printf("Queue full...\nPress any key to continue");
• getch();
• }else{
• printf("Enter value:");
• scanf("%d",&element);
• enqueue(element);
• }break;
• case 2: clrscr();
• if(isEmpty()){
• printf("Queue empty...\nPress anykey to continue");
• getch();
• }else{
• printf("Value dequeue is %d\n",dequeue());
• printf("\nPress any key to continue...");
• getch();
• }break;
• case 3: clrscr();
• if(isEmpty()){
• printf("Queue empty...\nPree any key to continue");
• getch();
• }else{
• printf("1st Element in queue is %d\n",peek());
• printf("Press any key to continue...");
• getch();
• }
• }
• }while(choice!=4);
• getch();
• }