#include <stdio.h>
typedef char datatype/*单链表的类型定义*/
typedef struct node {
datatype data
struct node *next
} linklist
linklist *creatlink() { /*建立一个单链表,返回单链表的头指针*/
char ch
linklist *p,*head,*r
ch = getchar()
head = p = (linklist *)malloc(sizeof(linklist))
r = head
while(ch != '$') {
p->next = (linklist *)malloc(sizeof(linklist))
p->next->data = ch
p = p->next
ch = getchar()
}
p->next = NULL
return head
}
void printList(linklist *head) {/*遍历单链表,并输出每个结点的数据*/
linklist *p = head->next
printf("\t\t")
while(p) {
printf("%c ",p->data)
p = p->next
}
printf("\n\n")
}
void insert(linklist *p,datatype x) {/*一个数值为x的新结点插友肢入到*p结点之后*/
/*请将函数体的代码补充完整*/
linklist *newnode = (linklist *)malloc(sizeof(linklist))
newnode->data = x
newnode->next = p->next
p->next = newnode
}
void deleteNode(linklist *head,linklist *p) {/*删除头指针为head的单链表上的*p结点*/
/*请将函数体的代码补充完整*/
linklist *q = head
while(q->next != p &&q) q = q->next
if(q) {
q->next = p->next
free(p)
}
else printf("没有找到指定的结点p, *** 作失败!!!\n")
}
linklist *get(linklist *head, int i) {/*查找单链表中的第i个结点,并返回其地址*/
linklist *p = head
int k = 0
while(k <i &&p) {
p = p->next
k++
}
return p
}
void main() {/*主函数*/
linklist *head,*f
head = creatlink()/*建立一个单链表,head存储表头指针*/
printList(head)/*遍历输出好喊世该单链表*/
f = get(head, 2)/*查找单链表渗差中的第2个结点,将其地址存储在变量f中*/
if(f) printf("\nthis is 2 node:%c\n", f->data)/*输出第2个结点的值*/
/*在第2个结点后面插入一个新结点,其值为’w’*/
insert(head,'w')//这个函数的功能没有构思好,因为所给的参数的限制
printf("\n after insert a new node:")
printList(head)/*遍历输出该单链表*/
f= get(head,5)/*查找单链表中的第5个结点,将其地址存储在变量f中*/
if(f) printf("\nthis is 5 node:%c" )/*输出第5个结点的值*/
/*删除第5个结点*/
deleteNode(head,f)
printf("\n after delete a node:")
printList(head)/*遍历输出该单链表*/
}
#include "stdafx.h"穗尘碰
#include <iostream>
void print(int a[], int n)
{
int i
for (i = 0i <兄慧 ni++)
printf("%4d", a[i])
printf("猜谈\n")
}
void append(int a[], int alen, int b[], int blen)
{
int i, j, t
i = 0
j = alen-1
while (i-j)
{
t = a[i]
a[i] = a[j]
a[j] = t
i++
j--
}
i = alen
j = blen - 1
while (j>=0)
a[i++] = b[j--]
}
int main()
{
int a[10] = { 13,10,5,3,1 }, b[] = { 4,6,9,15 }
append(a, 5, b, 4)
print(a, 9)
getchar()
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)