Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define TYPE int
- typedef struct Node{
- TYPE data;
- struct Node *next;
- struct Node *back;
- }Node;
- void initialize(Node *&head){ head=NULL; }
- void addToStart(Node *&head,TYPE val){
- Node *newNode = (Node*) malloc(sizeof(Node));
- newNode->next = head;
- newNode->back = NULL;
- newNode->data = val;
- head = newNode;
- }
- void addToLast(Node *&head,TYPE val){
- Node *newNode = (Node*) malloc(sizeof(Node));
- newNode->next = NULL;
- newNode->data = val;
- if(!head) {
- head = newNode;
- newNode->back = NULL;
- }else{
- Node *p = head;
- while(p->next) p = p->next; // enta wa2f 3nd el a5er
- newNode->back = p;
- p->next = newNode;
- }
- }
- void display(Node *head){
- if(!head)
- printf("EMPTY LIST.\n");
- else{
- printf("List: ");
- while(head){
- printf("[%d]->",head->data );
- head = head->next;
- }
- printf("\b\b \n");
- }
- }
- void deleteFromStart(Node *&head){
- (head->next)->back = NULL;
- Node *node = head;
- head = head->next;
- free(node);
- }
- void buildList(Node *&head,int lim){
- int i; for(i=0;i<=lim;i++) addToStart(head,i);
- }
- int main(){
- system("CLS"); printf("\n");
- Node *list; initialize(list);
- buildList(list,5);
- display(list);
- deleteFromStart(list);
- display(list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment