#include "stdlib.h"
typedef int ElemType//元素类型
typedef struct DuLNode
ElemType data
struct DuLNode *prior, *next
}DuLNode,*DuLinkList
int Create(DuLinkList &L)
{//建立双向链表
DuLinkList p,q
ElemType n,i
L = (DuLinkList) malloc (sizeof(DuLNode))
L->next = NULL
q = L
printf("输入数据个数:")
scanf("%d",&n)
printf("输入数据元素:")
for ( i = 0i <ni++)
{
p = (DuLinkList) malloc (sizeof(DuLNode))
scanf("%d",&p->data)//插入数据
p->prior = q
q->next = p
p->next = 0
q = q->next
}
return 1
}
int Visit(DuLinkList &L)
{//遍历双向链表
DuLinkList p
p = L->next
printf("双向链表为:")
while (p)
{
printf("%4d",p->data)
p = p->next
}
printf("\n")
return 1
}
int Clear(DuLinkList &L)
{
DuLinkList p
p = L->next
while(p)
{
L->next = p->next
free(p)
p = L->next
}
return 1
}
main()
{
int i,e,s,num
char c='y'
DuLinkList L
Create(L)
Visit(L)
while (c=='y')
{
printf("\n\n\n1.双向链表插入元素\n2.双向链表中删除元素\n")
printf("3.判断双向链表元素是否对称\n")
printf("4.双向链表中奇数排在偶数前面\n")
printf("5.建立递增链表并有序插入元素\n\n")
printf("选择需要的操作\n\n")
scanf("%d",&num)
switch(num)
{
case 1:
printf("输入插入元素位置以及元素:\n")
scanf("%d%d",&i,&e)
ListInsert(L, i, e)
Visit(L)
break
case 2:
printf("输入删除元素位置:")
scanf("%d",&i)
Delete(L,i,s)
printf("删除的元素为:%d\n",s)
Visit(L)
break
case 3:
if(Same(L)) printf("链表对称\n")
else printf("链表不对称\n")
break
case 5:
printf("清空原链表,建立递增链表:\n")
XCreate(L)
Visit(L)
break
case 4:
printf("奇数放在偶数前面:\n")
Jiou(L)
Visit(L)
break
}
printf("继续操作(y/n):\n")
scanf("%s",&c)
}
}
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
struct list{
int data
struct list *next
struct list *pre
}
typedef struct list node
typedef node *link
link front=NULL,rear,ptr,head=NULL
link push(int item){
link newnode=(link)malloc(sizeof(node))
newnode->data=item
if(head==NULL)
{
head=newnode
head->next=NULL
head->pre=NULL
rear=head
}
else
{
rear->next=newnode
newnode->pre=rear
newnode->next=NULL
rear=newnode
}
return head
}
void makenull(){
front=NULL
rear=NULL
}
empty(){
if(front==NULL)
return 1
else
return 0
}
int tops(){
if(empty())
return NULL
else
return rear->data
}
void pop(){
if(empty())
printf("stack is empty!\n")
else
rear=rear->pre
}
void display(link l){
link p
p=l
while(p!=NULL){
printf("%d->",p->data)
p=p->next
}
}
void main(){
int n,i
printf("input your first data!\n")
scanf("%d",&n)
front=push(n)
/*another data*/
for(i=0i<3i++)
{
printf("input:\n")
scanf("%d",&n)
push(n)
}
ptr=front
display(ptr)
printf("\n Please enter any key to pop")
pop()
ptr=front
display(ptr)
}
双层链表是什么?数组链表还是双链表,如果是后者,使用尾查发的时候多一行代码就OK,就是指向前面一个节点。1 #include<stdio.h>
2 #include<malloc.h>
3 struct nodeone
4 {
5 int data
6 struct nodeone *next
7 struct nodeone *down
8 }
9
10 struct nodetwo
11 {
12 int data
13 struct nodetwo *two
14 }
15 void main()
16 {
17 int i = 0, j = 0
18 struct nodeone *head, *p1, *p2, *p3
19 p2 = head = (struct nodeone*)malloc(sizeof(struct nodeone))
20 while(i <10)
21 {
22 p1 = (struct nodeone*)malloc(sizeof(struct nodeone))
23 p1->down = NULL
24 while(j <5)
25 {
26 p3 = (struct nodetwo*)malloc(sizeof(struct nodetwo))
27 p3->next = p1->down
28 p1->down = p3
j++
29 }
30 p2->next = p1
31 p2 = p1
i++ //忘记了这两个计数变量,i和j。
32 }
33 p2->next = NULL
34 }
~
理论上是对的,Linux下编译通过。外层使用尾插,内层使用头插,主要是为了减少一个变量。
我以前没有听过双层链表,不过记住这些名字还是有点用处。