/**************
** linkTest.h *
**************/
#include
#include
typedef struct node
{
char name[10];
int age;
struct node *next;
}LinkList;
#define Debug 0
LinkList * createLinkList();
LinkList * initInsert(LinkList * head, int insertNum);//initiate linklist
int lengthLinkList(LinkList * head);
void destroyLinkList(LinkList * head);
void traverseLinklist(LinkList * head); //traversal
void reverse(LinkList *head); //reverse
LinkList * insertList(LinkList * head, int dstNum); // insert data according to the position appointed.
LinkList * deleteLinklist(LinkList * head, int dstNum);
/**************
** linkFuncs.c *
**************/
#include "linkTest.h"
LinkList * createLinkList()
{
LinkList * head;
head = (LinkList *) malloc(sizeof(LinkList));
head->next = NULL;
return head;
}
LinkList * initInsert(LinkList * head, int insertNum)
{
int i = 0;
LinkList * ptrNode = NULL;
LinkList * rear = head;
for(; i < insertNum; i++)
{
ptrNode = (LinkList *)malloc(sizeof(LinkList));
printf("insert person.name person.age\n");
scanf("%s %d",&ptrNode->name,&ptrNode->age);
ptrNode->next = NULL;
rear->next = ptrNode;
rear = rear->next;
}
}
int lengthUpdate(LinkList * head)
{
int count = 0;
LinkList * ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
count++;
}
printf("LinkList len = %d\n",count);
head->age = count;
return head->age+1;
}
void traverseLinklist(LinkList * head)
{
LinkList * ptr = head->next;
printf("person's name | person's age:\n");
while(ptr->next != NULL)
{
printf("%s \t %d\n",ptr->name, ptr->age);
ptr = ptr->next;
}
printf("%s \t %d\n",ptr->name, ptr->age);
}
void reverse(head)
LinkList * head;
{
LinkList * ptr = head;
LinkList * tempr = ptr; //tempr->head; ptr->head;
LinkList * rear = ptr->next; //rear->(head->next)
ptr->next = NULL; //head break
ptr = rear;
rear = rear->next;
ptr->next = NULL; //2 isoluted points:head & 1st-node
tempr = ptr;
while(rear)
{
ptr = rear;
rear = rear->next;
ptr->next = tempr;
tempr = ptr;
}
head->next = ptr;
}
LinkList * insertList(LinkList * head, int dstNum)
{
int len = lengthUpdate(head);
if(dstNum > len || dstNum <1)
{
printf("wrong position!\n");
return 0;
}
int current = 1;
LinkList * ptr = head;
LinkList * newnode = (LinkList *)malloc(sizeof(LinkList));
printf("input: newperson's name | age\n");
scanf("%s %d",&newnode->name, &newnode->age);
while(current < dstNum)
{
ptr = ptr->next;
current++;
}
newnode->next = ptr->next;
ptr->next = newnode;
return head;
}
void destroyLinkList(LinkList * head)
{
int len = lengthUpdate(head);
LinkList * ptr = head;
LinkList * fore =head;
while(len>0)
{
ptr = fore->next;
free(fore);
fore = ptr;
len--;
}
printf("LinkList has been destroyed\n");
}
LinkList * deleteLinkList(LinkList * head, int dstNum)
{
LinkList * ptr = head;
LinkList * qtr = NULL;
int len = lengthUpdate(head);
int current = 1;
if(dstNum > len || dstNum <1)
{
printf("Wrong delete Number\n");
return 0;
}
while(current < dstNum)
{
ptr = ptr->next;
current ++;
}
qtr = ptr->next;
ptr->next = qtr->next;
free(qtr);
}
/**************
** linkTest.c *
**************/
#include "linkTest.h"
int main (argc,argv)
int argc;
char ** argv;
{
if(argc<2)
{
printf("Usage:[xxx] [c]|[num]\n");
return 0;
}
/*********
* create *
*********/
LinkList * head = NULL;
if((*argv[1])== 'c')
head = createLinkList();
else
{
printf("bad parameters!\n");
return 0;
}
int initNum = atoi(argv[2]);
int dstNum;
if(Debug)
printf("insertNum = %d\n",initNum);
initInsert(head,initNum);
traverseLinklist(head);
/*********
* opts *
*********/
printf("Choose insertPositon:\n");
scanf("%d",&dstNum);
printf("dstNum = %d\n",dstNum);
insertList(head,dstNum);
printf("Choose deletePositon:\n");
scanf("%d",&dstNum);
printf("dstNum = %d\n",dstNum);
deleteLinkList(head,dstNum);
// reverse(head);
traverseLinklist(head);
destroyLinkList(head);
}
/************************************************************************************************************/
/*
*功能:显示在create函数中的malloc并不能随着create函数结束而返回。必须用**T才能返回。
*这个现象与swap(a,b)相同。
*更多启示:
*/
#include
#include
struct tree{
int data;
char * name;
};
int create (struct tree ** T){
*T = (struct tree *)malloc(sizeof(struct tree));
(*T)->data = 17;
(*T)->name = "baoshuai";
}
int main(){
struct tree *T;
create(&T);
printf("age = %d, name = %s\n", T->data, T->name);
free(T);
}
阅读(696) | 评论(0) | 转发(0) |