Tags » Linked List

Q107: Linked list with a cycle

Question #107: Following linked list is an example of linked list with a cycle.

Which of the following is a method to detect if there is a loop in a linked list of unknown size? 124 more words

GATE

Linked list remove all duplicates

#include <stdio.h>
#include <stdlib.h>

struct node {
    struct node *next;
    int value;
};
typedef struct node node_t;

void removeallduplicates(node_t *head) {
    node_t *ptr1 = head;
    node_t *current;
    node_t *prev;
    node_t *deleteit;
    while (ptr1 != NULL) {
        current = ptr1->next;
        prev = ptr1;
        while (current != NULL) {
            if (current->value == ptr1->value) {
                deleteit = current;
                prev->next = current->next;
                current = current->next;
                free(deleteit);
            } else {
                prev = current;
                current = current->next;
            }
        }
        ptr1 = ptr1->next;
    }
}

void push(node_t **head, int value) {
    node_t * newnode = malloc(sizeof(node_t));
    newnode->value = value;
    newnode->next = *head;
    *head = newnode;  // newnode becomes the new head
}

void printlist(node_t *head) {
    node_t *ptr = head;
    while (ptr != NULL) {
        printf("%d, ", ptr->value);
        ptr = ptr->next;
    }
    printf("\n");
}

int main() {

    node_t * headptr = NULL;
    int numbs[] = {1,2,3,4,5,1,5,2,4,3};
    int i;
    for (i = 0; i < 10; i++)
        push(&headptr, numbs[i]);
    printlist(headptr);

    removeallduplicates(headptr);
    printlist(headptr);

    return 0;
}


>  gcc linkedlistremovedup.c  -g
> ./a.out
3, 4, 2, 5, 1, 5, 4, 3, 2, 1,
3, 4, 2, 5, 1,

Common C Interview Questions

Linked list functions with dummy head and without it

Some linked list routines need to modify the head node (first node) of the list, if for example the head node is a new node, or the head node needs to be removed. 552 more words

Common C Interview Questions

Queue (code):

<br />#include<iostream>
#include<conio.h>
using namespace std;

struct queue
{
int data;
struct queue *link;
}*front,*rear,*temp,*ptr;

void insert()
{
int ele;
cout<<"Enter element to be inserted:"<<endl;
cin>>ele;
temp=new queue;
if(temp==NULL)
cout<<"Out of Memory !! 252 more words
Data Structure

Removing duplicate elements from a sorted linked list

In an earlier post, Removing duplicate elements from unsorted linked list

I described how to remove duplicate elements from an unsorted linked list. In this post I will explain how to remove duplicate elements from… 418 more words

Javascript

queue using linked list

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>

struct qnode // create structure
{
int data; //to store data
qnode *next; //pointer to next element
}*top=NULL,*front=NULL; //top(rear) and front initialised to zero… 196 more words

Data Structure

Reverse a linked list

How to reverse a linked list is one of the most commonly asked data structures interview
questions. Here we have both an iterative and a recursive solution for this problem. 79 more words
Linked List