任何正确的此队列程序 [英] any correct this queue program

查看:64
本文介绍了任何正确的此队列程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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]缩进的代码块-OriginalGriff [/edit]



[edit]Code block indented - OriginalGriff[/edit]

推荐答案

您的代码很混乱.
试试:
Your code is messy.
Try:
#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");
  }
}


这篇关于任何正确的此队列程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆