Click here to Skip to main content
15,916,442 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include<stdio.h>
#include<conio.h>

// creating structure
struct node
  {
  int data;
  struct node *next;  // this variable is refering the struct node
  }*front=NULL,*temp,*get,*last=NULL;

int main()
  {
  //front = NULL;
  clrscr();
  // insert the queue element
  // insert function declaration
  add(&front,&last,56);
  add(&front,&last,88);
  add(&front,&last,44);
  add(&front,&last,99);
  add(&front,&last,100);
  add(&front,&last,23);
  
  // display the queue element
  display(front);
  
  //pop function
  pop(&front);
  pop(&front);
  
  getch();
  return 0;
  }

//definition of the insert function
add(struct node *temp,struct node *last,int val)
  {
  temp = (struct node *)malloc(sizeof(struct node));
  //ne = (temp *) malloc(40*sizeof(temp));
  
  if(front == NULL)
    {
    temp->data = val;
    temp->next = NULL;
    front = temp;
    printf("added stack value is %d\n",temp->data);
    }
  else
    //struct node *p;
    {
    get = temp;
    get->data = val;
    last->next = temp;
    printf("added stack value is %d\n",get->data);
    }
  last = temp;
  }

display(front)
  {
  int count=0;
  while(front->next != NULL)
    {
    printf("%d\n",front -> data);
    count++;
    }
  printf("No of Nodes: %d\n",count);
  }


/* pop()
  {
  struct node *del;
  del = front;
  if(front->next == NULL)
    {
    printf("Deleted node value: %d",front->data);
    free(front);
    front = NULL;
    }
  else
    {
    while(del->next != NULL)
      {
      temp = del;
      del = del->next;
      }
    printf("deleted element is: %d\n",del->data);
    temp->next = NULL;
    free(del);
    }
  }
*/

pop(struct node *front)
  {
  struct node *del;
  int d;
  del = front;
  if(del->next == NULL)
    {
    printf("\n\nDeleted node value: %d",del->data);
    free(del);
    del = NULL;
    }
  else
    {
    //temp = del;
    d= del->data;
    //front = del->next;
    front = del->next;
    printf("\n\ndeleted : %d\n",d);
    //temp->next = NULL;
    free(del);
    }
  }


[edit]Code block indented - OriginalGriff[/edit]
Posted
Updated 6-Oct-11 22:30pm
v2
Comments
CodingLover 7-Oct-11 2:34am    
What is your problem here?
OriginalGriff 7-Oct-11 4:33am    
Rules of posting questions (and indeed of programming):
1) INDENT YOUR CODE. Do you see how much easier it is to work out what is happening in your code when the blocks are indented as above? It makes it easier for you, it makes it easier for us. Win-win.
2) When you want help, it is a very, very good idea to describe what the problem is. Just going "fix this" doesn't help - we don't know what you consider to be the problem.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Your code is messy.
Try:
C
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node;
void add(int val);

void display();
void pop();

// creating structure
struct node
{
  int data;
  struct node *next;  // this variable is refering the struct node
} *front=NULL;

int main()
{
  // insert the queue element
  // insert function declaration

  add(56);
  add(88);
  add(44);
  add(99);
  add(100);
  add(23);

  // display the queue element
  display();

  pop();
  pop();

  printf("after 2 pops\n");
  display();

  // cleanup
  while (front) pop();

  getchar();
  return 0;
}

//definition of the insert function
void add(int val)
{
  struct node * temp = (struct node *)malloc(sizeof(struct node));

  assert(temp);

  temp->data = val;
  temp->next = NULL;
  if ( ! front )
  {
    front = temp;
  }
  else
  {
    struct node * p = front;
    while ( p->next )
    {
      p = p->next;
    }
    p->next = temp;
  }
}

void display()
{
  int count=0;
  struct node * temp = front;
  while(temp)
  {
    printf("%d\n", temp->data);
    count++;
    temp = temp->next;
  }
  printf("No of Nodes: %d\n",count);
}

void pop()
{
  struct node *temp;
  temp = front;
  if ( temp )
  {
    front = temp->next;
    printf("\nDeleting node value: %d\n", temp->data);
    free(temp);
  }
  else
  {
    printf("\nEmpty queue\n");
  }
}
 
Share this answer
 
Comments
kschandru 7-Oct-11 10:08am    
hi friend.
I am the beginner of c.and i have one doubt in assert(temp); what the purpose we use this function and what is assert? and in "add and Display" function explain the while condition friend plz.
kschandru 7-Oct-11 11:00am    
and one more in "add" function explain the if(!front) condition...
CPallini 7-Oct-11 12:09pm    
1. assert(temp), in debug build, simply stops the execution if temp == 0 i.e. if memory allocation failed. In my code is just a placeholder for a better handling of this (possible) failure.
2. while (temp->next) executes the loop while there are 'next' elements in the queue, i.e. current item have a (not null) next one.
3.while(temp) (together with temp = temp->next) iterates over all valid items (not null) of the queue.
4.In add function, iteration should start from front. However, if the queue is still empty, front is null and hence we should set front itself to point the newly added element.
kschandru 8-Oct-11 10:01am    
del(int num)
{
struct node *tmp,*q;
if(start->info==num)
{
tmp=start;
start=start->next; /*first element deleted*/
start->prev = NULL;
free(tmp);
return;
}
q=start;
while(q->next->next!=NULL)
{
if(q->next->info==num) /*Element deleted in between*/
{
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
free(tmp);
return;
}
q=q->next;
}
if(q->next->info==num) /*last element deleted*/
{ tmp=q->next;
free(tmp);
q->next=NULL;
return;
}
printf("Element %d not found\n",num);
}/*End of del()*/

hello friend
im trying to do doubly linked list program i saw the code in"http://thecodecracker.com/c-programming/double-linked-list/" this site but i did't under stand the delete operation but graphical vice i under stand the concept but some command i could't under stand plz help me friend. plz explain line by line plz plz...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900