Data Structure

3 views
Skip to first unread message

Sourabh Shastri

unread,
Sep 20, 2013, 6:35:13 AM9/20/13
to mca2k5
How to reverse a linked list ?
--
Regards
SOURABH SHASTRI (Assistant Professor)
I/C Head
Department of Computer Science & I.T.
Bhaderwah Campus
University of Jammu
+91 94191 71476

amit sharma

unread,
Sep 20, 2013, 9:14:29 AM9/20/13
to Mca2K5 Group

#include <stdio.h>

typedef struct Node {
  char data;
  struct Node* next;
} Node;

void print_list(Node* root) {
  while (root) {
    printf("%c ", root->data);
    root = root->next;
  }
  printf("\n");
}

Node* reverse(Node* root) {
  Node* new_root = 0;
  while (root) {
    Node* next = root->next;
    root->next = new_root;
    new_root = root;
    root = next;
  }
  return new_root;
}

int main() {
  Node d = { 'd', 0 };
  Node c = { 'c', &d };
  Node b = { 'b', &c };
  Node a = { 'a', &b };

  Node* root = &a;
  print_list(root);
  root = reverse(root);
  print_list(root);

  return 0;
}


--------------------------------------------------------------------------------------------------------------------------------
Check out my professional profile and connect with me on LinkedIn. http://lnkd.in/dwjYrV



--
You received this message because you are subscribed to the Google Groups "jammuuniversityMCA2k5" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jammuuniversitym...@googlegroups.com.
To post to this group, send an email to jammuunive...@googlegroups.com.
Visit this group at http://groups.google.com/group/jammuuniversitymca2k5.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages