Elliot Lee
asked on
I am learning c programming by my own effort and I stumbled upon this linked list and I have no idea how this works.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "assignment.h"
#include "list.h"
void ex7() {
node_t *a=Initialize('1');
node_t *b=NULL;
PrintList(a);
InsertFirstList(&a, 'V');
InsertFirstList(&a, 'M');
PrintList(a);
InsertLastList(&a, 'C');
PrintList(a);
SplitList(&a, &b, 2);
PrintList(a);
PrintList(b);
DeleteFirstList(&a);
PrintList(a);
InsertLastList(&a, 'G');
DeleteLastList(&b);
PrintList(b);
InsertLastList(&b,'0');
PrintList(b);
InsertLastList(&b, '1');
PrintList(b);
MergeList(&a,&b);
PrintList(a);
char target='G';
printf("Count '%c': %d\n",target, SearchList(&a,target));
target='1';
printf("Count '%c': %d\n",target, SearchList(&a,target));
FreeList(&a);
}
node_t *Initialize(char ch) {
node_t *head;
head=(node_t*)calloc(1,siz eof(node_t ));
if(head==NULL){
fprintf(stderr,"Failed to assign memory!\n");
exit(-1);
}
head->next=NULL;
head->ch=ch;
return head;
}
void PrintList(node_t *head) {
node_t *temp=head;
printf("***Print Linked List***\n");
while(temp!=NULL) {
printf("%c ",temp->ch);
temp=temp->next;
}
printf("\n****Print Finished****\n\n");
}
void FreeList(node_t **head) {
node_t *tmp=NULL;
node_t *pHead=*head;
while(pHead->next!=NULL) {
tmp=pHead;
pHead=pHead->next;
free(tmp);
}
free(pHead);
}
bool IsEmptyList(node_t *head){
bool IsEmpty;
IsEmpty= head==NULL ? false : true;
return IsEmpty;
}
void InsertFirstList(node_t **head,char insert_char){
node_t *newHead;
node_t *temp=*head;
newHead=(node_t *)calloc(1,sizeof(node_t)) ;
if(newHead==NULL){
fprintf(stderr,"Failed to assign memory!\n");
exit(-1);
}
*head=newHead;
newHead->next=temp;
newHead->ch=insert_char;
}
void InsertLastList(node_t **head,char insert_char){
node_t *newTail;
newTail=(node_t *)calloc(1,sizeof(node_t)) ;
if(newTail==NULL){
fprintf(stderr,"Failed to assign memory!\n");
exit(-1);
}
node_t *temp=*head;
while(temp->next!=NULL){
temp=temp->next;
}
newTail->next=NULL;
temp->next = newTail;
newTail->ch=insert_char;
}
void DeleteFirstList(node_t **head){
node_t *temp=*head;
*head=(*head)->next;
free(temp);
}
void DeleteLastList(node_t **head){
node_t *temp=*head;
while((temp->next)->next!= NULL){//ge t to the second last one
temp=temp->next;
}
free((temp->next)->next);
temp->next=NULL;
}
int SizeList(node_t *head){
node_t *temp=head;
int length=0;
while(temp->next!=NULL){
temp=temp->next;
length++;
}
return length;
}
int SearchList(node_t **head,char target){
node_t *temp=*head;
int time=0;
while(temp->next!=NULL){
temp=temp->next;
if(temp->ch==target) time++;
}
return time;
}
void SplitList(node_t **head,node_t **tail, int pos){
node_t *temp=*head;
for(int i=0;i<pos-1;i++){
temp=temp->next;
}
*tail=temp->next;
temp->next=NULL;
}
void MergeList(node_t **head1,node_t **head2){
node_t *temp=*head1;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=*head2;
}
So I found out these nodes have certain functions to initialize, print the list, free the list and so on. I do not understand what the each nodes means and i dont know how the codes in the nodes(Void Initialize, void PrintList....) work. Can somebody help me out understanding it? (comments in the codes would be very appreciated).
#include <stdlib.h>
#include <string.h>
#include "assignment.h"
#include "list.h"
void ex7() {
node_t *a=Initialize('1');
node_t *b=NULL;
PrintList(a);
InsertFirstList(&a, 'V');
InsertFirstList(&a, 'M');
PrintList(a);
InsertLastList(&a, 'C');
PrintList(a);
SplitList(&a, &b, 2);
PrintList(a);
PrintList(b);
DeleteFirstList(&a);
PrintList(a);
InsertLastList(&a, 'G');
DeleteLastList(&b);
PrintList(b);
InsertLastList(&b,'0');
PrintList(b);
InsertLastList(&b, '1');
PrintList(b);
MergeList(&a,&b);
PrintList(a);
char target='G';
printf("Count '%c': %d\n",target, SearchList(&a,target));
target='1';
printf("Count '%c': %d\n",target, SearchList(&a,target));
FreeList(&a);
}
node_t *Initialize(char ch) {
node_t *head;
head=(node_t*)calloc(1,siz
if(head==NULL){
fprintf(stderr,"Failed to assign memory!\n");
exit(-1);
}
head->next=NULL;
head->ch=ch;
return head;
}
void PrintList(node_t *head) {
node_t *temp=head;
printf("***Print Linked List***\n");
while(temp!=NULL) {
printf("%c ",temp->ch);
temp=temp->next;
}
printf("\n****Print Finished****\n\n");
}
void FreeList(node_t **head) {
node_t *tmp=NULL;
node_t *pHead=*head;
while(pHead->next!=NULL) {
tmp=pHead;
pHead=pHead->next;
free(tmp);
}
free(pHead);
}
bool IsEmptyList(node_t *head){
bool IsEmpty;
IsEmpty= head==NULL ? false : true;
return IsEmpty;
}
void InsertFirstList(node_t **head,char insert_char){
node_t *newHead;
node_t *temp=*head;
newHead=(node_t *)calloc(1,sizeof(node_t))
if(newHead==NULL){
fprintf(stderr,"Failed to assign memory!\n");
exit(-1);
}
*head=newHead;
newHead->next=temp;
newHead->ch=insert_char;
}
void InsertLastList(node_t **head,char insert_char){
node_t *newTail;
newTail=(node_t *)calloc(1,sizeof(node_t))
if(newTail==NULL){
fprintf(stderr,"Failed to assign memory!\n");
exit(-1);
}
node_t *temp=*head;
while(temp->next!=NULL){
temp=temp->next;
}
newTail->next=NULL;
temp->next = newTail;
newTail->ch=insert_char;
}
void DeleteFirstList(node_t **head){
node_t *temp=*head;
*head=(*head)->next;
free(temp);
}
void DeleteLastList(node_t **head){
node_t *temp=*head;
while((temp->next)->next!=
temp=temp->next;
}
free((temp->next)->next);
temp->next=NULL;
}
int SizeList(node_t *head){
node_t *temp=head;
int length=0;
while(temp->next!=NULL){
temp=temp->next;
length++;
}
return length;
}
int SearchList(node_t **head,char target){
node_t *temp=*head;
int time=0;
while(temp->next!=NULL){
temp=temp->next;
if(temp->ch==target) time++;
}
return time;
}
void SplitList(node_t **head,node_t **tail, int pos){
node_t *temp=*head;
for(int i=0;i<pos-1;i++){
temp=temp->next;
}
*tail=temp->next;
temp->next=NULL;
}
void MergeList(node_t **head1,node_t **head2){
node_t *temp=*head1;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=*head2;
}
So I found out these nodes have certain functions to initialize, print the list, free the list and so on. I do not understand what the each nodes means and i dont know how the codes in the nodes(Void Initialize, void PrintList....) work. Can somebody help me out understanding it? (comments in the codes would be very appreciated).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.