Jump to content

Linked list

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
BINNY88

BINNY88

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
I want a program to perform insertion & deletion in a linked list in c language.
can anyone help?.I have this program but it doesnt seem to work.I use a dev c++ compiler

Attached Files



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
  int a;
  struct node *next;
};

struct node *create()
{
  struct node *p,*r,*n;
  int s,k;
  s=sizeof(struct node);
  printf("Enter the element -1 to stop : ");
  scanf("%d",&k);
  p=r=NULL;
  while(k!=-1)
  {
    n=(struct node *)malloc(s);
    n->a = k;
    n->next = NULL;
    if(r==NULL)
     r=n;
    else
    p->next = n;
    p=n;
    printf("Enter the element -1 to stop");
    scanf("%",&k);
  }     
  return(r);
}
void display(r);
struct node *r;
{
  printf("\nroot  ");
  while(r!=NULL)
  {
    printf("\t%d",r->a)a;
    r=r->next;
  }
  print("null");
}

main()
{
  struct node *r;
  clrscr();
  r=create();
  display(r);
  getch();
}

First problem: in your display function, you are using the variable r as a parameter and an internal variable. Odds are that you are completely scrambling your list in there.

Second problem: your variable names make it hard to interpret the purpose of each pointer. When working with a linked list, you want a head pointer and a current pointer. The head pointer should NEVER be reassigned. That said, your create function appears to work properly.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
BINNY88

BINNY88

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Thanks a lot!

#4
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
I had a very good implementation written, unfortunately my OS crashed recently and I lost it. If I find the time to re-write it, I will post it in this thread. I'm kind of sick at the moment so it may be a while. Check back often.

EDIT: Ok, so I found the "solutions" to my old LinkedList assignment, basically identical to what I initially wrote, but still, I do not take full credit for this, a lot of it comes from the Stanford CS library.

LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <stdlib.h>
#include <assert.h>

struct node {
    int data;
    struct node* next;
};

int Count(struct node *head, int searchFor);
int GetNth(struct node* head, int index);
void DeleteList(struct node** headRef);
int Pop(struct node** headRef);
int Length(struct node* head);
void Push(struct node** headRef, int newData);
struct node* BuildOneTwoThree(void);

#endif


LinkedList.c
#include "LinkedList.h"

int main() {
    printf("Put your program implementation here.\n\n");
    system("PAUSE");
    return 0;
}

int Count(struct node *head, int searchFor)
{
	struct node *p;
	int count = 0;

	for (p = head; p != NULL; p = p->next) {
		if (p->data == searchFor) {
			count++;
		}
	}
	return count;
}

int GetNth(struct node* head, int index)
{
    struct node* current = head;
    int count = 0;
    while (current != NULL) {
        if (count == index) return(current->data);
        count++;
        current = current->next;
    }
    assert(0);
}

void DeleteList(struct node** headRef)
{
    struct node* current = *headRef;
    struct node* next;
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
    *headRef = NULL;
}

int Pop(struct node** headRef)
{
    struct node* head;
    int result;
    head = *headRef;
    assert(head != NULL);
    result = head->data;
    *headRef = head->next;
    free(head);
    return(result);
}

int Length(struct node* head) {
	int count = 0;
	struct node* current = head;
	while (current != NULL) {
	   count++;
	   current=current->next;
	}
	return(count);
}

void Push(struct node** headRef, int newData) {
	struct node* newNode = malloc(sizeof(struct node));
	newNode->data = newData;
	newNode->next = (*headRef);
	(*headRef) = newNode;
}

struct node* BuildOneTwoThree(void) {
	struct node* head = NULL;
	Push(&head, 3);
	Push(&head, 2);
	Push(&head, 1);
	return(head);
}


Obviously there are many, many more function that you could implement, as I did when I wrote my linked list assignment. Remove duplicates, merge lists, reverse a list, turn the list into a stack, etc... Look up linked lists on a search engine and try to find ideas for your own implementations!

Hope this helps.

EDIT: Forgot to mention, although I included the standard library, and the assert header file, you may still get compile errors, depending on your compiler. If you do, try including the stdio.h library as well. If all else fails, download Dev-cpp and run the code from there.

Edited by Steve.L, 02 October 2008 - 02:36 PM.


#5
BINNY88

BINNY88

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Thanks steve.Its a very good implementation of pointers.

#6
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
No problem.