自己實現C語言雙向向鍊錶

2021-06-25 20:11:33 字數 2902 閱讀 5978

#include#include#include#include#include#define char char

#define vartype char //預設存放char型別

using namespace std;

struct mynode;

typedef struct mynode node;

typedef node* list;

typedef node* ptrtonode;

struct mynode

;//在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()

//在p位置後插入元素

void _insert(list t,int p,vartype x)

tmp = (ptrtonode)malloc(sizeof(node));

if(null==tmp)

perror("malloc");

else

};//尾部插入元素

void _push_back(list t,vartype x)

};//頭部插入元素

void _push_front(list t,vartype x)

};//移除內容為x的元素

void _removec(list t,vartype x)

t = t->next;

}};//移除內容為編號為p的元素

void _removep(list t,int p)

t->prev->next = t->next;

t->next->prev = t->prev;

free(t);

};//刪除鍊錶

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)

return t->data;

}//鍊錶元素個數

int _size(list t)

return i;

}

2014/10/15更新

#include#include#include#includeusing namespace std;

struct mynode;

typedef struct mynode node;

struct mynode

;char str = ;

node* createlist()

head->data = -1;

head->next = null;

head->prev = null;

printf("請輸入節點個數:");

scanf("%d",&n);

p = head;

for(int i=1; i<=n; ++i)

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)

}void printlist(node *head)

printf("反向\n");

while(p)//測試反向是正確連線

}void _insert(node *head)

temp = (node *)malloc(sizeof(node));

if(null==temp)

printf("輸入data:");

scanf("%d",&temp->data);

temp->prev = head;

temp->next = head->next;

head->next->prev = temp;

head->next = temp;

}void delnode(node *head)

head->prev->next = head->next;

head->next->prev = head->prev;

free(head);

}int main()

}}

雙向鍊錶C語言實現

ifndef stdlist h define stdlist h typedef struct tagstdnode stdnode,lpstdnode typedef struct tagstdlist stdlist,lpstdlist 鍊錶資料結構 struct tagstdnode 鍊錶節...

C語言方式實現雙向鍊錶

太久不看資料結構都忘記了,所以現在複習下雙向鍊錶,如下 list.h 雙向鍊錶 ifndef list h define list h include include 節點 typedef struct listnode list node 鍊錶 typedef struct list list 初...

c語言實現雙向鍊錶

單向鍊錶有一定的缺陷,其中乙個就是只能一條路走到黑,只能前進不能後退,但雙向鍊錶就解決了這一問題 include include typedef struct node node,linklist void create list tail linklist l 頭插法建立 void create ...