#include<iostream>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<list>
#define CHAR char
#define VARTYPE CHAR //默認存放char類型
using namespace std;
struct myNode;
typedef struct myNode Node;
typedef Node* List;
typedef Node* PtrToNode;
struct myNode
{
VARTYPE data;
PtrToNode next;
PtrToNode prev;
};
//在p位置后插入元素
void _insert(List T,int p,VARTYPE x);
//頭插入元素
void _push_front(List T,VARTYPE x);
//尾部插入元素
void _push_back(List T,VARTYPE x);
//移除內容為x的元素
void _removec(List T,VARTYPE x);
//移除內容為編號為p的元素
void _removep(List T,int p);
//刪除鏈表
void _dellist(List T);
//打印char類型鏈表內容
#ifdef CHAR
void _print(List T);
#endif
//清空鏈表
void _clear(List T);
//創建鏈表
List createlist();
//返回編號為p的元素
VARTYPE _findc(List T,int p);
//鏈表元素個數
int _size(List T);
int main()
{
List T = createlist();
_push_back(T,'a');
_push_back(T,'b');
_push_back(T,'c');
_push_back(T,'d');
_push_back(T,'e');
_push_back(T,'e');
_push_back(T,'d');
_push_front(T,'x');
//_insert(T,2,'p');
//_removec(T,'e');
//_removep(T,3);
//_print(T);
//_clear(T);
_push_back(T,'p');
_push_back(T,'q');
//printf("%d",_size(T));
//printf("%c",_findc(T,3));
_print(T);
_dellist(T);
return 0;
}
//在p位置后插入元素
void _insert(List T,int p,VARTYPE x)
{
PtrToNode tmp;
int i = 0;
while(NULL!=T)
{
T = T->next;
if(++i==p)
break;
}
tmp = (PtrToNode)malloc(sizeof(Node));
if(NULL==tmp)
perror("malloc");
else
{
tmp->data = x;
tmp->next = T;
tmp->prev = T->prev;
T->prev->next = tmp;
T->prev = tmp;
}
};
//尾部插入元素
void _push_back(List T,VARTYPE x)
{
while(NULL!=T->next)
T = T->next;
PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));
if(NULL==tmp)
perror("malloc");
else
{
tmp->data = x;
tmp->next = NULL;
tmp->prev = T;
T->next = tmp;
}
};
//頭部插入元素
void _push_front(List T,VARTYPE x)
{
PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));
if(NULL==tmp)
perror("malloc");
else
{
tmp->data = x;
tmp->prev = T;
tmp->next = T->next;
T->next = tmp;
tmp->next->prev = tmp;
}
};
//移除內容為x的元素
void _removec(List T,VARTYPE x)
{
T = T->next;
while(NULL!=T)
{
if(T->data==x)
{
T = T->prev;
T->next = T->next->next;
free(T->next->prev);
T->next->prev = T;
}
T = T->next;
}
};
//移除內容為編號為p的元素
void _removep(List T,int p)
{
int i = 0;
while(NULL!=T)
{
T = T->next;
if(++i==p)
break;
}
T->prev->next = T->next;
T->next->prev = T->prev;
free(T);
};
//刪除鏈表
void _dellist(List T)
{
PtrToNode tmp = T;
while(NULL!=T)
{
tmp = T->next;
free(T);
T = tmp;
}
};
//打印char類型鏈表內容
#ifdef CHAR
void _print(List T)
{
T = T->next;
while(NULL!=T)
{
printf("%c ",T->data);
T=T->next;
}
};
#endif
//清空鏈表
void _clear(List T)
{
_dellist(*&T->next);
T->next = NULL;
T->prev = NULL;
}
//創建鏈表
List createlist()
{
PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));
tmp->next = NULL;
tmp->prev = NULL;
return tmp;
}
//返回編號為p的元素
VARTYPE _findc(List T,int p)
{
int i = 0;
while(NULL!=T)
{
T = T->next;
if(++i==p)
break;
}
return T->data;
}
//鏈表元素個數
int _size(List T)
{
int i = -1;
while(NULL!=T)
{
T = T->next;
++i;
}
return i;
}
2014/10/15更新
#include<iostream>
#include<cstdio>
#include<string>
#include<malloc.h>
using namespace std;
struct mynode;
typedef struct mynode Node;
struct mynode
{
int data;
Node *next;
Node *prev;
};
char str[] = {"\
-------------------------------------------------\n\
-------C:創建鏈表-------------------------------\n\
-------D:刪除節點-------------------------------\n\
-------I:插入新節點-----------------------------\n\
-------P:輸出節點-------------------------------\n\
-------Q:撤銷鏈表-------------------------------\n\
-------E:退出-----------------------------------\n\
-------------------------------------------------\n\
"
};
Node* createlist()
{
Node* head,*temp,*p;
int n;
head = (Node *)malloc(sizeof(Node));
if(NULL==head)
{
perror("out of space");
exit(1);
}
head->data = -1;
head->next = NULL;
head->prev = NULL;
printf("請輸入節點個數:");
scanf("%d",&n);
p = head;
for(int i=1; i<=n; ++i)
{
temp = (Node *)malloc(sizeof(Node));
if(NULL==temp)
{
perror("out of space");
exit(1);
}
temp->next = NULL;
temp->prev = p;
printf("data%2d:",i);
scanf("%d",&temp->data);
p->next = temp;
p = temp;
}
return head;
}
void dellist(Node *head)
{
Node *p;
while(head)
{
p = head->next;
free(head);
head = p;
}
}
void printlist(Node *head)
{
int i = 1;
Node *p;
head = head->next;
while(head)
{
printf("data%2d: %d\n",i++,head->data);
p = head;
head = head->next;
}
printf("反向\n");
while(p)//測試反向是正確連接
{
printf("data%2d: %d\n",--i,p->data);
p = p->prev;
}
}
void _insert(Node *head)
{
int pos;
Node* temp;
printf("輸入插入節點的位置:");
scanf("%d",&pos);
if(pos<0)
return ;
while(head->next&&pos--)
{
head = head->next;
}
temp = (Node *)malloc(sizeof(Node));
if(NULL==temp)
{
perror("out of space");
exit(1);
}
printf("輸入data:");
scanf("%d",&temp->data);
temp->prev = head;
temp->next = head->next;
head->next->prev = temp;
head->next = temp;
}
void delnode(Node *head)
{
int pos;
printf("輸入刪除節點位置:");
scanf("%d",&pos);
if(pos<1)
return ;
while(pos--)
{
head = head->next;
}
head->prev->next = head->next;
head->next->prev = head->prev;
free(head);
}
int main()
{
char p;
printf(str);
while(scanf("%c",&p)!=EOF)
{
if(p>='a'&&p<='z')p = p-('a'-'A');
Node *_list;
switch(p)
{
case 'C':
dellist(_list);
_list = createlist();
break;
case 'D':
delnode(_list);
break;
case 'I':
_insert(_list);
break;
case 'P':
printlist(_list);
break;
case 'Q':
dellist(_list);
break;
case 'E':
dellist(_list);
return 0;
break;
default:
printf(str);
break;
}
}
}
