0% found this document useful (0 votes)
13 views3 pages

Tutorial 3

1. A C program to reverse a singly linked list in a single pass is presented. It uses three pointers - start, p and q to traverse the list and reverse the links in one pass. 2. A method to split a singly linked list into two equal parts in a single pass is shown. It uses two pointers p and q to traverse the list and find the midpoint. The pointer to the second half is returned. 3. The implementation of a queue using linked list is demonstrated. It uses two pointers - front and rear to implement the queue. Functions for enqueue and dequeue are defined to insert and remove elements from the queue.

Uploaded by

piyali999
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)
13 views3 pages

Tutorial 3

1. A C program to reverse a singly linked list in a single pass is presented. It uses three pointers - start, p and q to traverse the list and reverse the links in one pass. 2. A method to split a singly linked list into two equal parts in a single pass is shown. It uses two pointers p and q to traverse the list and find the midpoint. The pointer to the second half is returned. 3. The implementation of a queue using linked list is demonstrated. It uses two pointers - front and rear to implement the queue. Functions for enqueue and dequeue are defined to insert and remove elements from the queue.

Uploaded by

piyali999
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/ 3

Data Structure and Object Representation

Autumn 2014
Tutorial 3
August 7, 2014
---------------------------------------------------------------------------------------------------------------------------1. Write a C program to reverse a singly linked list in a single pass.
Soln.-

nodeptr reverse(nodeptr start)


{
nodeptr p,q;
p=start;
q=p->next;
p->next=NULL;
while(q->next!=NULL)
{
q->next=p;
p=q;
q=q->next;
}
return q; //q is the pointer to the starting node in the reversed list
}

2. Split a singly linked list into two equal parts in a single pass and return the pointer
to both the lists.
Soln./*structure of the node*/
struct node
{
Int data;
Struct node* next;
};
//function for partitioning
nodeptr partition(nodeptr start)
//function returns pointer to the second partition
{
nodeptr p,q,start2;
//start2 is the pointer to the second list after partition
p=start;
q=p->next;
if(q=NULL)
{
return NULL;
}
else
{
while(q->next!=NULL)
{

p=p->next;
q=q->next;
if(q->next!=NULL)
{
q=q->next;
}
}
start2=p->next;
p->next=NULL;
}
return start2;
}
3. Implement a queue using linked list.
Soln./*structure of the node*/
struct node
{
Int data;
Struct node* next;
}*front, *rear;
/*front points to the end from where the queue is emptied, rear is the end
from where the link is filled.*/
/*function to insert an element into the queue*/
void enqueue()
{
Int item;
printf("Enter ITEM: ");
scanf("%d", &item);
//taking a user input for inserting into queue
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->data = item;
rear->next = NULL;
front = rear;
}
else
{
rear->next = (struct node *)malloc(sizeof(struct node));
rear = rear->next;
rear->data = item;
rear->next = NULL;
}
}
/*Function to remove an element from the queue*/
void dequeue()
{
Int item;

struct node *ptr;


if(front == NULL)
printf("Queue is empty");
else
{
ptr = front;
item = front->data;
front = front->next;
free(ptr);
printf("\nItem deleted: %d\n", item);
if(front == NULL)
rear = NULL;
}
}

You might also like