Linked List Problems and Solutions, Lab Reports of Computer Science

Solutions to various problems related to linked lists. It includes functions to delete the first node, add a node to the end, copy a list, add a node at a specific position, remove the first element, count duplicate items, count nodes in a circular list, and reverse a list using a stack. The document also includes code snippets for each function.

Typology: Lab Reports

Pre 2010

Uploaded on 11/08/2009

koofers-user-b8c
koofers-user-b8c 🇺🇸

5

(3)

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Labsession Linked list problems-II
Assume the following node structure:
struct node {
int data;
struct node *next;
}
1. Given a linked list pointed by *A, write a function trim which deletes the first node.
In all linked list problems, always take care of the situation that the list may have only
one node or that the list may be empty.
struct node *trim(struct node *A)
2. Repeat problem 1 by writing the following function:
void trim(struct node * *A)
3. Given a linked list *B, write a function to add 10 to the last element.
struct node *add_last(struct node *B)
4. A linked list contains n nodes. Write a function which returns the data stored in the ith
node in response to the following call from the main program (0<i<=n)
value = content (pList, i );
int contents(struct node* list, int index)
{
int ii;
if (ii > 1) {
for( ii = 1; ii<index; ii++)
list = list->next;
}
return list->data;
}
5. Given a linked list *First, it is required to create a copy of the list and call it *Second.
The call from the main program would be
pf3
pf4
pf5

Partial preview of the text

Download Linked List Problems and Solutions and more Lab Reports Computer Science in PDF only on Docsity!

Labsession Linked list problems-II

Assume the following node structure: struct node { int data; struct node *next; }

  1. Given a linked list pointed by *A, write a function trim which deletes the first node. In all linked list problems, always take care of the situation that the list may have only one node or that the list may be empty. struct node *trim(struct node *A)
  2. Repeat problem 1 by writing the following function: void trim(struct node * *A)
  3. Given a linked list *B, write a function to add 10 to the last element. struct node *add_last(struct node *B)
  4. A linked list contains n nodes. Write a function which returns the data stored in the ith node in response to the following call from the main program (0<i<=n) value = content (pList, i ); int contents(struct node* list, int index) { int ii; if (ii > 1) { for( ii = 1; ii<index; ii++) list = list->next; } return list->data; }
  5. Given a linked list *First, it is required to create a copy of the list and call it *Second. The call from the main program would be

Second = copy(First); Write the following function: struct node *copy(struct node *list )

  1. Given a list *job, it is desired to add a node *A22 at the end of the list through the following call job = add(job,A22); Write a function to carry it out.
  2. A list contains pointer to the first node as well as to the last node. The following call is supposed to remove the first element of the list. First_go(struct node &first, struct node &last); Write the appropriate function. Take care of all possible situations. void First_go(struct nodeqfront, struct nodeqback) { struct node* temp; if (*qfront!= NULL) { temp = *qfront; qfront = temp->next; if ((qfront) == NULL) *qback = NULL; free (temp); } return d; }
  3. Given a linked list pList, whose elements are in non-decreasing order, count the number of duplicate items present in the list. If the list is 12,23,23,45,67,67,67,67,82, it should print out count = 4. pCur= pList; while(pCur->next !=NULL) { if(pCur->data == pCur->next->data) { count++; afternext = pCur->next->next; pCur->next = afternext; } else pCur = pCur->next; }

_______________ ;

Solution: void extend(struct node *p, struct node * q) { struct node t; / t is used to traverse the lists / if ( __ q == NULL ____________ ) / if standard linked list is empty / return; t = p; / Assume p cannot be NULL / while ( t->next != p ) /traverse the circular list / t = t ->next ; t->next = q _________; / append the standard list / while ( t->next != NULL ) / traverse the std. list */ t = t->next ; t->next = _ p ;

  1. Reverse a given list, making use of a stack. Use the stack functions done in the class. (Stack code given on the webpage.) Let A be the given list. Put the nodes of the stack in stackA. Then pop them up one by one and append to another list B. struct node* stackA = (struct node*) (malloc(sizeof(struct node)));

struct node* front = (struct node) (malloc(sizeof(struct node))); struct node back = (struct node*) (malloc(sizeof(struct node))); stackA = NULL; pCur =A; while(pCur!=NULL) { dd = pCur->data; push(&stackA, dd); pCur=pCur->next; } front=NULL; back=NULL; //Now pop the nodes from stackA and append to a new list B do { dd = pop(&stackA); enqueue(&front,dd,&back); } while (stackA !=NULL);