Skip to content

Instantly share code, notes, and snippets.

@pellaeon
Last active December 10, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pellaeon/4482240 to your computer and use it in GitHub Desktop.
Save pellaeon/4482240 to your computer and use it in GitHub Desktop.
/*
* =====================================================================================
*
* Filename: concatenate_lists.c
*
* Description: Concatenates 2 linked lists of characters
*
* Version: 1.0
* Created: 2013年01月07日 11時38分00秒
* Revision: none
* Compiler: gcc
*
* Author: Pellaeon Lin (),
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
struct listnode {
char data;
struct listnode *nextnode;
};
typedef struct listnode Listnode;
typedef Listnode *ListnodePtr;
int concatenate ( ListnodePtr *, ListnodePtr * );
int insertnode ( ListnodePtr *, char );
int printlist ( ListnodePtr *);
int main () {
// Arrays used for initializing lists
char list1[5] = { 'A', 'B', 'C', 'D', 'E' };
char list2[5] = { 'V', 'W', 'X', 'Y', 'Z' };
// Create 2 Listnodes
ListnodePtr Aptr;
ListnodePtr Bptr;
int i;
for ( i=0; i<=5; i++) {
insertnode( &Aptr, list1[i] ); //Initialize listnodeA
insertnode( &Bptr, list2[i] ); //Initialize listnodeB
}
//concatenate( &Aptr, &Bptr );
printlist( &Aptr );
}
int insertnode ( ListnodePtr *nodePtr, char value ) {
ListnodePtr newptr = NULL;
newptr = malloc( sizeof(Listnode) );
if ( newptr != NULL ) {
newptr->data = value;
newptr->nextnode = (*nodePtr);
(*nodePtr) = newptr;
}
}
int printlist ( ListnodePtr *nodePtr ) {
ListnodePtr currentptr;
currentptr = *nodePtr;
while ( currentptr->nextnode != NULL ) {
printf("'%c' --> ", currentptr->data);
currentptr = currentptr->nextnode;
}
puts("NULL");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment