Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node
- {
- node* next;
- char* key;
- };
- struct list
- {
- node* first;
- int size;
- };
- list* createList()
- {
- list* l = (list*)malloc(sizeof(list));
- l->first = NULL;
- l->size = 0;
- return l;
- }
- node* createNode(char *k)
- {
- node* n = (node*)malloc(sizeof(node));
- n->next = NULL;
- n->key = k;
- return n;
- }
- void addFirst(list* l, char* key)
- {
- node* x = createNode(key);
- if (l->size == 0)
- {
- l->first = x;
- }
- else
- {
- x->next = l->first;
- l->first = x;
- }
- l->size++;
- }
- void deleteList(list* l)
- {
- node* p = l->first;
- node* temporary;
- while (p->next!=NULL)
- {
- temporary=p->next;
- p->key=NULL;
- p->next=NULL;
- l->size--;
- free(p);
- p=temporary;
- }
- l->size=0;
- l->first=NULL;
- free(l);
- }
- void showList(struct list* l)
- {
- node* p = l->first;
- while (p!=NULL)
- {
- printf("%s ",p->key);
- p=p->next;
- }
- }
- void copyPlus(struct list* l,char *ch)
- {
- node* p = l->first;
- addFirst(l,ch);
- while (p!=NULL)
- {
- int len1=strlen(p->key);
- int len2=strlen(ch);
- char * cat = (char*)malloc(len1+len2+1);
- strcpy(cat,p->key);
- strcat(cat,ch);
- cat[len1+1] = '\0';
- printf("[%s] \n",cat);
- addFirst(l,cat);
- printf("{%s} \n",l->first->key);
- p=p->next;
- }
- }
- int main(void)
- {
- struct list *A=createList();
- addFirst(A,"1");
- addFirst(A,"2");
- addFirst(A,"4");
- copyPlus(A,"3");
- printf("=%s= \n",A->first->key); //this one works!
- printf("=%s= \n",A->first->next->key);
- printf("=%s= \n",A->first->next->next->key);
- showList(A);
- deleteList(A);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement