cpppro

Archive for the ‘Data Structures in C/C++’ Category

C/C++ Interview algorithms

In Algorithms, Data Structures in C/C++, Question Patterns on April 17, 2007 at 10:35 pm

1. Reverse a singly linked list

//
// iterative version
//
Node* ReverseList( Node ** List )
{
  Node *temp1 = *List;
  Node * temp2 = NULL;
  Node * temp3 = NULL;

  while ( temp1 )
  {
    //set the head to last node
    *List = temp1;

    // save the next ptr in temp2
    temp2= temp1->pNext;

    // change next to privous
    temp1->pNext = temp3;
    temp3 = temp1;
    temp1 = temp2;
  }

  return *List;
}

2. Delete a node in double linked list

void deleteNode(node *n)
{
  node *np = n->prev;
  node *nn = n->next;
  np->next = n->next;
  nn->prev = n->prev;
  delete n;
}

3. Sort a linked list

//sorting in descending order
struct node
{
  int value;
  node* NEXT;
}
//Assume HEAD pointer denotes the first
//element in the linked list
//only change the values? don't have to
//change the pointers
Sort( Node *Head)
{
  node* first,second,temp;
  first= Head;
  while(first!=null)
  {
    second=first->NEXT;
    while(second!=null)
    {
      if(first->value value)
      {
        temp = new node();
        temp->value=first->value;
        first->value=second->value;
        second->value=temp->value;
        delete temp;
      }
      second=second->NEXT;
    }
    first=first->NEXT;
  }
}

4. Reverse a string

void ReverseString (char *String)
{
  char *Begin = String;
  char *End = String + strlen(String) - 1;
  char TempChar;

  while (Begin < End)
  {
    TempChar = *Begin;
    *Begin = *End;
    *End = TempChar;
    Begin++;
    End- -;
  }
}

5. Insert a node a sorted linked list

void sortedInsert(Node * head, Node* newNode)
{
  Node *current = head;

  // traverse the list until you find
  //item bigger the new node value
  //
  while ((current!= NULL) &&
         (current->data data))
  {
    current = current->next;
  }
  //
  // insert the new node before the big item
  //
  newNode->next = current->next;
  current = newNode;
}

6. Covert a string to upper case

void ToUpper(char * S)
{
  while (*S!=0)
  {
    *S=(*S >= 'a' && *S <= 'z')?(*S-'a'+'A'):*S;
    S++;
  }
}

7. Multiple a number by 7 without using * and + operator.

Linked Lists

In C Tidbits, Data Structures in C/C++, Pointers & Callbacks, Tutorials on April 12, 2007 at 2:55 pm

Linked Lists Basics
[Download this tutorial as linked lists tutorial , for updated conetnt and comments, generate a new pdf with the option which is present below the post]

What are Linked Lists?
Linked lists store collections of data like arrays.  Linked lists are chain of records/nodes, one record/node points to the next. Record holds the data.

Why Linked Lists?
There are several  disadvantages with arrays, here are some…
1) The size of the array is fixed. Most often this size is specified at compile time however the size of the array can be deferred until the array is created at runtime (from heap), but after that it remains fixed. This causes to waste memory eventhough e may not use.
2) Inserting new elements at the front is potentially expensive because existing elements need to be shifted over to make room.
3)When array was full, to insert more data, it need to be resized, this operation is quite expensive, even may not be possible if in case memory got fragmented.
Linked lists performs well where arrays fail to do it.
Please refer http://www.refcode.net/2013/02/linked-lists.html

 

Sorting A Linked List with Bubble Sort

In C Tidbits, Data Structures in C/C++, Pointers & Callbacks on April 11, 2007 at 10:01 pm

The basic idea of this sorting algorithm is to
compare two neighboring objects, and to swap them if
they are in the wrong order.

here is an example, which shows sorted list in several passes..

5   10   20   13   7   17
5   10   13   7    17  20
5   10   7    13   17  20
5   7    10   13   17  20
5   7    10   13   17  20

Here’s a snippet of C code for bubble sort for sorting an array:

Please refer http://www.refcode.net/2013/02/sorting-linked-list-with-bubble-sort.html

Reversing a double linked list

In Algorithms, C Tidbits, Data Structures in C/C++ on March 30, 2007 at 5:43 pm

refer double linked list post for information about double linked list. Here in this post we will be discussing about reversing a double linked list.

Here is the pictorial representation of our algo…
reversing double linked list

in our routine we will swap our next, prev pointers of nodes to reverse the double linked list… this routine capable of reversing entire list for given any node in the double linked list.

The below routine takes input as any node in double linked list and reverse the entire double linked list and returns same node pointer.

please refer http://www.refcode.net/2013/02/reversing-double-linked-list.html

Detecting broken links in double linked list

In C Tidbits, Data Structures in C/C++ on March 30, 2007 at 4:12 pm

Here is a routine which does find out broken links in double linked list, wondering what is a broken link…

 

here is the code snippet for detecting broken links in double linked list…

for complete operations (all operations) on double linked list, visit earlier post

please refer http://www.refcode.net/2013/02/detecting-broken-links-in-double-linked.html

Reversing a single linked list using stack

In Algorithms, C Tidbits, Data Structures in C/C++ on March 30, 2007 at 3:40 pm

Here is the routine for reversing single linked list using a stack, for reversing linked list using pointers, see the earlier post. For reversing linked list recursively visit this post. This routine takes a single linked list as argument and reverse it and returns back the head node to the reversed list.

please refer http://www.refcode.net/2013/02/reversing-single-linked-list-using-stack.html

Reversing single linked list recursively

In Algorithms, C Tidbits, Data Structures in C/C++ on March 30, 2007 at 3:30 pm

Here is the routine for reversing single linked list recursively, for reversing iteratively see the earlier post . For reversing circular single linked list visit this post. This routine takes a single linked list as argument and reverse it and returns back the head node to the reversed list.

 

please refer http://www.refcode.net/2013/02/reversing-single-linked-list-recursively.html

Reversing a circular single linked list using pointers

In C Tidbits, Data Structures in C/C++ on March 10, 2007 at 4:50 pm

Reversing a single linked list is explained in this post

this post deals with reversing circular single linked list, which is not much different from what we did there except the last reversed node points the head node, then we change the head node to point to reversed linked list….

You might be thinking why do we need to reverse a circular linked list when the list is circular, but as the list is single linked list here we are going to reverse the direction of the list.

here is the routine which reverses circular list….

please refer http://www.refcode.net/2013/02/reversing-circular-single-linked-list.html

Circular Doubly Linked List With Out Using Special Head or Tail Node

In C Tidbits, Data Structures in C/C++ on February 28, 2007 at 5:24 pm

Circular double linked list can be traversed clockwise or anticlockwise. In this list all nodes are linked as shown in the below figure. The only difference between double linked list and circular double linked list is last node points to first node in the list, first node points to last node instead of pointing to NULLs.

please refer http://www.refcode.net/2013/02/double-linked-lists.html

 

Data structures lectures

In Data Structures in C/C++ on December 22, 2006 at 1:33 pm
       

Date

Title
View archived webcast
Wed, 01/18 Course Introduction
View archived webcast
Fri, 01/20 Developing a Simple Program
View archived webcast
Mon, 01/23 Containers
View archived webcast
Wed, 01/25 Pointer Manipulation
View archived webcast
Fri, 01/27 Arrays and Objects
View archived webcast
Mon, 01/30 Arrays and Objects (con’t)
View archived webcast
Wed, 02/01 Java Library List Classes
View archived webcast
Fri, 02/03 Object-Oriented Mechanisms
View archived webcast
Mon, 02/06 Abstract Methods and Classes
View archived webcast
Wed, 02/08 Abstract Methods and Classes con’t.
View archived webcast
Fri, 02/10 Examples of Interfaces
View archived webcast
Mon, 02/13 More on constructors, Exceptions
View archived webcast
Wed, 02/15 Loose ends: Packages, imports, access control, nested classes, super, instanceof
View archived webcast
Fri, 02/17 Loose ends: Packages, imports, access control, nested classes, super, instanceof (con’t)
Mon, 02/20 President’s Day
View archived webcast
Wed, 02/22 Delagation and Wrappers
View archived webcast
Fri, 02/24 Algorithmic Analysis I
View archived webcast
Mon, 02/27 Data Structures
View archived webcast
Wed, 03/01 Collections overview
View archived webcast
Fri, 03/03 Collections Overview (con’t)
View archived webcast
Mon, 03/06 Trees
View archived webcast
Wed, 03/08 Trees (con’t)
View archived webcast
Fri, 03/10 Tree representation, searching
View archived webcast
Mon, 03/13 Generic Programming
View archived webcast
Wed, 03/15 Priority queues, range queries
View archived webcast
Fri, 03/17 Hashing
View archived webcast
Mon, 03/20 Sorting I
View archived webcast
Wed, 03/22 Sorting II
View archived webcast
Fri, 03/24 Balanced Search Structures I
Mon, 03/27 Spring Break
Wed, 03/29 Spring Break
Fri, 03/31 Spring Break
View archived webcast
Mon, 04/03 Balanced Search Structures
View archived webcast
Wed, 04/05 Balanced Search Structures 2
View archived webcast
Fri, 04/07 Pseudo-Random Sequences
View archived webcast
Mon, 04/10 Threads, Concurrency
View archived webcast
Wed, 04/12 Balanced Search Structures
View archived webcast
Fri, 04/14 Backtracking search, game trees
View archived webcast
Mon, 04/17 Search and Game Trees (con’t), Enumeration Types
View archived webcast
Wed, 04/19 Graphs, Introduction
View archived webcast
Fri, 04/21 Graphs, more Algorithms
View archived webcast
Mon, 04/24 Algorithms (con’t)
View archived webcast
Wed, 04/26 Dynamic Programming
View archived webcast
Fri, 04/28 Storage Management
View archived webcast
Mon, 05/01 Garbage Collection
View archived webcast
Wed, 05/03 GUI Interfaces
Fri, 05/05 No Webcast
View archived webcast
Mon, 05/08 Course Summary

 

Audio, video lectures on data structres

 [tags]audio/video lectures on data structres, data structres lectures, data structures[/tags]