自从20130309结束培训的时候,我发现一个规律,也是自己的毛病:贪多必失,盲目自信,眼高手低。
把链表的操作都给写错了。呵呵,基本功呢,所以今日附上
/*
function:链表的相关操作
write by:342639355
time: 2013/03/10
*/
#ifndef _MYLIST_H
#define _MYLIST_H
// data struct
struct data
{
int a;
char b;
};
// element of list
struct ele
{
data mydata;
ele* next;
};
// initialize the list
ele* init(ele* head);
// insert the element to the head
ele* push_front(ele* head, ele mydata);
// insert the element to the tail
ele* push_back(ele* head, ele mydata);
// clear the list
void clearList(ele* head);
// print the element
void print(ele* head);
#endif
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "mylist.h"
// initialize the list
// the head not reserve the data
ele* init(ele* head)
{
assert(NULL==head);
head = (ele*)malloc(sizeof(ele));
assert(NULL!=head);
head->next = NULL;
return head;
}
// insert the element to the head
ele* push_front(ele* head, ele mydata)
{
if (NULL==head)
{
printf("You must initialize the list before first use!\n");
return NULL;
}
// insert to the head
ele* tmp = (ele*)malloc(sizeof(ele));
memcpy(tmp, &mydata, sizeof(ele));
assert(NULL!=head);
tmp->next = head->next;
head->next = tmp;
return head;
}
// insert the element to the tail
ele* push_back(ele* head, ele mydata)
{
if (NULL==head)
{
printf("You must initialize the list before first use!\n");
return NULL;
}
// copy data
ele* tmp = (ele*)malloc(sizeof(ele));
memcpy(tmp, &mydata, sizeof(ele));
assert(NULL!=head);
// find tail of list
ele* tail = head;
while(NULL!=tail->next)
{
tail = tail->next;
}
tmp->next = NULL;
tail->next = tmp;
// insert to the tail
return head;
}
// clear the list
void clearList(ele* head)
{
if ( NULL==head )
{
return;
}
ele* tempFree = head;
while ( tempFree )
{
head = head->next;
free(tempFree);
tempFree = head;
}
}
// print the element
void print(ele* head)
{
printf("data is:\n");
ele* curr = head->next;
while (curr)
{
printf("%d %c\n",curr->mydata.a,curr->mydata.b);
curr = curr->next;
}
}
#include <stdio.h>
#include "mylist.h"
// global var
ele* g_head=NULL;
void printMenu()
{
printf(" welcome to list\n");
printf(" 1. init the list\n");
printf(" 2. insert data to head\n");
printf(" 3. insert data to tail\n");
printf(" 4. print the list\n");
printf(" 5. clear the list\n");
printf(" 6. quit\n");
}
int main()
{
unsigned int choose;
printMenu();
scanf("%d",&choose);
ele tmp;
while (1)
{
switch (choose)
{
case 1:
g_head=init(g_head);
break;
case 2:
printf("please input the number\n");
printf("the data is:");
scanf("%d",&tmp.mydata.a);
fflush(stdin);
printf("the data is:");
scanf("%c",&tmp.mydata.b);
g_head = push_front(g_head, tmp);
break;
case 3:
printf("please input the number\n");
printf("the data is:");
scanf("%d",&tmp.mydata.a);
fflush(stdin);
printf("the alpha is:");
scanf("%c",&tmp.mydata.b);
g_head = push_back(g_head, tmp);
break;
case 4:
print(g_head);
break;
case 5:
clearList(g_head);
break;
case 6:
return 0;
default:
printf("not Valid operation,choose again!\n");
}
printMenu();
scanf("%d",&choose);
}
return 0;
}