- #ifndef _LINKEDSTRING_H
- #define _LINKEDSTRING_H
- typedef struct snode
- {
- char ch;
- struct snode *next;
- }LiString;
- #endif
- #include <stdio.h>
- #include "stdlib.h"
- #include "LinkedString.h"
- void StrAssign(LiString *l, char ch[])
- {
- int loop1=0; LiString *head = NULL, *rear = NULL;
- l = (LiString *)malloc(sizeof(LiString));
- l->next = NULL; head = l;
- while(ch[loop1] != '\0')
- {
- rear = (LiString *)malloc(sizeof(LiString));
- rear->ch = ch[loop1]; rear->next = NULL;
- head->next = rear; head = rear;
- loop1++;
- }
- }
- void StrCpy(LiString *str1, LiString *str2)
- {
- LiString *tmp1 = NULL,*head1 = NULL, *tmp2 = NULL;
- tmp2 = str2->next;
- str1 = (LiString *)malloc(sizeof(LiString));
- head1 = str1;
- while(tmp2 != NULL)
- {
- tmp1 = (LiString *)malloc(sizeof(LiString));
- tmp1->ch = tmp2->ch;
- head1->next = tmp1;
- head1 = tmp1;
- tmp2 = tmp2->next;
- }
- }
- int StrLen(LiString *str)
- {
- int len = 0;
- LiString *tmp = str->next;
- while(tmp != NULL)
- {
- len++;
- tmp = tmp->next;
- }
- return len;
- }
- LiString *ConCat(LiString *str1, LiString *str2)
- {
- LiString *total = NULL, *tmp1 = NULL, *rear = NULL, *tmp2 = str1->next;
- total = (LiString *)malloc(sizeof(LiString));
- total->next = NULL; rear = total;
- while(tmp2 != NULL)
- {
- tmp1 = (LiString *)malloc(sizeof(LiString));
- tmp1->ch = tmp2->ch;
- rear->next = tmp1;
- rear = tmp1;
- tmp2 = tmp2->next;
- }
- tmp2 = str2->next;
- while(tmp2 != NULL)
- {
- tmp1 = (LiString *)malloc(sizeof(LiString));
- tmp1->ch = tmp2->ch;
- rear->next = tmp1;
- rear = tmp1;
- tmp2 = tmp2->next;
- }
- return total;
- }
- LiString *SubStr(LiString *str, int begin, int num)
- {
- int loop1 = 0;
- LiString *substr = NULL,*rear = NULL, *tmp1 = NULL, *tmp2 = NULL;
- substr = (LiString *)malloc(sizeof(LiString));
- substr->next = NULL; rear = substr;
- tmp2 = str->next;
- while(begin > 0 && tmp2 != NULL)
- {
- tmp2 = tmp2->next;
- begin--;
- }
- for(loop1=0; loop1<num && tmp2 != NULL; loop1++)
- {
- tmp1 = (LiString *)malloc(sizeof(LiString));
- tmp1->ch = tmp2->ch;
- rear->next = tmp1;
- rear = tmp1;
- tmp2 = tmp2->next;
- }
- return substr;
- }
- LiString *InsStr(LiString *str1,LiString *str2, int loc)
- {
- int loop1=0;
- LiString *total = NULL, *tmp1 = NULL, *tmp2 = NULL;
- total = (LiString *)malloc(sizeof(LiString));
- total->next = NULL;
- tmp1 = total;
- str1 = str1->next;
- str2 = str2->next;
- for(loop1=0; loop1<loc; loop1++)
- {
- tmp2 = (LiString *)malloc(sizeof(LiString));
- tmp2->ch = str1->ch;
- tmp1->next = tmp2;
- tmp1 = tmp2;
- str1 = str1->next ;
- }
- while(str2 != NULL)
- {
- tmp2 = (LiString *)malloc(sizeof(LiString));
- tmp2->ch = str2->ch;
- tmp1->next = tmp2;
- tmp1 = tmp2;
- str2 = str2->next ;
- }
- while(str1 != NULL)
- {
- tmp2 = (LiString *)malloc(sizeof(LiString));
- tmp2->ch = str1->ch;
- tmp1->next = tmp2;
- tmp1 = tmp2;
- str1 = str1->next ;
- }
- return total;
- }
- LiString *DelStr(LiString *str,int begin, int num)
- {
- int loop1 = 0;
- LiString *total = NULL, *tmp = NULL, *rear = NULL;
- total = (LiString *)malloc(sizeof(LiString));
- total->next = NULL;
- str = str->next ;
- while(loop1 < begin)
- { tmp = (LiString *)malloc(sizeof(LiString));
- tmp->ch = str->ch;
- rear->next = tmp;
- rear = tmp;
- str = str->next;
- }
- while(num > 0 && str != NULL)
- { num--;
- str = str->next ;
- }
- while(str != NULL)
- {
- tmp = (LiString *)malloc(sizeof(LiString));
- tmp->ch = str->ch;
- rear->next = tmp;
- rear = tmp;
- str = str->next;
- }
- return total;
- }
转载于:https://blog.51cto.com/saibro/1183182