I'm trying to make doubly linked list in c, only not quite understand the theory. The code is as follows:
I know that is not correct, what are my mistakes in the theory of lists and in the language?Code:typedef struct list { int num; struct list *pre; struct list *next; } LIST; void insert_later(LIST *L, int n) { LIST *aux; aux = (LIST *) malloc(sizeof(LIST)); aux->num = n; aux->next = L->next; aux->pre= *L; L->next = *aux; }
This might be better way to do it.
Hope this helps.Code:struct List { int data; List* head; List* tail; }; // struct List int append( List* list, int data ) { // // is valid list if ( 0 == list ) return -1; // // allocate new node List* tail = (List*)malloc( sizeof(List) ); if ( 0 == tail ) return -1; // // forward to last valid node for ( ; 0 != list->tail; list = list->tail ) { /* ... */ } // // set tail to newly allocated node list->tail = tail; // set head to old list tail and // tail to 0 so we can tell it is last node tail->data = data; tail->head = list; tail->tail = 0; return 0; } int main( int argc, char* argv[] ) { List list = {0}; for ( int it = 1; it < 5; ++it ) append( &list, it ); for ( List* it = &list; 0 != it->tail; it = it->tail ) printf( "List::data=%d\n", it->data ); // // clean up return 0; }
Code:std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;
I would like to create a list structure.
The first function I tried was to insert value at the end of the list. How do I insert the end of the list?
I find this code in internet:
But to execute the program show the message of the windows that the memory can not be readCode:#include <stdio.h> #include <stdlib.h> struct node { int info; struct node *right; struct node *left; }; typedef struct node *nodeptr; nodeptr getnode() { nodeptr p; p = (struct node*) malloc(sizeof(struct node)); return p; } nodeptr initialize(nodeptr S) { S = NULL; return S; } int empty(nodeptr S) { if(S==NULL) return 1; else return 0; } void insertright(nodeptr p, int num) { nodeptr q; nodeptr temp; q = getnode(); temp = getnode(); if(p==NULL) { q->info = num; q = p->right; q->left = p; } else { temp = p->right; p->right = q; q->left = p; q->right = temp; temp->left = q; } } void insertleft(nodeptr p, int num) { nodeptr q; nodeptr temp; q = getnode(); temp = getnode(); if(p==NULL) { p->info = num; q->left = p; p->right = q; } else { p->info = num; temp = q->right; q->right = p; q = p->left; p->right = temp; temp->left = p; } } void removenode(nodeptr p) { nodeptr q; q = p; p = p->right; p->left = NULL; free(q); } void display(nodeptr S) { nodeptr p1; p1 = S; printf("\n LINKED LIST \n"); while(p1!=NULL) { printf("%d \n",p1->info); p1 = p1->right; } } int main() { nodeptr mainnode; initialize(mainnode); insertright(mainnode,10); insertright(mainnode,20); insertright(mainnode,30); insertleft(mainnode,40); insertleft(mainnode,50); display(mainnode); return 0; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks