0% found this document useful (0 votes)
14 views14 pages

CHP 4 Linked List - New UPDATED

Chp 4 Linked list_new UPDATED

Uploaded by

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

CHP 4 Linked List - New UPDATED

Chp 4 Linked list_new UPDATED

Uploaded by

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

Chp 4: Linked List

Linked List:-

1. Linked list can be defined as collection of objects called nodes that are
randomly stored in memory.

2.A node contains two fields i.e., data field & address field. Address field
contains the address of successor (next node).

3. Representation of linked list is given below.

Data Address

NODE

4.Example:-

Data address
Head Tail
12 14 16 NULL

(End of List)

Node

1. Terminology

Ans:- 1.Node:- The structure combination of data and it’s address to the next
structure is called as node.

2.Address:- It stores the address of next node of the linked list.


Dhrupesh sir 9699692059
3.Pointer:- A datatype used to point to the address of the next node.

4.Information:- The data stored in data field of the linked list structure.

5.Null Pointer:- It signifies the end of linked list when the next pointer points to
null.

6.Empty list:- It signifies no data in the linked list.

2. Operation on Linked list

Ans:-1.Creating a linked list

2.Inserting an item or data

3.Delete an item or data

4.Traversing a linked list

5.Sorting of element in a linked list

6. Counting the nodes

7.Merging of two sorted linked list

8.Searching an item in a linked list

3. Types of linked list

Ans:- 1.Singly Linked List

2.Doubly Linked List

3.Circular Linked List

4. Structure used to define a node

Dhrupesh sir 9699692059


Ans:-

struct node

int data;

struct node * next

}node;

5. Construct a singly linked list using data fields 21 25 96 58 74 and show


procedure step-by-step with the help of diagram start to end.

Dhrupesh sir 9699692059


Dhrupesh sir 9699692059
6. Differentiate between array and linked list

Array Linked list

Dhrupesh sir 9699692059


Dhrupesh sir 9699692059
7. Write an algorithm to search a particular
node in the give linked list

Assumption:

Node contains two fields: info and next pointer

start pointer : Header node that stores address of first node

step 1: start

step 2: Declare variable no, flag and pointer temp

step 3: Input search element

step 4: Initialize pointer temp with the address from start pointer.(

temp=start), flag with 0

step 5: Repeat step 6 till temp != NULL

step 6: compare: temp->info = no then

set flag=1 and go to step 7

otherwise

increment pointer temp and go to step5

step 7: compare: flag=1 then

display "Node found"

otherwise

display "node not found"

step 8: stop

8. Show with suitable diagram for deleting a node in SLL.

Dhrupesh sir 9699692059


Dhrupesh sir 9699692059
Dhrupesh sir 9699692059
ALGORITHMS :
9. ALGORITHM TO DELETE

Delete a node from the beginning:-


Step 1: Create temporary node ‘temp’.

Step 2: Assign address of first node to ‘temp’ pointer.

Step 3: Store address of second node (temp->next) in header pointer ‘start’.

Step 4: Free temp.

Delete a node from in between position:-


Step 1: Create temporary node ‘temp’, ’q’.

Step 2: Assign address of first node to ‘temp’ pointer.

Step 3: Traverse list up to previous node of node to be deleted.

Step 4: Mark the node to be deleted ‘q’.

Dhrupesh sir 9699692059


Step 5: Store address from node ‘q’ in address field of ‘temp’ node (temp- >next=q->next).

Step 6: Free q.

Delete a node from the end:-


Step 1: Create temporary node ‘temp’,’q’.

Step 2: Assign address of first node to ‘temp’ pointer.

Step 3: Traverse list upto second last node.

Step 4: Mark last node’s address in node ‘q’.

Step 5: store NULL value in address field of second last node (temp->next).

Step 6: Free q

10. ALGORITHM OF COUNTING of nodes


For example, the function should return 5 for linked list 1->3->1->2->1.

Algorithm: Using Iterative Solution

1) Initialize count as 0

2) Initialize a node pointer, current = head.

3) Do following while current is not NULL

a) current = current -> next

b) count++;

4) Return count

11. Algorithm to insert an element at the beginning of


linked list:
Dhrupesh sir 9699692059
1. Start

2. Create the node pointer *temp Struct node * temp

3. Allocate address to temp using malloc temp = malloc(sizeof(struct node));

4. Check whether temp is null, if null then Display “Overflow”

Else

temp-> info=data

temp-> next=start

5. Start=temp

6. stop

12. WAP for traverse in linked list

Write a program to traverse a linked list.


(Note: create_list and addatbeg are optional)
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

void create_list(int);
void addatbeg(int);
void display();
struct node
{
int info;
struct node *next;
}*start=NULL;

void main()
{
int m;
clrscr();
printf("enter data value\n");
scanf("%d",&m);
create_list(m);
printf("enter data value\n");
scanf("%d",&m);

Dhrupesh sir 9699692059


addatbeg(m);
display();
getch();
}

void create_list(int data)


{
struct node *tmp,*q;
tmp=malloc(sizeof(struct
node)); tmp->info=data;
tmp->next=NULL;
start=tmp;
}

void addatbeg(int data)


{
struct node *tmp;
tmp=malloc(sizeof(struct
node)); tmp->info=data;
tmp->next=start;
start=tmp;
}
void display()
{
struct node *q;
if(start==NULL)
{
printf("list is empty\n");
}
q=start;
printf("list is:\n");
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->next;
}
}

13. Algorithm to insert an element at the end of linked list:


1. Start

2. Create two node pointers *temp, *q struct node * temp, *q;

3. q= start

4. Allocate address to temp using malloc temp = malloc (sizeof(struct node));


Dhrupesh sir 9699692059
5. Check whether temp is null, if null then Display “Overflow”

else

temp-> info=data

temp-> next=null

6. While(q->next!=null) q= q-> next

7. q->next= temp

8. stop

Dhrupesh sir 9699692059

You might also like