Single Linked Lists
Single Linked Lists
Single Linked Lists
The linked list consists of a series of structures called nodes. Each node
contains two fields: a "data" field and a "next" field which is a pointer used to link
one node to the next node.
Each node is allocated in the heap with a call to malloc(), so the node memory
continues to exist until it is explicitly deallocated with a call to free().The node
called a head is the first nod in the list . The last node's next pointer points to Null
value. The following figure shows the actual representation of the list with its delete
and insert operations .
Programming Details
Before writing the code to build the above list, we need two data types:
• Node The type for the nodes which will make up the body of the
list. These are allocated in the heap. Each node contains a single
data element and a pointer to the next node in the list.
struct node
{
int data;
struct node* next;
};
1
• Node Pointer The type for pointers to nodes.
struct node
{
int data;
struct node *next;
};
typedef struct node* List;
List header;
Here is simple function which uses pointer operations to build the list {1, 2, 3} :
Length() Function:
The Length() function takes a linked list and computes the number of elements
in the list.
int Length(node* head) // example1
{
node* current = head;
int count = 0;
while (current != NULL)
{ count++;
current = current->next;
}
return count;
}
Calling Length()
2
Here's some typical code which calls Length().
struct node* myList = BuildOneTwoThree();
int len = Length(myList); // results in len == 3
BuildOneTwoThree() cotains three steps to add a node in the list:
1- Allocate the new node in the heap and set its data .
struct node* newNode;
newNode = malloc(sizeof(node));
newNode->data = data;
2- Link Next Set the .next pointer of the new node to point to the current first
node of the list.
newNode->next = head;
3- Link Head Change the head pointer to point to the new node, so it is now the
first node in the list.
head = newNode;
CopyList() function
It takes a list and returns a complete copy of that list.
node* Copy_List(node* head1)
{ //example1
node *head2,*temp1,*temp2;
if(head1==NULL)
{
head2=NULL;
return head2;
}
temp1=head1;
head2=(node*)malloc(sizeof(node));
temp2=head2;
while(temp1->next)
{
temp2->data=temp1->data;
temp2->next=(node*)malloc(sizeof(node));
temp1=temp1->next;
temp2=temp2->next;
}
temp2->data=temp1->data;
4
temp2->next=NULL;
return head2;
}
CopyList() Recursive
// Recursive variant
node* Copy_List_Rec(node* head1)
{
if(!head1)
return NULL;
node* newnode=(node*)malloc(sizeof(node));
newnode->data=head1->data;
newnode->next=Copy_List_Rec(head1->next);
return newnode;
}
Questions:
Q1:Write a Count() function that counts the number of times a
given int occurs in a list.
6
Q2: Write a GetNth() function that takes a linked list and an integer index
and returns the data value stored in the node at that index position.
Q3-Write a function DeleteList() that takes a list, deallocates all of its
memory and sets its head pointer to NULL (the empty list).
Q4- Write a function InsertNth() which can insert a new node at any
index within a list .
Q5-Write a SortedInsert() function which given a list that is sorted in
increasing order, and a single node, inserts the node into the correct
sorted position in the list.
Q6-Write an Append() function that takes two lists, 'a' and 'b', appends 'b'
onto the end of 'a'.
Q7-Given a list, split it into two sublists — one for the front half, and one
for the back half. If the number of elements is odd, the extra element
should go in the front list. So FrontBackSplit() on the list {2, 3, 5, 7,
11} should yield the two lists {2, 3, 5} and {7,11}.
Q8-Write a RemoveDuplicates() function which takes a list sorted in
increasing order and deletes any duplicate nodes from the list.
Q9-Write an iterative Reverse() function that reverses a list by
rearranging all the .next pointers and the head pointer.
Q10- Write a MoveNode() function that takes two lists, removes the
front node from the second list and pushes it onto the front of the
first.
Q11-Write a POP() function that Extracts the data from the head node,
deletes the node, advances the head pointer to point at the next node
in line
Q12-Given two lists, merge their nodes together to make one list, taking
nodes alternately between the two lists. So ShuffleMerge() with {1,
2, 3} and {7, 13, 1} should yield {1, 7,2, 13, 3, 1}. If either list runs
out, all the nodes should be taken from the other list.
Solutions:
Q1:
int Count(list head,int val)
{
7
int count =0;
list temp;
for(temp=head;temp!=NULL;temp=temp->next)
if(temp->data==val) count++;
return count;
}
Q2:
int GetNth(list head,int index)
{ //example1
int count =1;
list temp;
for(temp=head;temp!=NULL;temp=temp->next)
{ if(index==count)
return temp->data;
count++;
}
return 0;
}
Q3:
void DeleteList(list* head)
{ //example1
list next,current=*head;
while(current!=NULL)
{
next=current->next;
free(current);
current=next;
}
*head=NULL;
}
Q4-
void InsertNth(list* head,int index,int data)
{ //example1
list nod,temp,prev;
int count;
if(index<1) return ;
count=1;
prev=NULL;
temp=*head;
while(count!=index && temp!=NULL)
{
count++;
prev=temp;
temp=temp->next;
}
8
if(count!=index) return ;
nod=(list)malloc(sizeof(node));
nod->data=data;
if(prev==NULL)
{
nod->next=*head;
*head=nod;
return;
}
prev->next=nod;
nod->next=temp;
}Q5-
void SortedInsert(node** headRef,int val)
{ //example1
node* current;
node* nod=(node*)malloc(sizeof(node));
nod->data=val;
if (*headRef == NULL||(*headRef)->data >=val)
{
nod->next = *headRef;
*headRef = nod;
}
else
{
current = *headRef;
while (current->next!=NULL &&
current->next->data<nod->data)
current = current->next;
nod->next = current->next;
current->next = nod;
}
}
Q6-
void Append( node** aRef,node* bRef )
{ //example1
node* current;
if (*aRef == NULL)
{
*aRef =bRef;
}
else
{
current = *aRef;
while (current->next != NULL)
{
9
current = current->next;
}
current->next =bRef;
}
}
Q7-
void FrontBackSplit(node* source,node** frontRef, node**
backRef)
{ //example1
int len = Length(source);
int i;
node* current = source;
if (len < 2)
{
*frontRef = source;
*backRef = NULL;
}
else
{
int hopCount = (len)/2;
for (i = 1; i<=hopCount; i++)
current = current->next;
*frontRef = source;
*backRef = current->next;
current->next = NULL;
}
}
Q8-
void RemoveDuplicates(node* head)
{ //example1
struct node* current = head, * nextNext;
if (current == NULL)
return;
while(current->next!=NULL)
if (current->data == current->next->data)
{
nextNext =current->next;
current->next=current->next->next;
free(nextNext);
}
else
current = current->next;
}
Q9-
10
void Reverse(node** head)
{ //example1
node* result = NULL;
node* current = *head;
node* next;
while (current != NULL)
{
next = current->next;
current->next = result;
result = current;
current = next;
}
*head = result;
}
Q10-
void MoveNode(node** dest,node** source)
{ //example1
node* nod = *source;
if(*source ==NULL) return;
*source = nod->next;
nod->next = *dest;
*dest = nod;
}
Q11-
int Pop(node** head)
{
node* head;
int result;
if(*head ==NULL) return;
head = *headRef;
result = head->data;
*headRef = head->next;
free(head);
return(result);
}
11