Print integers in reverse order using LinkedLists

| December 15, 2010 | 0 Comments

Scenario:

You need to make program in C that can accept numbers from user and print integers in reverse order using LinkedLists.

Solution:

There are three functions:

  • Insert – Insert each number or integer into LinkedList from user.
  • Reverse – Reverse each number in linkedlist starting from first number in linkedlist.
  • PrintList – Print each number in linkedlist to output screen.

For rest check out code snippet provided below.

Code Snippet:

# include
# include
# include  /* For LinkedList */

// Designing struct named "node"
struct node
{
 int data;
 struct node *link; // link pointer pointing to struct node
};

struct node *insert(struct node *p, int n)
{
 struct node *temp; // temp pointer pointing to struct node

 // if the existing list is empty then insert a new node as the
 // starting node
 if(p==NULL)
 {
  // creates new node data value passes as parameter
  p=(struct node *)malloc(sizeof(struct node));

  if(p==NULL)
  {
   printf("Error\n");
   exit(0);
  }

  p-> data = n; // p pointer pointing to data of struct start
  p-> link = NULL;
 }
 else
 {
  temp = p;

  // traverses the existing list to get the pointer to the last node of it
  while (temp-> link != NULL)
  temp = temp-> link;
  temp-> link = (struct node *)malloc(sizeof(struct node));

  //creates new node using data value passes as parameter and puts its
  //address in the link field of last node of the existing list
  if(temp -> link == NULL)
  {
   printf("Error\n");
   exit(0);
  }

  temp = temp-> link;
  temp-> data = n;
  temp-> link = NULL;
 }
 return (p);
}

// Function for reversing integer list
struct node *reverse(struct node *p)
{
    struct node *prev, *curr;
 prev = NULL;
 curr = p;
 while (curr != NULL)
 {
  p = p-> link;
  curr-> link = prev;
  prev = curr;
  curr = p;
 }
 return(prev);
}

// Function to print on screen
void printlist ( struct node *p )
{
 while(p!= NULL)
 {
  printf("%d\n",p->data);
  p=p->link;
 }
}

// Main function
void main()
{
 int n;
 int x;

 struct node *start = NULL ;
 printf("Enter the number of nodes to create: \n");
 scanf("%d",&n);
 while ( n-->0 )
 {
  printf( "Enter the data values to be placed in a node\n");
  scanf("%d",&x);
  start = insert ( start, x );
 }

 printf("\nDETAILS OF CREATED LIST:\n");
 printlist (start);
 start=reverse(start);
 printf("\nREVERSE LIST OF NUMBERS:\n");
    printlist(start);
}

Tags:

Category: .NET

Leave a Reply

%d bloggers like this:
tt.term_id WHERE tt.taxonomy = 'nav_menu' AND t.term_id = 32 LIMIT 1 66 | 0.0006 | enabled | not cached | 897 | SELECT tr.object_id FROM wp_term_relationships AS tr INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('nav_menu') AND tt.term_id IN ('32') ORDER BY tr.object_id ASC 67 | 0.0006 | enabled | not cached | 11135 | SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (225,226,227,228,229) AND wp_posts.post_type = 'nav_menu_item' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.menu_order ASC 68 | 0.0005 | enabled | not cached | 2087 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (226) ORDER BY meta_id ASC 69 | 0.0003 | enabled | not cached | 2087 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (229) ORDER BY meta_id ASC 70 | 0.0005 | enabled | not cached | 2087 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (227) ORDER BY meta_id ASC 71 | 0.0005 | enabled | not cached | 2087 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (225) ORDER BY meta_id ASC 72 | 0.0004 | enabled | not cached | 2087 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (228) ORDER BY meta_id ASC 73 | 0.0007 | enabled | not cached | 4495 | SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('category') AND t.term_id IN ( 4,8,9,2,3 ) ORDER BY t.name ASC 74 | 0.0002 | enabled | cached | 736 | SELECT t.term_id FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category') AND tr.object_id IN (10) ORDER BY t.name ASC 75 | 0.0003 | enabled | not cached | 529 | SELECT option_value FROM wp_options WHERE option_name = 'text_direction' LIMIT 1 76 | 0.0007 | enabled | not cached | 3551 | SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('post_tag') AND tr.object_id IN (10) ORDER BY t.name ASC 77 | 0.0007 | enabled | not cached | 3555 | SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category') AND tr.object_id IN (10) ORDER BY t.name ASC 78 | 0.0005 | enabled | not cached | 4903 | SELECT * FROM wp_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 10 ORDER BY comment_date_gmt ASC 79 | 0.0004 | enabled | not cached | 632 | SELECT p.ID FROM wp_posts AS p WHERE p.post_date < '2010-12-15 12:29:00' AND p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_date DESC LIMIT 1 80 | 0.0004 | enabled | not cached | 8666 | SELECT * FROM wp_posts WHERE ID = 11 LIMIT 1 81 | 0.0004 | enabled | not cached | 630 | SELECT p.ID FROM wp_posts AS p WHERE p.post_date > '2010-12-15 12:29:00' AND p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_date ASC LIMIT 1 82 | 0.0003 | enabled | not cached | 9221 | SELECT * FROM wp_posts WHERE ID = 9 LIMIT 1 83 | 0.0012 | enabled | not cached | 3091 | SELECT p.ID AS 'id', p.post_title AS 'title', p.post_date AS 'date', p.post_author AS 'uid', ( SUM(v.pageviews)/(IF ( DATEDIFF('2017-02-01 01:10:05', DATE_SUB('2017-02-01 01:10:05', INTERVAL 1 WEEK)) > 0, DATEDIFF('2017-02-01 01:10:05', DATE_SUB('2017-02-01 01:10:05', INTERVAL 1 WEEK)), 1) ) ) AS 'avg_views' FROM wp_popularpostssummary v LEFT JOIN wp_posts p ON v.postid = p.ID WHERE 1 = 1 AND p.post_type IN('post','page') AND v.last_viewed > DATE_SUB('2017-02-01 01:10:05', INTERVAL 1 WEEK) AND p.post_password = '' AND p.post_status = 'publish' GROUP BY v.postid ORDER BY avg_views DESC LIMIT 5; 84 | 0.0002 | enabled | not cached | 11898 | SELECT * FROM wp_posts WHERE ID = 1 LIMIT 1 85 | 0.0005 | enabled | not cached | 9642 | SELECT * FROM wp_posts WHERE ID = 26 LIMIT 1 86 | 0.0006 | enabled | not cached | 15475 | SELECT * FROM wp_posts WHERE ID = 22 LIMIT 1 87 | 0.0005 | enabled | not cached | 10190 | SELECT * FROM wp_posts WHERE ID = 20 LIMIT 1 88 | 0.0006 | enabled | not cached | 16352 | SELECT * FROM wp_posts WHERE ID = 38 LIMIT 1 89 | 0.0006 | disabled (query) | not cached | 0 | SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_date DESC LIMIT 0, 5 90 | 0.0001 | disabled (query) | not cached | 0 | SELECT FOUND_ROWS() 91 | 0.0004 | enabled | not cached | 23663 | SELECT wp_posts.* FROM wp_posts WHERE ID IN (353,339,2,3) 92 | 0.001 | enabled | not cached | 6478 | SELECT t.*, tt.*, tr.object_id FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category', 'post_tag', 'post_format') AND tr.object_id IN (2, 3, 339, 353) ORDER BY t.name ASC 93 | 0.0012 | enabled | not cached | 10510 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (2,3,339,353) ORDER BY meta_id ASC 94 | 0.0023 | enabled | not cached | 7307 | SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,100) AS com_excerpt FROM wp_comments LEFT OUTER JOIN wp_posts ON (wp_comments.comment_post_ID = wp_posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 5 95 | 0.0004 | enabled | not cached | 10061 | SELECT * FROM wp_posts WHERE ID = 15 LIMIT 1 96 | 0.0005 | enabled | not cached | 14705 | SELECT * FROM wp_posts WHERE ID = 42 LIMIT 1 97 | 0.0007 | enabled | not cached | 2074 | SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC 98 | 0.0002 | enabled | cached | 4714 | SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('category') ORDER BY t.name ASC 99 | 0.001 | enabled | not cached | 8853 | SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('post_tag') AND tt.count > 0 ORDER BY tt.count DESC LIMIT 200 100 | 0.0004 | enabled | not cached | 3470 | SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = 'nav_menu' AND t.term_id = 33 LIMIT 1 101 | 0.0003 | enabled | not cached | 946 | SELECT tr.object_id FROM wp_term_relationships AS tr INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('nav_menu') AND tt.term_id IN ('33') ORDER BY tr.object_id ASC 102 | 0.0005 | enabled | not cached | 11910 | SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (231,232,234,244,289,306) AND wp_posts.post_type = 'nav_menu_item' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.menu_order ASC 103 | 0.0004 | enabled | not cached | 2109 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (234) ORDER BY meta_id ASC 104 | 0.0004 | enabled | not cached | 2092 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (231) ORDER BY meta_id ASC 105 | 0.0004 | enabled | not cached | 2086 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (232) ORDER BY meta_id ASC 106 | 0.0005 | enabled | not cached | 2086 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (289) ORDER BY meta_id ASC 107 | 0.0005 | enabled | not cached | 2086 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (244) ORDER BY meta_id ASC 108 | 0.0005 | enabled | not cached | 2086 | SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (306) ORDER BY meta_id ASC 109 | 0.0008 | enabled | not cached | 26416 | SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (207,209,279,239,301) AND wp_posts.post_type = 'page' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC 110 | 0.0002 | enabled | cached | 736 | SELECT t.term_id FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category') AND tr.object_id IN (10) ORDER BY t.name ASC 111 | 0.0002 | enabled | cached | 559 | select meta_key from wp_postmeta where meta_key like '_wp_page_template' and meta_value like 'page-youtube.php' 112 | 0.0001 | enabled | cached | 625 | select meta_key from wp_postmeta where meta_key like '_wp_page_template' and meta_value like 'page-portfolio.php' 113 | 0.0001 | enabled | cached | 559 | select meta_key from wp_postmeta where meta_key like '_wp_page_template' and meta_value like 'page-youtube.php' 114 | 0.0001 | enabled | cached | 625 | select meta_key from wp_postmeta where meta_key like '_wp_page_template' and meta_value like 'page-portfolio.php' -->