You are on page 1of 35

Linked Lists

Review Discussion
CSEDIU
struct node {
Menu
int data;
struct node* next;
};

typedef struct node Node;


typedef struct node* List;

List Initialize();
void InsertBegin(List l,int d);
void InsertEnd(List l, int d);
void Insert(List l, Node* pos,int d);
Node* Find(List l,int d);
void Delete(List l, int d);

CSE@DIU
Menu Menu

Initialize
InsertBegin
InsertEnd
Insert
Find
Delete

CSE@DIU
Menu

Initialize

CSE@DIU
Menu

List Initialize()
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
return temp;
}

CSE@DIU
Menu

head

List Initialize()
{
Node* temp;
main() temp = (Node*)calloc(1,sizeof(Node));
{ return temp;
List head; }
head = Initialize();
}

CSE@DIU
Menu

InsertBegin

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


1
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 1

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


1
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


1
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

void InsertBegin(List head,int d)


1 {
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

head->next = temp;
temp->next = head->next;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 1

void InsertBegin(List head,int d)


{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 1

void InsertBegin(List head,int d)


10
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = head->next;
head->next = temp;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 1

void InsertBegin(List head,int d)


10
{
Node* temp;
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

head->next = temp;
temp->next = head->next;
}
CSE@DIU
Menu

InsertEnd

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1

tail

void InsertEnd(List head,int d)


{
Node *tail,*temp;
tail = head;
........
........
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1

tail

void InsertEnd(List head,int d)


{
Node *tail,*temp;
tail = head;
while(tail->next != NULL)
tail = tail->next;
........
........
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1

tail

void InsertEnd(List head,int d)


{
Node *tail,*temp;
tail = head;
while(tail->next != NULL)
tail = tail->next;
........
........
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head tail

X 10 1

8
void InsertEnd(List head,int d)
{
........
........
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

tail->next = temp;

CSE@DIU
Menu

Insert

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 8

void Insert(List head,Node* p,int d)


{
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = p->next;
p->next = temp;
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 8

4
void Insert(List head,Node* p,int d)
{
temp = (Node*)calloc(1,sizeof(Node));
temp->data = d;

temp->next = p->next;
p->next = temp;
}

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 4 8

CSE@DIU
Menu

Find

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 4 8

void Find(List l,Node* p,int d)


{
Node *temp;
temp = l;
while(temp->next != NULL)
{
if(temp->next->data == d)
return temp;
temp = temp->next;
}
return NULL;
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head temp

X 10 1 4 8

void Find(List l,Node* p,int d)


{
Node *temp;
temp = l;
while(temp->next != NULL)
{
if(temp->next->data == d)
return temp;
temp = temp->next;
}
return NULL;
}
CSE@DIU
Menu

Delete

CSE@DIU
Menu
1 10 8 4 6 3 2 5

head

X 10 1 4 8

void Delete(List l,Node* p,int d)


{
Node *temp,*del;
temp = Find(l,d);
if(temp != NULL)
{
del = temp->next;
temp->next = del->next;
free(del);
}
}
CSE@DIU
Menu
1 10 8 4 6 3 2 5

head temp del

X 10 1 4 8

void Delete(List l,Node* p,int d)


{
Node *temp,*del;
temp = Find(l,d);
if(temp != NULL)
{
del = temp->next;
temp->next = del->next;
free(del);
}
}
CSE@DIU
Menu
10 8 4 6 3 2 5

head

X 10 4 8

CSE@DIU
Menu

int main
{
List l;
Node* temp;
l = Initialize();
InsertBegin(l,1);
InsertBegin(l,10);
InsertEnd(l,8);
temp = Find(l,8);
Insert(l,temp,4);
Delete(l,1);
}
CSE@DIU
Menu

Enjoy Linking You in List

CSE@DIU

You might also like