Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct node
- {
- int data;
- struct node *next;
- struct node *prev;
- };
- struct node *GetNewNode(int x);
- void InsertAtHead(int x);
- struct node *head;
- struct node *GetNewNode(int x){
- struct node *newNode= (struct node *)malloc(sizeof(struct node));
- newNode->data=x;
- newNode->next=NULL;
- newNode->prev=NULL;
- return newNode;
- }
- void InsertAtHead(int x){
- struct node *newNode = GetNewNode(x);
- if (head==NULL){
- head=newNode;
- return;
- }
- head->prev=newNode;
- newNode->next=head;
- head=newNode;
- }
- void print(){
- struct node *temp=head;
- printf("Forward: ");
- while (temp!=NULL){
- printf("%i " , temp->data);
- temp=temp->next;
- }
- printf("\n");
- }
- void printReverse(){
- struct node *temp=head;
- if (temp==NULL) return;
- while (temp->next!=NULL) temp=temp->next;
- printf("Reverse: ");
- while (temp!=NULL){
- printf("%i " , temp->data);
- temp=temp->prev;
- }
- printf("\n");
- }
- int main()
- {
- InsertAtHead(5);
- InsertAtHead(8);
- InsertAtHead(9);
- print();
- printReverse();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement