0% found this document useful (0 votes)
37 views23 pages

CS DataStructure-Lecture 5 Doubly Linked List and Circular Doubly

Data strructure

Uploaded by

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

CS DataStructure-Lecture 5 Doubly Linked List and Circular Doubly

Data strructure

Uploaded by

AsmaaGhoniem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Data Structure

Dr. Ahmed Hesham Mostafa


Lecture 5 – Doubly Linked List and
Circular Doubly Linked List
TextBooks
• s k chang, “data structures and algorithms”
• Kruse and Leung, “Data Structures & Program Design in C”
Online Martials
• CS214: Data Structures by Prof. Dr Waleed A. Yousef
• https://www.youtube.com/playlist?list=PLoK2Lr1miEm-
5zCzKE8siQezj9rvQlnca
• Data Structures Learning Course by Dr Mohammed El-Said
• https://www.youtube.com/playlist?list=PLfay0LLBd0wiNeOR_SGoYfC
3w-NxFwd0D
• Lectures Source code (updated frequently)
• https://github.com/ahmedheshamostafa/DataStructure
Doubly Linked List
• There two pointers in each node the next and previous pointers

0 1 k size-1
tail head size
typedef struct data{
int key;
Double Linked List
int val; Definition and operations
}info;

typedef struct node{ void create(dlist*);


struct node *next; void destroy(dlist*);
struct node *prev; void insertNode(info,dlist*);
info data; void deleteNode(info*,dlist*);
}Node; void deleteNodeAtPos(int,info*,dlist*);
void insertNodeAtPos(int,info,dlist*);
typedef struct list{ void TraverseAsStack(dlist*,void (*)(info));
Node *head; void TraverseAsQueue(dlist*,void (*)(info));
Node *tail; int isEmpty(dlist*);
int size; int isFull(dlist*);
}dlist; int listSize(dlist*);
Implementation Level User Level
void create(dlist*list){
list->head=NULL;
list->tail=NULL; int main()
list->size=0; {
} info e;
int isEmpty(dlist*list){
return list->size==0; int i=0;
} dlist l;
int isFull(dlist*list){ create(&l);
return 0;
} }
int listSize(dlist*list){
return list->size;
}
Implementation Level User Level
void insertNode(info e,dlist*list){ int main()
Node *n=(Node*)malloc(sizeof(Node)); {
n->data=e; info e;
n->next=NULL; int i=0;
n->prev=NULL; dlist l;
if(!list->head){ create(&l);
list->head=n;
list->tail=n;
} while(i<4){
else{ scanf("%d %d",&e.key,&e.val);
n->next=list->tail->next; insertNode(e,&l);
n->prev=list->tail; i++;
list->tail->next=n;
list->tail=n; }
} The new data will be inserted
list->size++; form the tail .. Like a Queue
}
void insertNodeAtPos(int pos,info e,dlist*list){ How to Insert node at
Node *n=(Node*)malloc(sizeof(Node));
Node*curr; position
int i=0;
n->data=e;
n->next=NULL;
User can insert node at
n->prev=NULL;
if(pos==0){
position 0<=pos<=size
list->head=n;
list->tail=n;
}
else{
for(curr=list->head;i<pos-1;i++){
curr=curr->next;
}
n->next=curr->next;
n->prev=curr;
curr->next=n;
}
list->size++;
}
How to delete node
void deleteNode(info* e,dlist*list){
if(!list->head)printf("\n List is Empty");
else{
Node*n=list->head->next;
The new data will be
deleted form the head ..
*e=list->head->data;
Like a Queue
free(list->head);
list->head=n;
n->prev=NULL;
list->size--;
}
}
void deleteNodeAtPos(int pos,info* e,dlist*list){ How to delete node at
int i=0;
Node* tmp; position
if(!list->head)
printf("\n List is Empty");
else{
Node* curr=list->head; User can delete node at
Node*n=list->head->next;
for(curr=list->head;i<pos-1;i++){ position 0<=pos<=size
curr=curr->next;
}
*e=curr->next->data;
tmp=curr->next->next;
tmp->prev=curr;
free(curr->next);
curr->next=tmp;

list->size--;
}
}
void TraverseAsStack(dlist* list,void (*visit)(info e)){
Node*tmp=list->tail;
while(tmp){ Traverse as stack and
(*visit)(tmp->data); queue
tmp=tmp->prev;

}
}

void TraverseAsQueue(dlist* list,void (*visit)(info e)){


Node*tmp=list->head;
while(tmp){
(*visit)(tmp->data);
tmp=tmp->next;
}
}
How to destroy the double
linked list at position
void destroy(dlist*list){
Node*tmp;
while(list->head){
tmp=list->head->next;
free(list->head);
list->head=tmp;
}
list->size=0;
}

12
#include <stdio.h>
#include <stdlib.h> Full Example
#include "dlist.h"

void display(info e){


at user Level
printf(" %d %d \n",e.key,e.val);
}

int main() deleteNode(&e,&l);


{ printf("\n Print List after deleteing an item \n");
info e; TraverseAsQueue(&l,&display);
int i=0;
dlist l; insertNodeAtPos(1,e,&l);
create(&l); printf("\n Print List after inserting an item \n");
TraverseAsQueue(&l,&display);
while(i<4){ deleteNodeAtPos(3,&e,&l);
scanf("%d %d",&e.key,&e.val); printf("\n Print List after deleteing an item \n");
insertNode(e,&l); TraverseAsQueue(&l,&display);
i++; printf("\n Print List after deleteing an item as stack \n");
} TraverseAsStack(&l,&display);
printf("\n Print List as Queue \n"); return 0;
TraverseAsQueue(&l,&display); }
printf("\n Print List as Stack \n ");
TraverseAsStack(&l,&display);
Full Example
at User Level
Full Example
at User Level
Circular Doubly Linked List
• There two pointers in each node the next and previous pointers
• The next for last node point to the first node and previous for first
node point to the last node

0 1 k size-1
tail head size
Circular Doubly Linked List
• Same struct definitions and operations for Doubly linked list
• Same implementation for
• void create(dlist*);
• int isEmpty(dlist*);
• int isFull(dlist*);
• int listSize(dlist*);
void insertNode(info e,dlist*list){
How to insert
Node *n=(Node*)malloc(sizeof(Node));
n->data=e; node
n->next=NULL;
n->prev=NULL;
if(!list->head){
list->head=n;
list->tail=n;
n->next=list->head;
n->prev=list->tail;
}
else{
n->next=list->head;
n->prev=list->tail;
list->tail->next=n;
list->tail=n;
}
list->size++;
}
How to Delete
void deleteNode(info* e,dlist*list){ node
if(!list->head)printf("\n List is Empty");
else{
Node*n=list->head->next;
*e=list->head->data;
free(list->head);
list->head=n;
n->prev=list->tail;
list->size--;
}
}
void TraverseAsStack(dlist* list,void (*visit)(info e)){
Node*tmp=list->tail; Traverse as stack
int size=list->size;
and Queue
while(size){
(*visit)(tmp->data);
tmp=tmp->prev;
size--;

}
}
void TraverseAsQueue(dlist* list,void (*visit)(info e)){
Node*tmp=list->head;
int size=list->size;
while(size){
(*visit)(tmp->data);
tmp=tmp->next;
size--;

}
}
How to Destroy
void destroy(dlist*list){
the List
Node*tmp;
while(list->size){
tmp=list->head->next;
free(list->head);
list->head=tmp;
list->size--;
}
}
#include <stdio.h>
#include <stdlib.h>
#include "dlist.h" Full Example at
void display(info e){
printf(" %d %d \n",e.key,e.val);
}
user Level
int main()
{
info e;
int i=0; deleteNode(&e,&l);
dlist l; printf("\n Print List after deleteing an item \n");
create(&l); TraverseAsQueue(&l,&display);
printf("\n Print List after deleteing an item as stack\n");
while(i<4){ TraverseAsStack(&l,&display);
scanf("%d %d",&e.key,&e.val); return 0;
insertNode(e,&l); }
i++;
}
printf("\n Print List as Queue \n");
TraverseAsQueue(&l,&display);
printf("\n Print List as Stack \n ");
TraverseAsStack(&l,&display);
Full Example
at User Level

You might also like