l9 Linkedlist PDF
l9 Linkedlist PDF
Introduction
A linked list is a data structure which can change during execution.
Successive elements are connected by pointers. Last element points to NULL. It can grow or shrink in size during execution of a program. It can be made just as long as required. It does not waste memory space. A
July 21, 2009
head
B
Programming and Data Structure
C
2
Illustration: Insertion
A B C
Item to be inserted
X
July 21, 2009 Programming and Data Structure 4
Illustration: Deletion
Item to be deleted
In essence ...
For insertion:
A record is created holding the new item. The next pointer of the new record is set to link it to the item which is to follow it in the list. The next pointer of the item which is to precede it must be modified to point to the new item.
For deletion:
The next pointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item.
July 21, 2009 Programming and Data Structure 6
Types of Lists
Depending on the way in which the links are used to maintain adjacency, several different types of linked lists are possible.
Linear singly-linked list (or simply linear list)
One we have discussed so far.
head
head
10
11
Why abstract?
Because details of the implementation are hidden. When you do some operation on the list, say insert an element, you just call a function. Details of how the list is implemented or how the insert function is written is no longer required.
July 21, 2009 Programming and Data Structure 12
Conceptual Idea
13
Creating a List
15
How to begin?
To start with, we have to create a node (the first node), and make head point to it.
head = (node *) malloc(sizeof(node));
head
16
Contd.
If there are n number of nodes in the initial linked list:
Allocate n records, one by one. Read in the fields of the records. Modify the links of the records so that the chain is formed.
head
A
July 21, 2009
B
Programming and Data Structure
C
17
node *create_list() { int k, n; node *p, *head; printf ("\n How many elements to enter?"); scanf ("%d", &n); for { (k=0; k<n; k++) if (k == 0) { head = (node *) malloc(sizeof(node)); p = head; } else { p->next = (node *) malloc(sizeof(node)); p = p->next; } scanf ("%d %s %d", &p->roll, p->name, &p->age); } p->next = NULL; return (head); }
July 21, 2009 Programming and Data Structure 18
19
20
What is to be done?
Once the linked list has been constructed and head points to the first node of the list,
Follow the pointers. Display the contents of the nodes as they are traversed. Stop when the next pointer points to NULL.
21
void display (node *head) { int count = 1; node *p; p = head; while (p != NULL) { printf ("\nNode %d: %d %s %d", count, p->roll, p->name, p->age); count++; p = p->next; } printf ("\n"); }
July 21, 2009 Programming and Data Structure 22
23
24
How to do?
The problem is to insert a node before a specified node.
Specified means some value is given for the node (called key). In this example, we consider it to be roll.
Convention followed:
If the value of roll is given as negative, the node will be inserted at the end of the list.
25
Contd.
When a node is added at the beginning,
Only one next pointer needs to be modified.
head is made to point to the new node. New node points to the previously first element.
void insert (node **head) { int k = 0, rno; node *p, *q, *new; new = (node *) malloc(sizeof(node)); printf ("\nData to be inserted: "); scanf ("%d %s %d", &new->roll, new->name, &new->age); printf ("\nInsert before roll (-ve for end):"); scanf ("%d", &rno); p = *head; if (p->roll == rno) { new->next = p; *head = new; } /* At the beginning */
27
else { while ((p != NULL) && (p->roll != rno)) { q = p; p = p->next; } if { (p == NULL) q->next = new; new->next = NULL; } else if (p->roll { q->next = new; new->next = p; } } }
July 21, 2009 Programming and Data Structure 28
/* At the end */
29
30
What is to be done?
Here also we are required to delete a specified node.
Say, the node whose roll field is given.
31
void delete (node **head) { int rno; node *p, *q; printf ("\nDelete for roll :"); scanf ("%d", &rno); p = *head; if (p->roll == rno) /* Delete the first element */ { *head = p->next; free (p); }
32
else { while ((p != NULL) && (p->roll != rno)) { q = p; p = p->next; } if (p == NULL) /* Element not found */ printf ("\nNo match :: deletion failed");
else if (p->roll == rno) /* Delete any other element */ { q->next = p->next; free (p); } } }
July 21, 2009 Programming and Data Structure 33
Insert an element in a linked list in sorted order. The function will be called for every element to be inserted.
void insert_sorted (node **head, node *element);
Always insert elements at one end, and delete elements from the other end (first-in first-out QUEUE).
void insert_q (node **head, node *element) node *delete_q (node **head) /* Return the deleted node */
34
In
35
36
37
} typedef struct cplx complex; complex *add (complex a, complex *sub (complex a, complex *mul (complex a, complex *div (complex a, complex *read(); void print (complex a); complex complex complex complex b); b); b); b);
Structure definition
Function prototypes
38
add
Complex Number
print
July 21, 2009 Programming and Data Structure 39
Structure definition
Function prototypes
40
union
Set
size
July 21, 2009 Programming and Data Structure 41
*s);
push
STACK
43
Contd.
We shall look into two different ways of implementing stack:
Using arrays Using linked list
44
queue *create();
/* Create a new queue */
enqueue
QUEUE
46
47
PUSH
top top
48
POP
top top
49
PUSH OPERATION
top
50
POP OPERATION
top
51
Basic Idea
In the array implementation, we would:
Declare an array of fixed size (which determines the maximum size of the stack). Keep a variable which always points to the top of the stack. Contains the array index of the top element.
52
Declaration
#define MAXSIZE 100 struct lifo { int st[MAXSIZE]; int top; }; typedef struct lifo stack; stack s; struct lifo { int value; struct lifo *next; }; typedef struct lifo stack; stack *top;
ARRAY
LINKED LIST
53
Stack Creation
void create (stack *s) { s->top = -1; /* s->top points to last element pushed in; initially -1 */ } void create (stack **top) { *top = NULL; /* top points to NULL, indicating empty stack */ }
54
ARRAY
July 21, 2009 Programming and Data Structure 55
void push (stack **top, int element) { stack *new; new = (stack *) malloc(sizeof(stack)); if (new == NULL) { printf (\n Stack is full); exit(-1); } new->value = element; new->next = *top; *top = new; }
LINKED LIST
July 21, 2009 Programming and Data Structure 56
ARRAY
July 21, 2009 Programming and Data Structure 57
int pop (stack **top) { int t; stack *p; if (*top == NULL) { printf (\n Stack is empty); exit(-1); } else { t = (*top)->value; p = *top; *top = (*top)->next; free (p); return t; } }
July 21, 2009 Programming and Data Structure 58
LINKED LIST
ARRAY
LINKED LIST
59
ARRAY
LINKED LIST
60
push(&A,30); push(&B,100);
push(&B,5);
printf (%d %d, pop(&A), pop(&B)); push (&A, pop(&B)); if (isempty(&B)) printf (\n B is empty); }
61
push(&A,30); push(&B,100); push(&B,5); printf (%d %d, pop(&A), pop(&B)); push (&A, pop(&B)); if (isempty(B)) printf (\n B is empty); }
62
63
Basic Idea
Basic idea:
Create a linked list to which items would be added to one end and deleted from the other end. Two pointers will be maintained:
One pointing to the beginning of the list (point from where elements will be deleted). Another pointing to the end of the list (point where new elements will be inserted).
Rear
Front
DELETION
Programming and Data Structure
INSERTION
64
ENQUEUE
front
rear
65
DEQUEUE
front
rear
66
_QNODE *enqueue (_QUEUE *q, char x[]) { if(q->queue_rear==NULL) _QNODE *temp; { temp= (_QNODE *) q->queue_rear=temp; malloc (sizeof(_QNODE)); q->queue_front= if (temp==NULL){ q->queue_rear; printf(Bad allocation \n"); } return NULL; else } { strcpy(temp->name,x); q->queue_rear->next=temp; temp->next=NULL; q->queue_rear=temp; } return(q->queue_rear); } July 21, 2009 Programming and Data Structure 68
char *dequeue(_QUEUE *q,char x[]) { else{ _QNODE *temp_pnt; strcpy(x,q->queue_front->name); if(q->queue_front==NULL){ q->queue_rear=NULL; printf("Queue is empty \n"); return(NULL); } temp_pnt=q->queue_front; q->queue_front= q->queue_front->next; free(temp_pnt); if(q->queue_front==NULL) q->queue_rear=NULL; return(x); } }
69
void init_queue(_QUEUE *q) { q->queue_front= q->queue_rear=NULL; } int isEmpty(_QUEUE *q) { if(q==NULL) return 1; else return 0; }
70
main() { int i,j; char command[5],val[30]; _QUEUE q; init_queue(&q); command[0]='\0'; printf("For entering a name use 'enter <name>'\n"); printf("For deleting use 'delete' \n"); printf("To end the session use 'bye' \n"); while(strcmp(command,"bye")){ scanf("%s",command);
July 21, 2009 Programming and Data Structure 71
if(!strcmp(command,"enter")) { scanf("%s",val); if((enqueue(&q,val)==NULL)) printf("No more pushing please \n"); else printf("Name entered %s \n",val); } if(!strcmp(command,"delete")) { if(!isEmpty(&q)) printf("%s \n",dequeue(&q,val)); else printf("Name deleted %s \n",val); } } /* while */ printf("End session \n"); } July 21, 2009 Programming and Data Structure 72
ENQUEUE
DEQUEUE
front front
73
74
75
80